forked from bssm-portfolio/app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.ts
35 lines (29 loc) · 953 Bytes
/
api.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
import { Storage } from "@/models/storage";
import { getDateParsedData } from "@/utils/date";
import { AxiosRequestConfig, AxiosResponse } from "axios";
export const requestInterceptors = (requestConfig: AxiosRequestConfig) => {
const accessToken = Storage.getItem("ACCESS_TOKEN");
if (requestConfig.headers) {
(requestConfig.headers as any).Token = accessToken;
}
const urlParams = requestConfig.url?.split("/:") || [];
if (urlParams.length < 2) return requestConfig;
const paramParsedUrl = urlParams
?.map((paramKey) => {
return requestConfig.params[paramKey];
})
.join("/");
urlParams?.forEach((paramKey: string) => {
delete requestConfig.params[paramKey];
}, {});
return {
...requestConfig,
url: paramParsedUrl,
};
};
export const responseInterceptors = (originalResponse: AxiosResponse) => {
return {
...originalResponse,
data: getDateParsedData(originalResponse.data),
};
};