-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublicDataClient.ts
More file actions
45 lines (37 loc) · 1.09 KB
/
Copy pathpublicDataClient.ts
File metadata and controls
45 lines (37 loc) · 1.09 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
import axios, { type AxiosResponse } from 'axios';
export type PublicDataResponse<T> = {
response: {
header: {
resultCode: string;
resultMsg: string;
};
body: T;
};
};
const PUBLIC_DATA_SUCCESS_CODE = '00';
const PUBLIC_DATA_REQUEST_TIMEOUT = 8000;
export const publicDataClient = axios.create({
baseURL: import.meta.env.VITE_DATA_GO_KR_BASE_URL,
timeout: PUBLIC_DATA_REQUEST_TIMEOUT,
params: {
serviceKey: import.meta.env.VITE_DATA_GO_KR_API_KEY,
dataType: 'JSON',
},
});
publicDataClient.interceptors.response.use(
(response: AxiosResponse<PublicDataResponse<unknown>>) => {
const apiResponse = response.data?.response;
if (!apiResponse) {
throw new Error('응답 구조가 올바르지 않습니다.');
}
const { header } = apiResponse;
if (header.resultCode !== PUBLIC_DATA_SUCCESS_CODE) {
throw new Error(header.resultMsg ?? '알 수 없는 오류입니다.');
}
return response;
},
(error) => {
if (error instanceof Error) throw error;
throw new Error('네트워크 요청에 실패했습니다.');
},
);