axios-serializer
Version:
A serializer for axios
95 lines (86 loc) • 3 kB
TypeScript
import type { AxiosInstance } from 'axios';
import type { AxiosResponse } from 'axios';
import type { CancelToken } from 'axios';
import type { CancelTokenSource } from 'axios';
import type { InternalAxiosRequestConfig } from 'axios';
/**
* AxiosSerializer class
*
* @return Promise
*/
declare class AxiosSerializer {
axiosInstance: AxiosInstance;
waiting: WaitingList;
unique: SerializerOptions['unique'];
orderly: SerializerOptions['orderly'];
onCancel: SerializerOptions['onCancel'] | null;
constructor({ unique, onCancel, ...defaultOptions }: SerializerConfig);
/**
* Initialization
*/
init(defaultOptions: SerializerConfig): void;
/**
* Create request
*/
create<T = any, R = AxiosResponse<T>, D = any>(config: SerializerRequestOptions<D>): Promise<R>;
/**
* Drop all un-need requests
*
* @param key - the key of waiting line, usually to be the request url
*/
clear(key?: string): void;
/**
* Waiting to resolve the item before this request
*
* @param key - the key of waiting line, usually to be the request url
* @param promiseKey - the unique promise key
* @returns - Promise<void>
*/
private wait;
/**
* set item to waiting list
*
* @param key - the key of waiting line, usually to be the request url
* @param item - waiting object
*/
private add;
}
export default AxiosSerializer;
export declare interface SerializerConfig<D = any> extends InternalAxiosRequestConfig<D> {
unique?: boolean;
orderly?: boolean;
setHeaders?(instance: AxiosInstance): void;
onRequest?(config: InternalAxiosRequestConfig, requestOptions: SerializerRequestOptions): InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
onRequestError?(error: any): void;
onResponse?(res: AxiosResponse<any>, requestOptions: SerializerRequestOptions): AxiosResponse<any> | Promise<AxiosResponse<any>>;
onResponseError?(error: any): void;
onError?(error: any): void;
onCancel?(error: any): void;
}
export declare interface SerializerCurrentStateType {
lastRequestTime: number;
retryCount: number;
}
export declare interface SerializerOptions extends Record<string, unknown> {
unique?: boolean;
orderly?: boolean;
onCancel?: (err: any) => void;
}
export declare interface SerializerRequestOptions<D = any> extends InternalAxiosRequestConfig<D> {
['axios-serializer']?: any;
unique?: boolean;
orderly?: boolean;
requestOptions?: SerializerRequestOptions;
cancelToken?: CancelToken;
type?: string;
error?: boolean;
}
export declare interface WaitingItem {
promiseKey: symbol;
url: string;
promise: Promise<any>;
source: CancelTokenSource;
abortController?: AbortController;
}
export declare type WaitingList = Record<string, WaitingItem[]>;
export { }