/
raw.ts
79 lines (71 loc) · 1.78 KB
/
raw.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AxiosInstance, AxiosRequestConfig } from 'axios'
import errorHandler from '../../../error-handler'
function getBaseUrl(http: AxiosInstance) {
return http.defaults.baseURL?.split('/spaces')[0]
}
export function get<T = any>(http: AxiosInstance, url: string, config?: AxiosRequestConfig) {
return http
.get<T>(url, {
baseURL: getBaseUrl(http),
...config,
})
.then((response) => response.data, errorHandler)
}
export function patch<T = any>(
http: AxiosInstance,
url: string,
payload?: any,
config?: AxiosRequestConfig
) {
return http
.patch<T>(url, payload, {
baseURL: getBaseUrl(http),
...config,
})
.then((response) => response.data, errorHandler)
}
export function post<T = any>(
http: AxiosInstance,
url: string,
payload?: any,
config?: AxiosRequestConfig
) {
return http
.post<T>(url, payload, {
baseURL: getBaseUrl(http),
...config,
})
.then((response) => response.data, errorHandler)
}
export function put<T = any>(
http: AxiosInstance,
url: string,
payload?: any,
config?: AxiosRequestConfig
) {
return http
.put<T>(url, payload, {
baseURL: getBaseUrl(http),
...config,
})
.then((response) => response.data, errorHandler)
}
export function del<T = any>(http: AxiosInstance, url: string, config?: AxiosRequestConfig) {
return http
.delete<T>(url, {
baseURL: getBaseUrl(http),
...config,
})
.then((response) => response.data, errorHandler)
}
export function http<T = any>(
http: AxiosInstance,
url: string,
config?: Omit<AxiosRequestConfig, 'url'>
) {
return http(url, {
baseURL: getBaseUrl(http),
...config,
}).then((response) => response.data as T, errorHandler)
}