-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
206 lines (190 loc) · 5.19 KB
/
client.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import * as R from "remeda";
import { getCookie } from "@pyconkr-common/utils/cookie";
import ShopAPISchema, {
isObjectErrorResponseSchema,
} from "@pyconkr-shop/schemas";
const DEFAULT_TIMEOUT = 10000;
const DEFAULT_ERROR_MESSAGE =
"알 수 없는 문제가 발생했습니다, 잠시 후 다시 시도해주세요.";
const DEFAULT_ERROR_RESPONSE = {
type: "unknown",
errors: [{ code: "unknown", detail: DEFAULT_ERROR_MESSAGE, attr: null }],
};
export class ShopAPIClientError extends Error {
readonly name = "ShopAPIError";
readonly status: number;
readonly detail: ShopAPISchema.ErrorResponseSchema;
readonly originalError: unknown;
constructor(error?: unknown) {
let message: string = DEFAULT_ERROR_MESSAGE;
let detail: ShopAPISchema.ErrorResponseSchema = DEFAULT_ERROR_RESPONSE;
let status = -1;
if (axios.isAxiosError(error)) {
const response = error.response;
if (response) {
status = response.status;
detail = isObjectErrorResponseSchema(response.data)
? response.data
: {
type: "axios_error",
errors: [
{
code: "unknown",
detail: R.isString(response.data)
? response.data
: DEFAULT_ERROR_MESSAGE,
attr: null,
},
],
};
}
} else if (error instanceof Error) {
message = error.message;
detail = {
type: error.name || typeof error || "unknown",
errors: [{ code: "unknown", detail: error.message, attr: null }],
};
}
super(message);
this.originalError = error || null;
this.status = status;
this.detail = detail;
}
isRequiredAuth(): boolean {
return this.status === 401 || this.status === 403;
}
}
type AxiosRequestWithoutPayload = <T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: AxiosRequestConfig<D>
) => Promise<R>;
type AxiosRequestWithPayload = <T = any, R = AxiosResponse<T>, D = any>(
url: string,
data?: D,
config?: AxiosRequestConfig<D>
) => Promise<R>;
class ShopAPIClient {
readonly baseURL: string;
protected readonly csrfCookieName: string;
private readonly shopAPI: AxiosInstance;
constructor(
baseURL: string = import.meta.env.VITE_PYCONKR_SHOP_API_DOMAIN,
csrfCookieName: string = import.meta.env.VITE_PYCONKR_SHOP_CSRF_COOKIE_NAME,
timeout: number = DEFAULT_TIMEOUT
) {
this.baseURL = baseURL;
this.csrfCookieName = csrfCookieName;
this.shopAPI = axios.create({
baseURL,
timeout,
withCredentials: true,
headers: { "Content-Type": "application/json" },
});
this.shopAPI.interceptors.request.use(
(config) => {
config.headers["x-csrftoken"] = this.getCSRFToken();
return config;
},
(error) => Promise.reject(error)
);
}
_safe_request_without_payload(
requestFunc: AxiosRequestWithoutPayload
): AxiosRequestWithoutPayload {
return async <T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: AxiosRequestConfig<D>
) => {
try {
return await requestFunc<T, R, D>(url, config);
} catch (error) {
throw new ShopAPIClientError(error);
}
};
}
_safe_request_with_payload(
requestFunc: AxiosRequestWithPayload
): AxiosRequestWithPayload {
return async <T = any, R = AxiosResponse<T>, D = any>(
url: string,
data: D,
config?: AxiosRequestConfig<D>
) => {
try {
return await requestFunc<T, R, D>(url, data, config);
} catch (error) {
throw new ShopAPIClientError(error);
}
};
}
getCSRFToken(): string | undefined {
return getCookie(this.csrfCookieName);
}
async get<T, D = any>(
url: string,
config?: AxiosRequestConfig<D>
): Promise<T> {
return (
await this._safe_request_without_payload(this.shopAPI.get)<
T,
AxiosResponse<T>,
D
>(url, config)
).data;
}
async post<T, D>(
url: string,
data: D,
config?: AxiosRequestConfig<D>
): Promise<T> {
return (
await this._safe_request_with_payload(this.shopAPI.post)<
T,
AxiosResponse<T>,
D
>(url, data, config)
).data;
}
async put<T, D>(
url: string,
data: D,
config?: AxiosRequestConfig<D>
): Promise<T> {
return (
await this._safe_request_with_payload(this.shopAPI.put)<
T,
AxiosResponse<T>,
D
>(url, data, config)
).data;
}
async patch<T, D>(
url: string,
data: D,
config?: AxiosRequestConfig<D>
): Promise<T> {
return (
await this._safe_request_with_payload(this.shopAPI.patch)<
T,
AxiosResponse<T>,
D
>(url, data, config)
).data;
}
async delete<T, D = any>(
url: string,
config?: AxiosRequestConfig<D>
): Promise<T> {
return (
await this._safe_request_without_payload(this.shopAPI.delete)<
T,
AxiosResponse<T>,
D
>(url, config)
).data;
}
}
export const shopAPIClient = new ShopAPIClient(
import.meta.env.VITE_PYCONKR_SHOP_API_DOMAIN
);