|
| 1 | +import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"; |
| 2 | +import * as R from "remeda"; |
| 3 | + |
| 4 | +import { getCookie } from "@pyconkr-common/utils/cookie"; |
| 5 | +import ShopAPISchema, { |
| 6 | + isObjectErrorResponseSchema, |
| 7 | +} from "@pyconkr-shop/schemas"; |
| 8 | + |
| 9 | +const DEFAULT_TIMEOUT = 10000; |
| 10 | +const DEFAULT_ERROR_MESSAGE = |
| 11 | + "알 수 없는 문제가 발생했습니다, 잠시 후 다시 시도해주세요."; |
| 12 | +const DEFAULT_ERROR_RESPONSE = { |
| 13 | + type: "unknown", |
| 14 | + errors: [{ code: "unknown", detail: DEFAULT_ERROR_MESSAGE, attr: null }], |
| 15 | +}; |
| 16 | + |
| 17 | +export class ShopAPIClientError extends Error { |
| 18 | + readonly name = "ShopAPIError"; |
| 19 | + readonly status: number; |
| 20 | + readonly detail: ShopAPISchema.ErrorResponseSchema; |
| 21 | + readonly originalError: unknown; |
| 22 | + |
| 23 | + constructor(error?: unknown) { |
| 24 | + let message: string = DEFAULT_ERROR_MESSAGE; |
| 25 | + let detail: ShopAPISchema.ErrorResponseSchema = DEFAULT_ERROR_RESPONSE; |
| 26 | + let status = -1; |
| 27 | + |
| 28 | + if (axios.isAxiosError(error)) { |
| 29 | + const response = error.response; |
| 30 | + |
| 31 | + if (response) { |
| 32 | + status = response.status; |
| 33 | + detail = isObjectErrorResponseSchema(response.data) |
| 34 | + ? response.data |
| 35 | + : { |
| 36 | + type: "axios_error", |
| 37 | + errors: [ |
| 38 | + { |
| 39 | + code: "unknown", |
| 40 | + detail: R.isString(response.data) |
| 41 | + ? response.data |
| 42 | + : DEFAULT_ERROR_MESSAGE, |
| 43 | + attr: null, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }; |
| 47 | + } |
| 48 | + } else if (error instanceof Error) { |
| 49 | + message = error.message; |
| 50 | + detail = { |
| 51 | + type: error.name || typeof error || "unknown", |
| 52 | + errors: [{ code: "unknown", detail: error.message, attr: null }], |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + super(message); |
| 57 | + this.originalError = error || null; |
| 58 | + this.status = status; |
| 59 | + this.detail = detail; |
| 60 | + } |
| 61 | + |
| 62 | + isRequiredAuth(): boolean { |
| 63 | + return this.status === 401 || this.status === 403; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +type AxiosRequestWithoutPayload = <T = any, R = AxiosResponse<T>, D = any>( |
| 68 | + url: string, |
| 69 | + config?: AxiosRequestConfig<D> |
| 70 | +) => Promise<R>; |
| 71 | +type AxiosRequestWithPayload = <T = any, R = AxiosResponse<T>, D = any>( |
| 72 | + url: string, |
| 73 | + data?: D, |
| 74 | + config?: AxiosRequestConfig<D> |
| 75 | +) => Promise<R>; |
| 76 | + |
| 77 | +class ShopAPIClient { |
| 78 | + readonly baseURL: string; |
| 79 | + protected readonly csrfCookieName: string; |
| 80 | + private readonly shopAPI: AxiosInstance; |
| 81 | + |
| 82 | + constructor( |
| 83 | + baseURL: string = import.meta.env.VITE_PYCONKR_SHOP_API_DOMAIN, |
| 84 | + csrfCookieName: string = import.meta.env.VITE_PYCONKR_SHOP_CSRF_COOKIE_NAME, |
| 85 | + timeout: number = DEFAULT_TIMEOUT |
| 86 | + ) { |
| 87 | + this.baseURL = baseURL; |
| 88 | + this.csrfCookieName = csrfCookieName; |
| 89 | + this.shopAPI = axios.create({ |
| 90 | + baseURL, |
| 91 | + timeout, |
| 92 | + withCredentials: true, |
| 93 | + headers: { "Content-Type": "application/json" }, |
| 94 | + }); |
| 95 | + this.shopAPI.interceptors.request.use( |
| 96 | + (config) => { |
| 97 | + config.headers["x-csrftoken"] = this.getCSRFToken(); |
| 98 | + return config; |
| 99 | + }, |
| 100 | + (error) => Promise.reject(error) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + _safe_request_without_payload( |
| 105 | + requestFunc: AxiosRequestWithoutPayload |
| 106 | + ): AxiosRequestWithoutPayload { |
| 107 | + return async <T = any, R = AxiosResponse<T>, D = any>( |
| 108 | + url: string, |
| 109 | + config?: AxiosRequestConfig<D> |
| 110 | + ) => { |
| 111 | + try { |
| 112 | + return await requestFunc<T, R, D>(url, config); |
| 113 | + } catch (error) { |
| 114 | + throw new ShopAPIClientError(error); |
| 115 | + } |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + _safe_request_with_payload( |
| 120 | + requestFunc: AxiosRequestWithPayload |
| 121 | + ): AxiosRequestWithPayload { |
| 122 | + return async <T = any, R = AxiosResponse<T>, D = any>( |
| 123 | + url: string, |
| 124 | + data: D, |
| 125 | + config?: AxiosRequestConfig<D> |
| 126 | + ) => { |
| 127 | + try { |
| 128 | + return await requestFunc<T, R, D>(url, data, config); |
| 129 | + } catch (error) { |
| 130 | + throw new ShopAPIClientError(error); |
| 131 | + } |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + getCSRFToken(): string | undefined { |
| 136 | + return getCookie(this.csrfCookieName); |
| 137 | + } |
| 138 | + |
| 139 | + async get<T, D = any>( |
| 140 | + url: string, |
| 141 | + config?: AxiosRequestConfig<D> |
| 142 | + ): Promise<T> { |
| 143 | + return ( |
| 144 | + await this._safe_request_without_payload(this.shopAPI.get)< |
| 145 | + T, |
| 146 | + AxiosResponse<T>, |
| 147 | + D |
| 148 | + >(url, config) |
| 149 | + ).data; |
| 150 | + } |
| 151 | + async post<T, D>( |
| 152 | + url: string, |
| 153 | + data: D, |
| 154 | + config?: AxiosRequestConfig<D> |
| 155 | + ): Promise<T> { |
| 156 | + return ( |
| 157 | + await this._safe_request_with_payload(this.shopAPI.post)< |
| 158 | + T, |
| 159 | + AxiosResponse<T>, |
| 160 | + D |
| 161 | + >(url, data, config) |
| 162 | + ).data; |
| 163 | + } |
| 164 | + async put<T, D>( |
| 165 | + url: string, |
| 166 | + data: D, |
| 167 | + config?: AxiosRequestConfig<D> |
| 168 | + ): Promise<T> { |
| 169 | + return ( |
| 170 | + await this._safe_request_with_payload(this.shopAPI.put)< |
| 171 | + T, |
| 172 | + AxiosResponse<T>, |
| 173 | + D |
| 174 | + >(url, data, config) |
| 175 | + ).data; |
| 176 | + } |
| 177 | + async patch<T, D>( |
| 178 | + url: string, |
| 179 | + data: D, |
| 180 | + config?: AxiosRequestConfig<D> |
| 181 | + ): Promise<T> { |
| 182 | + return ( |
| 183 | + await this._safe_request_with_payload(this.shopAPI.patch)< |
| 184 | + T, |
| 185 | + AxiosResponse<T>, |
| 186 | + D |
| 187 | + >(url, data, config) |
| 188 | + ).data; |
| 189 | + } |
| 190 | + async delete<T, D = any>( |
| 191 | + url: string, |
| 192 | + config?: AxiosRequestConfig<D> |
| 193 | + ): Promise<T> { |
| 194 | + return ( |
| 195 | + await this._safe_request_without_payload(this.shopAPI.delete)< |
| 196 | + T, |
| 197 | + AxiosResponse<T>, |
| 198 | + D |
| 199 | + >(url, config) |
| 200 | + ).data; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +export const shopAPIClient = new ShopAPIClient( |
| 205 | + import.meta.env.VITE_PYCONKR_SHOP_API_DOMAIN |
| 206 | +); |
0 commit comments