-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaxios-instance.ts
More file actions
35 lines (29 loc) · 996 Bytes
/
axios-instance.ts
File metadata and controls
35 lines (29 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Axios, {
type AxiosError,
type AxiosRequestConfig,
type AxiosResponse,
} from 'axios';
import env from '../env';
export const AXIOS_INSTANCE = Axios.create({
baseURL: env.EXPO_PUBLIC_API_URL,
});
// add a second `options` argument here if you want to pass extra options to each generated query
export const customAxios = <T>(
config: AxiosRequestConfig,
options?: AxiosRequestConfig,
): Promise<AxiosResponse<T, unknown>> => {
const source = Axios.CancelToken.source();
const promise = AXIOS_INSTANCE({
...config,
...options,
cancelToken: source.token,
}).then((data) => data);
// @ts-expect-error: The cancel method is not typed.
promise.cancel = () => {
source.cancel('Query was cancelled');
};
return promise;
};
// In some case with react-query and swr you want to be able to override the return error type so you can also do it here like this
export type ErrorType<Error> = AxiosError<Error>;
export type BodyType<BodyData> = BodyData;