@bit-ocean/axios
Version:
Universal Axios instance.
1 lines • 23.2 kB
Source Map (JSON)
{"version":3,"sources":["../src/error-message.map.ts","../src/http-client.ts"],"names":["errorMessageMap","HttpStatusCode","axios","_instance","_instanceOptions","_interceptorsLoaded","_isTokenRefreshing","_pendingQueue","_HttpClient_instances","initRequestInterceptor_fn","initResponseInterceptor_fn","addLanguage_fn","addAuthorization_fn","handleMessage_fn","pendingRequest_fn","handleStatusCode_fn","handleUnauthorized_fn","refresh_fn","HttpClient","options","config","__privateAdd","__privateSet","__async","__privateGet","router","message","i18n","__privateMethod","url","params","__spreadValues","data","req","err","res","_a","msg","response","newResponse","LangUtils","AuthUtils","messageType","m","resolve","_b","status","errorMsg","e","unauthorizedRedirectUrl","currentPath","token","accessToken","refreshToken","__spreadProps","task"],"mappings":"wkCAEaA,IAAAA,CAAAA,CAAkB,IAAI,GAAI,CAAA,CACrC,CAACC,cAAAA,CAAe,YAAc,CAAA,oBAAoB,EAClD,CAACA,cAAAA,CAAe,SAAW,CAAA,iBAAiB,CAC5C,CAAA,CAACA,eAAe,QAAU,CAAA,gBAAgB,CAC1C,CAAA,CAACA,cAAe,CAAA,gBAAA,CAAkB,0BAA0B,CAC5D,CAAA,CAACA,cAAe,CAAA,QAAA,CAAU,gBAAgB,CAAA,CAC1C,CAACA,cAAe,CAAA,mBAAA,CAAqB,4BAA4B,CAAA,CACjE,CAACA,cAAAA,CAAe,gBAAiB,yBAAyB,CAAA,CAC1D,CAACA,cAAAA,CAAe,mBAAqB,CAAA,6BAA6B,CAClE,CAAA,CAACA,cAAe,CAAA,UAAA,CAAY,mBAAmB,CAAA,CAC/C,CAACA,cAAAA,CAAe,WAAY,uBAAuB,CACrD,CAAC,ECQDC,CAAAA,CAAM,QAAS,CAAA,OAAA,CAAU,GACzBA,CAAAA,CAAAA,CAAM,SAAS,OAAQ,CAAA,MAAA,CAAO,cAAc,CAAA,CAAI,gCAtBhD,CAAA,IAAAC,EAAAC,CAAAC,CAAAA,CAAAA,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ode } from 'axios'\n\nexport const errorMessageMap = new Map([\n [HttpStatusCode.Unauthorized, '401: Unauthorized!'],\n [HttpStatusCode.Forbidden, '403: Forbidden!'],\n [HttpStatusCode.NotFound, '404: NotFound!'],\n [HttpStatusCode.MethodNotAllowed, '405: Method Not Allowed!'],\n [HttpStatusCode.Conflict, '409: Conflict!'],\n [HttpStatusCode.UnprocessableEntity, '422: Unprocessable Entity!'],\n [HttpStatusCode.TooManyRequests, '429: Too Many Requests!'],\n [HttpStatusCode.InternalServerError, '500: Internal Server Error!'],\n [HttpStatusCode.BadRequest, '502: Bad Gateway!'],\n [HttpStatusCode.BadGateway, '504: Gateway Timeout!']\n])\n","import { AuthUtils, LangUtils } from '@bit-ocean/utils'\nimport type { NoticeType } from 'antd/es/message/interface'\nimport type {\n AxiosError,\n AxiosInstance,\n AxiosRequestConfig,\n AxiosResponse,\n CreateAxiosDefaults,\n InternalAxiosRequestConfig\n} from 'axios'\nimport axios, { HttpStatusCode } from 'axios'\n\nimport { errorMessageMap } from './error-message.map'\nimport type {\n AxiosInstanceOptions,\n InterceptorInitOptions,\n PendingRequest,\n R,\n Tokens\n} from './types'\n\naxios.defaults.timeout = 30_000\naxios.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8'\n\nexport class HttpClient {\n /**\n * The instance of Axios.\n */\n #instance: AxiosInstance\n\n /**\n * The extra custom options for the instance.\n */\n #instanceOptions: AxiosInstanceOptions = {}\n\n /**\n * The flag to indicate whether the interceptors are loaded.\n */\n #interceptorsLoaded = false\n\n /**\n * The flag to indicate whether the token is refreshing.\n * @description If the token is refreshing, the request will be added to the pending list.\n */\n #isTokenRefreshing = false\n\n /**\n * The pending requests.\n */\n #pendingQueue: PendingRequest[] = []\n\n constructor(options?: AxiosInstanceOptions, config?: CreateAxiosDefaults) {\n this.#instance = axios.create(config)\n this.#instanceOptions = options ?? {}\n }\n\n /**\n * Initialize the interceptors.\n */\n async initInterceptors(options?: InterceptorInitOptions) {\n // NOTE: Prevent repeating the initialization.\n if (this.#interceptorsLoaded) {\n return\n }\n this.#interceptorsLoaded = true\n\n const { router, message, i18n } = options ?? {}\n\n this.#initRequestInterceptor()\n this.#initResponseInterceptor({ router, message })\n }\n\n /**\n * Initialize the request interceptor.\n */\n #initRequestInterceptor() {\n this.#instance.interceptors.request.use(\n (req: InternalAxiosRequestConfig) => {\n this.#addLanguage(req)\n this.#addAuthorization(req)\n return req\n },\n (err: AxiosError) => Promise.reject(err)\n )\n }\n\n /**\n * Initialize the response interceptor.\n */\n #initResponseInterceptor(options: InterceptorInitOptions) {\n const { message } = options\n this.#instance.interceptors.response.use(\n (res: AxiosResponse<R>) => {\n if (res.config?.rawResponse) {\n return res\n }\n const { data, msg } = res.data\n\n this.#handleMessage(msg, message)\n\n return data\n },\n async (err: AxiosError<R>) => {\n const { response, config } = err\n\n // If the request is canceled, throw the error.\n if (axios.isCancel(err)) {\n throw err\n }\n\n // Process the refresh token API error.\n if (config?.isRefreshRequest) {\n this.#isTokenRefreshing = false\n throw response?.data.data\n }\n\n // If the token is refreshing, add the request to the pending queue.\n if (this.#isTokenRefreshing) {\n return this.#pendingRequest(config)\n }\n\n // Handle by status code.\n const newResponse = await this.#handleStatusCode(response, options)\n if (newResponse) {\n return newResponse\n }\n\n throw response?.data\n }\n )\n }\n\n /**\n * Add the current language to the request header.\n */\n #addLanguage(req: InternalAxiosRequestConfig) {\n req.headers['Accept-Language'] = LangUtils.getDefaultLang()\n }\n\n /**\n * Add the authorization to the request header.\n */\n #addAuthorization(req: InternalAxiosRequestConfig) {\n if (AuthUtils.isAuthenticated()) {\n req.headers.Authorization = AuthUtils.getAuthorization()\n }\n }\n\n /**\n * Handle the message.\n * @description\n * - If the message is an array, show each message.\n * - If the message is a string, show the message.\n * - Default message type is `success`.\n */\n #handleMessage(\n msg?: string | string[],\n message?: InterceptorInitOptions['message'],\n messageType: NoticeType = 'success'\n ) {\n if (!message || !msg) {\n return\n }\n if (Array.isArray(msg)) {\n msg.forEach((m) => message[messageType](m))\n } else {\n message[messageType](msg)\n }\n }\n\n /**\n * Add the request to the pending queue.\n */\n #pendingRequest(config?: AxiosRequestConfig) {\n return new Promise((resolve) => {\n this.#pendingQueue.push({ config, resolve })\n })\n }\n\n /**\n * Error handling by status code.\n * @description\n * - If the status code is `401`, handle the unauthorized logic.\n * - If the status code is `403`, redirect to the `403` page.\n * - If the status code is `500`, redirect to the `500` page.\n * - If the status code is not in the above, do nothing.\n */\n async #handleStatusCode(res?: AxiosResponse<R>, options?: InterceptorInitOptions) {\n const { status = 0, data, config } = res ?? {}\n const { message, router } = options ?? {}\n\n /**\n * 1. Use the `msg` from the response data.\n * 2. Check the `errorMessageMap` by status code.\n * 3. Use the default message `Unknown error!`.\n */\n const errorMsg = data?.msg ?? errorMessageMap.get(status) ?? 'Unknown error!'\n\n switch (status) {\n case HttpStatusCode.Unauthorized: {\n try {\n /**\n * If the refresh token logic is enabled, try to refresh the token.\n */\n if (this.#instanceOptions.enableRefreshToken) {\n const newResponse = await this.#refresh(config)\n if (newResponse) {\n return newResponse\n }\n }\n } catch {\n //\n }\n this.#handleMessage(errorMsg, message, 'error')\n this.#handleUnauthorized(router)\n break\n }\n case HttpStatusCode.Forbidden: {\n router?.navigate({ to: '/403', replace: true })\n this.#handleMessage(errorMsg, message, 'error')\n break\n }\n case HttpStatusCode.InternalServerError:\n case HttpStatusCode.BadGateway:\n case HttpStatusCode.GatewayTimeout: {\n router?.navigate({ to: '/500', replace: true })\n this.#handleMessage(errorMsg, message, 'error')\n break\n }\n default: {\n this.#handleMessage(errorMsg, message, 'error')\n break\n }\n }\n return false\n }\n\n /**\n * Handle the unauthorized logic.\n * @description\n * - Clear the access token and refresh token.\n * - Redirect to the public page (`/login` by default) with query `?redirect=${currentPath}`.\n */\n #handleUnauthorized(router: InterceptorInitOptions['router']) {\n const { unauthorizedRedirectUrl = '/login' } = this.#instanceOptions\n\n AuthUtils.clearAccessToken()\n AuthUtils.clearRefreshToken()\n\n const currentPath = router?.state.location.pathname\n\n if (currentPath !== unauthorizedRedirectUrl) {\n router?.navigate({\n to: unauthorizedRedirectUrl,\n replace: true,\n search: currentPath === unauthorizedRedirectUrl ? undefined : { redirect: currentPath }\n })\n }\n }\n\n /**\n * Refresh the token.\n * @description\n * - Get the refresh token from the local storage.\n * - Send a request to the refresh token API.\n * - Set the new access token and refresh token.\n * - Retry the current request and pending requests after get new tokens.\n */\n async #refresh(config?: AxiosRequestConfig) {\n this.#isTokenRefreshing = true\n\n const token = AuthUtils.getRefreshToken()\n\n const { accessToken, refreshToken } = await this.POST<Tokens>(\n '/auth/refresh',\n {\n token\n },\n {\n isRefreshRequest: true\n }\n )\n\n AuthUtils.setAccessToken(accessToken)\n AuthUtils.setRefreshToken(refreshToken)\n\n this.#isTokenRefreshing = false\n\n // Retry the current request after get new tokens.\n if (config) {\n const newResponse = await this.request({\n ...config,\n headers: {\n ...config.headers,\n Authorization: AuthUtils.getAuthorization()\n }\n })\n\n if (this.#pendingQueue.length > 0) {\n this.#pendingQueue.forEach((task) => task.resolve(this.request(task.config!)))\n this.#pendingQueue = []\n }\n return newResponse\n }\n return false\n }\n\n /**\n * The method to send a request.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.request({ url: '/api/users', method: 'GET' })\n * ```\n */\n request<T>(config: AxiosRequestConfig): Promise<T> {\n return this.#instance.request(config)\n }\n\n /**\n * The method to send a `GET` request.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.GET('/api/users')\n * ```\n */\n get<T, D = any>(\n url: string,\n params?: Record<string, any>,\n config?: AxiosRequestConfig<D>\n ): Promise<T> {\n return this.#instance.get(url, { params, ...config })\n }\n\n /**\n * The method to send a `POST` request.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.POST('/api/users', { id: 1, name: 'Bruce' })\n * ```\n */\n post<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.post(url, data, config)\n }\n\n /**\n * The method to send a `PUT` request.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.PUT('/api/users/1', { name: 'Bruce' })\n * ```\n */\n put<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.put(url, data, config)\n }\n\n /**\n * The method to send a `DELETE` request.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.DELETE('/api/users/1')\n * ```\n */\n delete<T, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.delete(url, config)\n }\n\n /**\n * The method to send a `PATCH` request.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.PATCH('/api/users/1', { name: 'Bruce' })\n * ```\n */\n patch<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.patch(url, data, config)\n }\n\n /**\n * The method to send a `GET` request.\n * @description In order to highlight the method of request, also support the upper case methods.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.GET('/api/users')\n * ```\n */\n GET<T, D = any>(\n url: string,\n params?: Record<string, any>,\n config?: AxiosRequestConfig<D>\n ): Promise<T> {\n return this.#instance.get(url, { params, ...config })\n }\n\n /**\n * The method to send a `POST` request.\n * @description In order to highlight the method of request, also support the upper case methods.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.POST('/api/users', { id: 1, name: 'Bruce' })\n * ```\n */\n POST<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.post(url, data, config)\n }\n\n /**\n * The method to send a `PUT` request.\n * @description In order to highlight the method of request, also support the upper case methods.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.PUT('/api/users/1', { name: 'Bruce' })\n * ```\n */\n PUT<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.put(url, data, config)\n }\n\n /**\n * The method to send a `DELETE` request.\n * @description In order to highlight the method of request, also support the upper case methods.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.DELETE('/api/users/1')\n * ```\n */\n DELETE<T, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.delete(url, config)\n }\n\n /**\n * The method to send a `PATCH` request.\n * @description In order to highlight the method of request, also support the upper case methods.\n * @example\n * ```ts\n * const httpClient = new HttpClient()\n * httpClient.PATCH('/api/users/1', { name: 'Bruce' })\n * ```\n */\n PATCH<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T> {\n return this.#instance.patch(url, data, config)\n }\n}\n"]}