UNPKG

3.69 kBPlain TextView Raw
1/* eslint-disable @typescript-eslint/no-explicit-any */
2import type { CacheAxiosResponse } from 'axios-cache-interceptor';
3
4/** The internal axios state used by `axios-cache-hooks` */
5export type State<T> = LoadingState | ErrorState | SuccessState<T>;
6
7export type LoadingState = {
8 /** If the current request is loading */
9 loading: true;
10 /** The returned data for this request, if present */
11 data?: undefined;
12 /** The error thrown by this http request, if any */
13 error?: undefined;
14 /** The complete `CacheAxiosResponse` */
15 response?: undefined;
16 /** Last response data unique id. @internal */
17 rid?: number;
18};
19
20export type ErrorState = {
21 /** If the current request is loading */
22 loading: false;
23 /** The returned data for this request, if present */
24 data?: undefined;
25 /** The error thrown by this http request, if any */
26 error: unknown;
27 /** The complete `CacheAxiosResponse` */
28 response?: CacheAxiosResponse;
29 /** Last response data unique id. @internal */
30 rid: number;
31};
32
33export type SuccessState<D> = {
34 /** If the request is loading */
35 loading: false;
36 /** The returned data for this request, if present */
37 data: D;
38 /** The error thrown by this http request, if any */
39 error?: undefined;
40 /** The complete `CacheAxiosResponse` */
41 response: CacheAxiosResponse<D>;
42 /** Last response data unique id. @internal */
43 rid: number;
44};
45
46/**
47 * This is where you should define your custom axios request logic.
48 *
49 * **Note**: Remember to ALWAYS spread the {@link AxiosRequestConfig} object in the axios call.
50 */
51export type ApiCall<D, A extends unknown[]> = (
52 ...args: A
53) => Promise<CacheAxiosResponse<D>>;
54
55/**
56 * The current request state.
57 *
58 * The response data was provided as the in the first object of this tuple.
59 */
60export type DataLessState<D> = Omit<State<D>, 'data'>;
61
62export type AxiosCacheHooks = {
63 /**
64 * Uses the provided `apiCall` to execute a axios request.
65 *
66 * Make sure the `apiCall` function has an `CacheRequestConfig` argument in the same
67 * index as your `configIndexFinder` defined.
68 *
69 * **Note**: This is different from `useMutation` because it calls the axios request
70 * directly on the first render.
71 *
72 * @example
73 *
74 * ```tsx
75 * const [user, { loading }] = useQuery(getUser, 'Arthur');
76 *
77 * if (loading) {
78 * return <div>Loading...</div>;
79 * }
80 *
81 * return <div>{user.name}</div>;
82 * ```
83 *
84 * @param apiCall The function that will return a {@link CacheAxiosResponse}
85 * @param args Arguments to pass to the `apiCall`. You can change this arguments without
86 * any problems.
87 * @see http://tinylibs.js.org/packages/axios-cache-hooks
88 */
89 useQuery<D, A extends unknown[]>(
90 this: void,
91 apiCall: ApiCall<D, A>,
92 ...args: A
93 ): [data: D | undefined, info: DataLessState<D>];
94
95 /**
96 * Uses the provided `apiCall` to execute a axios request when the returned `refetch`
97 * function is called.
98 *
99 * Make sure the `apiCall` function has an `CacheRequestConfig` argument in the same
100 * index as your `configIndexFinder` defined.
101 *
102 * **Note**: This is different from `useQuery` because it **DOES NOT** calls the axios
103 * request on the first render.
104 *
105 * @example
106 *
107 * ```tsx
108 * const [state, refetch] = useMutation(createUser);
109 *
110 * return <button onClick={() => refetch('Arthur')}>Create profile!</button>;
111 * ```
112 *
113 * @param apiCall The function that will return a {@link CacheAxiosResponse}.
114 * @see http://tinylibs.js.org/packages/axios-cache-hooks
115 */
116 useMutation<D, A extends unknown[]>(
117 this: void,
118 apiCall: ApiCall<D, A>
119 ): [state: State<D>, refetch: (...args: A) => void];
120};