-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxios.ts
44 lines (39 loc) · 2.93 KB
/
axios.ts
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
36
37
38
39
40
41
42
43
44
/**
* This file provides utility functions for making HTTP requests using Axios.
* It includes types and helper methods to facilitate various HTTP methods (GET, POST, PUT, PATCH, DELETE).
* The main functions are:
* - `axiosRequest`: A generic function to make a customizable Axios request.
* - Specific methods for each HTTP verb (`axiosGet`, `axiosPost`, `axiosPut`, `axiosPatch`, `axiosDelete`),
* and their shorthand variants (`$axiosGet`, `$axiosPost`, `$axiosPut`, `$axiosPatch`, `$axiosDelete`),
* which directly return the `data` property from the response.
*/
import axios from 'axios';
import type {
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
Method,
} from 'axios';
export { AxiosError } from 'axios';
export type AxiosRequestData = AxiosRequestParams | FormData;
export type AxiosRequestParams = Record<number | string, any>;
export const axiosInstance: AxiosInstance = axios.create({});
export const axiosDelete = <T = string>(url: string, params?: AxiosRequestParams, config?: AxiosRequestConfig) => axiosRequest<T>(url, 'delete', params, {}, config);
export const $axiosDelete = async <T = string>(url: string, params?: AxiosRequestParams, config?: AxiosRequestConfig) => (await axiosDelete<T>(url, params, config)).data;
export const axiosGet = <T = any>(url: string, params?: AxiosRequestParams, config?: AxiosRequestConfig) => axiosRequest<T>(url, 'get', params, {}, config);
export const $axiosGet = async <T = any>(url: string, params?: AxiosRequestParams, config?: AxiosRequestConfig) => (await axiosGet<T>(url, params, config)).data;
export const axiosPatch = <T = string>(url: string, data?: AxiosRequestData, config?: AxiosRequestConfig<AxiosRequestData>) => axiosRequest<T>(url, 'patch', {}, data, config);
export const $axiosPatch = async <T = string>(url: string, data?: AxiosRequestData, config?: AxiosRequestConfig<AxiosRequestData>) => (await axiosPatch<T>(url, data, config)).data;
export const axiosPost = <T = string>(url: string, data?: AxiosRequestData, config?: AxiosRequestConfig<AxiosRequestData>) => axiosRequest<T>(url, 'post', {}, data, config);
export const $axiosPost = async <T = string>(url: string, data?: AxiosRequestData, config?: AxiosRequestConfig<AxiosRequestData>) => (await axiosPost<T>(url, data, config)).data;
export const axiosPut = <T = string>(url: string, data?: AxiosRequestData, config?: AxiosRequestConfig<AxiosRequestData>) => axiosRequest<T>(url, 'put', {}, data, config);
export const $axiosPut = async <T = string>(url: string, data?: AxiosRequestData, config?: AxiosRequestConfig<AxiosRequestData>) => (await axiosPut<T>(url, data, config)).data;
export function axiosRequest<T = any, R extends AxiosResponse<T> = AxiosResponse<T>, D extends AxiosRequestData = any>(url: string, method: Method, params?: AxiosRequestData, data?: D, config?: AxiosRequestConfig) {
return axiosInstance.request<T, R, D>({
...config,
data,
method,
params,
url,
});
}