import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'; export type ContentType = | 'text/html' | 'text/plain' | 'multipart/form-data' | 'application/json' | 'application/x-www-form-urlencoded' | 'application/octet-stream'; export type ResponseTransform = (input: Input) => Output | Promise; export interface RequestOption< ResponseData, ApiData = ResponseData, State extends Record = Record > { /** * The default state */ defaultState?: State; /** * transform the response data to the api data * * @param response Axios response */ transform: ResponseTransform, ApiData>; /** * transform the response data to the api data * * @deprecated use `transform` instead, will be removed in the next major version v3 * @param response Axios response */ transformBackendResponse: ResponseTransform, ApiData>; /** * The hook before request * * For example: You can add header token in this hook * * @param config Axios config */ onRequest: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise; /** * The hook to check backend response is success or not * * @param response Axios response */ isBackendSuccess: (response: AxiosResponse) => boolean; /** * The hook after backend request fail * * For example: You can handle the expired token in this hook * * @param response Axios response * @param instance Axios instance */ onBackendFail: ( response: AxiosResponse, instance: AxiosInstance ) => Promise | Promise; /** * The hook to handle error * * For example: You can show error message in this hook * * @param error */ onError: (error: AxiosError) => void | Promise; } interface ResponseMap { blob: Blob; text: string; arrayBuffer: ArrayBuffer; stream: ReadableStream; document: Document; } export type ResponseType = keyof ResponseMap | 'json'; export type MappedType = R extends keyof ResponseMap ? ResponseMap[R] : JsonType; export type CustomAxiosRequestConfig = Omit & { responseType?: R; }; export interface RequestInstanceCommon> { /** * cancel all request * * if the request provide abort controller sign from config, it will not collect in the abort controller map */ cancelAllRequest: () => void; /** you can set custom state in the request instance */ state: State; } /** The request instance */ export interface RequestInstance> extends RequestInstanceCommon { ( config: CustomAxiosRequestConfig ): Promise>; } export type FlatResponseSuccessData = { data: ApiData; error: null; response: AxiosResponse; }; export type FlatResponseFailData = { data: null; error: AxiosError; response: AxiosResponse; }; export type FlatResponseData = | FlatResponseSuccessData | FlatResponseFailData; export interface FlatRequestInstance> extends RequestInstanceCommon { ( config: CustomAxiosRequestConfig ): Promise>>; }