UNPKG

@bit-ocean/axios

Version:
165 lines (160 loc) 5 kB
import { HttpStatusCode, CreateAxiosDefaults, AxiosRequestConfig } from 'axios'; import { MessageInstance } from 'antd/es/message/interface'; import { i18n } from 'i18next'; declare const errorMessageMap: Map<HttpStatusCode, string>; interface AxiosInstanceOptions { /** * If set to `true`, the refresh token logic will be enabled. */ enableRefreshToken?: boolean; /** * When unauthorized, redirect to this url. * @default '/login' */ unauthorizedRedirectUrl?: string; } interface InterceptorInitOptions { /** * Tanstack Router instance. */ router?: any; /** * i18n instance. */ i18n?: i18n; /** * antd Message instance. */ message?: MessageInstance; } interface R<T = any> { msg: string; data: T; } interface Tokens { accessToken: string; refreshToken: string; } interface Page<T = unknown> { page: number; pageSize: number; total: number; records: T[]; } interface PageDto { page: number; pageSize: number; } declare class HttpClient { #private; constructor(options?: AxiosInstanceOptions, config?: CreateAxiosDefaults); /** * Initialize the interceptors. */ initInterceptors(options?: InterceptorInitOptions): Promise<void>; /** * The method to send a request. * @example * ```ts * const httpClient = new HttpClient() * httpClient.request({ url: '/api/users', method: 'GET' }) * ``` */ request<T>(config: AxiosRequestConfig): Promise<T>; /** * The method to send a `GET` request. * @example * ```ts * const httpClient = new HttpClient() * httpClient.GET('/api/users') * ``` */ get<T, D = any>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `POST` request. * @example * ```ts * const httpClient = new HttpClient() * httpClient.POST('/api/users', { id: 1, name: 'Bruce' }) * ``` */ post<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `PUT` request. * @example * ```ts * const httpClient = new HttpClient() * httpClient.PUT('/api/users/1', { name: 'Bruce' }) * ``` */ put<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `DELETE` request. * @example * ```ts * const httpClient = new HttpClient() * httpClient.DELETE('/api/users/1') * ``` */ delete<T, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `PATCH` request. * @example * ```ts * const httpClient = new HttpClient() * httpClient.PATCH('/api/users/1', { name: 'Bruce' }) * ``` */ patch<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `GET` request. * @description In order to highlight the method of request, also support the upper case methods. * @example * ```ts * const httpClient = new HttpClient() * httpClient.GET('/api/users') * ``` */ GET<T, D = any>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `POST` request. * @description In order to highlight the method of request, also support the upper case methods. * @example * ```ts * const httpClient = new HttpClient() * httpClient.POST('/api/users', { id: 1, name: 'Bruce' }) * ``` */ POST<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `PUT` request. * @description In order to highlight the method of request, also support the upper case methods. * @example * ```ts * const httpClient = new HttpClient() * httpClient.PUT('/api/users/1', { name: 'Bruce' }) * ``` */ PUT<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `DELETE` request. * @description In order to highlight the method of request, also support the upper case methods. * @example * ```ts * const httpClient = new HttpClient() * httpClient.DELETE('/api/users/1') * ``` */ DELETE<T, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<T>; /** * The method to send a `PATCH` request. * @description In order to highlight the method of request, also support the upper case methods. * @example * ```ts * const httpClient = new HttpClient() * httpClient.PATCH('/api/users/1', { name: 'Bruce' }) * ``` */ PATCH<T, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<T>; } export { HttpClient, type Page, type PageDto, type R, type Tokens, errorMessageMap };