-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
73 lines (67 loc) · 1.94 KB
/
index.ts
File metadata and controls
73 lines (67 loc) · 1.94 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
import axios, {
AxiosInstance,
AxiosInterceptorManager,
AxiosRequestConfig,
AxiosResponse,
InternalAxiosRequestConfig,
} from 'axios';
import { NEW_JWT_KEY } from '../../config/constant';
// 기존 서버 응답 타입
export type BaseResponse<T = any> = {
isSuccess: boolean;
message: string;
result: T;
};
export type PagingResponseType = {
currentPage: number;
totalPages: number;
totalItems: number;
};
interface CustomInstance extends AxiosInstance {
interceptors: {
request: AxiosInterceptorManager<InternalAxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
getUri(config?: AxiosRequestConfig): string;
request<T>(config: AxiosRequestConfig): Promise<T>;
get<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
delete<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
head<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
options<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
post<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
put<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
patch<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
}
// 새로운 서버 axios 인스턴스
export const newRequest: CustomInstance = axios.create({
baseURL: import.meta.env.VITE_NEW_API_URL,
timeout: 20000,
headers: {
accept: 'application/json',
Authorization: `Bearer ${NEW_JWT_KEY}`,
},
});
newRequest.interceptors.request.use(
(config) => {
const jwt = window.localStorage.getItem(NEW_JWT_KEY);
config.headers.Authorization = `Bearer ${jwt}`;
return config;
},
(error) => {
return Promise.reject(error);
},
);
newRequest.interceptors.response.use(
(response) => {
// 2XX 범위
// response body 반환
console.log('network log', response);
return response.data;
},
(error) => {
// 그 외
// error로 AxiosError 타입 반환
console.error(error);
return Promise.reject(error);
},
);