-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathapi.ts
More file actions
100 lines (87 loc) · 2.36 KB
/
api.ts
File metadata and controls
100 lines (87 loc) · 2.36 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import { REQUEST_TIMEOUT_MS, SERVER_HOST } from "constants/apiConstants";
import {
apiFailureResponseInterceptor,
apiRequestInterceptor,
apiSuccessResponseInterceptor,
} from "api/apiUtils";
import { API_REQUEST_HEADERS } from "constants/apiConstants";
import { sdkConfig } from "constants/sdkConfig";
import _ from "lodash";
let axiosIns: AxiosInstance | null = null;
function getAxiosInstance() {
if (axiosIns) {
return axiosIns;
}
const apiRequestConfig: AxiosRequestConfig = {
baseURL: `${_.trimEnd(sdkConfig.baseURL || SERVER_HOST, "/")}/api/`,
timeout: REQUEST_TIMEOUT_MS,
headers: API_REQUEST_HEADERS,
withCredentials: true,
};
const axiosInstance: AxiosInstance = axios.create(apiRequestConfig);
axiosInstance.interceptors.request.use(apiRequestInterceptor);
axiosInstance.interceptors.response.use(
apiSuccessResponseInterceptor,
apiFailureResponseInterceptor
);
axiosIns = axiosInstance;
return axiosIns;
}
class Api {
static get(url: string, queryParams?: any, config: Partial<AxiosRequestConfig> = {}) {
return getAxiosInstance().request({
url,
method: "GET",
params: queryParams,
...config,
});
}
static post(
url: string,
body?: any,
queryParams?: any,
config: Partial<AxiosRequestConfig> = {}
) {
return getAxiosInstance().request({
method: "POST",
url,
data: body,
params: queryParams,
...config,
});
}
static put(url: string, body?: any, queryParams?: any, config: Partial<AxiosRequestConfig> = {}) {
return getAxiosInstance().request({
method: "PUT",
url,
params: queryParams,
data: body,
...config,
});
}
static patch(
url: string,
body?: any,
queryParams?: any,
config: Partial<AxiosRequestConfig> = {}
) {
return getAxiosInstance().request({
method: "PATCH",
url,
data: body,
params: queryParams,
...config,
});
}
static delete(url: string, queryParams?: any, config: Partial<AxiosRequestConfig> = {}) {
return getAxiosInstance().request({
method: "DELETE",
url,
params: queryParams,
...config,
});
}
}
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "TRACE";
export default Api;