Class RequestTemplate<CC>Abstract
Properties
Protected
cache
cache
: Cache<Promise<unknown> | AxiosPromise<unknown>>
Protected
cancelerManager
Accessors
cancelCurrentRequest
- get cancelCurrentRequest(): undefined | Canceler
-
Returns undefined | Canceler
Methods
Protected
init
- init(): void
-
Returns void
Protected
generateRequestKey
- generateRequestKey(ctx: Omit<Context<CC>, "requestKey">): string
-
Parameters
-
ctx: Omit<Context<CC>, "requestKey">
Returns string
Protected
handleResponse
- handleResponse<T>(ctx: Context<CC>, response: AxiosResponse<any, any>): ResType<T>
-
Parameters
-
-
response: AxiosResponse<any, any>
Protected
registerCanceler
- registerCanceler(ctx: Context<CC>, canceler: Canceler): void
-
Returns void
Protected
Abstract
handleCanceler
- handleCanceler(ctx: Context<CC>): void
-
Returns void
Protected
Abstract
fetch
- fetch(ctx: RetryContext<CC>): Promise<unknown>
-
Returns Promise<unknown>
Protected
Abstract
isCancel
- isCancel(value: unknown, ctx: RetryContext<CC>): boolean
-
Returns boolean
Protected
handleRequestConfig
- handleRequestConfig(requestConfig: AxiosRequestConfig<any>): AxiosRequestConfig<any>
-
Parameters
-
requestConfig: AxiosRequestConfig<any>
Returns AxiosRequestConfig<any>
Protected
mergeCacheConfig
Protected
mergeRetryConfig
Protected
handleCustomConfig
- handleCustomConfig(customConfig: CC): CC
-
Returns CC
Protected
handleStatus
- handleStatus(ctx: Context<CC>, response: AxiosResponse<ResType<unknown>, any>, data: ResType<unknown>): ResType<unknown> | AxiosResponse<ResType<unknown>, any> | Promise<unknown>
-
Returns ResType<unknown> | AxiosResponse<ResType<unknown>, any> | Promise<unknown>
Protected
useCache
- useCache(ctx: Context<CC>, request: (() => Promise<unknown>)): Promise<unknown> | AxiosPromise<unknown>
-
Parameters
-
-
request: (() => Promise<unknown>)
-
- (): Promise<unknown>
-
Returns Promise<unknown>
Returns Promise<unknown> | AxiosPromise<unknown>
Protected
handleRetry
- handleRetry(ctx: Context<CC>): void
-
Returns void
Protected
beforeExecRequest
- beforeExecRequest(ctx: Context<CC>): void
-
Returns void
Protected
beforeRequest
- beforeRequest(ctx: Context<CC>): void
-
Returns void
Protected
afterRequest
- afterRequest(ctx: Context<CC>): void
-
Returns void
Protected
generateContext
- generateContext(customConfig: CC, requestConfig: AxiosRequestConfig<any>): Context<CC>
-
Parameters
-
customConfig: CC
-
requestConfig: AxiosRequestConfig<any>
Protected
execRequest
- execRequest(ctx: RetryContext<CC>): Promise<unknown>
-
Returns Promise<unknown>
request
- request<T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken" | "responseType"> & {
url: string;
responseType?: "json";
}, customConfig?: DynamicCustomConfig<CC, RC>): Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>
-
Type Parameters
-
T = never
-
RC extends boolean = false
Parameters
-
requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken" | "responseType"> & {
url: string;
responseType?: "json";
}
-
Returns Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>
- request<T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken"> & {
url: string;
}, customConfig?: DynamicCustomConfig<CC, RC>): Promise<RC extends true ? AxiosResponse<T, any> : T>
-
Type Parameters
-
T = never
-
RC extends boolean = false
Parameters
-
requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken"> & {
url: string;
}
-
Returns Promise<RC extends true ? AxiosResponse<T, any> : T>
Protected
handleError
- handleError(ctx: Context<CC>, e: AxiosError<ResType<unknown>, any>): ResType<unknown> | AxiosResponse<ResType<unknown>, any> | Promise<unknown>
-
Parameters
-
-
e: AxiosError<ResType<unknown>, any>
Returns ResType<unknown> | AxiosResponse<ResType<unknown>, any> | Promise<unknown>
cancelAll
- cancelAll(msg?: string): void
-
Returns void
cancelWithTag
- cancelWithTag(tag: undefined | Tag, msg?: string): void
-
Parameters
-
tag: undefined | Tag
-
Optional
msg: string
Returns void
methodFactory
- methodFactory(method: Method, handler?: ((configs: Configs<CustomConfig>) => void)): (<T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "method" | "cancelToken"> & {
url: string;
}, customConfig?: DynamicCustomConfig<CC, RC>) => Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>)
-
Returns (<T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "method" | "cancelToken"> & {
url: string;
}, customConfig?: DynamicCustomConfig<CC, RC>) => Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>)
-
- <T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "method" | "cancelToken"> & {
url: string;
}, customConfig?: DynamicCustomConfig<CC, RC>): Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>
-
Type Parameters
-
T = never
-
RC extends boolean = false
Parameters
-
requestConfig: Omit<AxiosRequestConfig<any>, "url" | "method" | "cancelToken"> & {
url: string;
}
-
Returns Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>
simplifyMethodFactory
- simplifyMethodFactory(method: Method, urlPrefix?: string): (<T, RC>(url: string, data?: {}, customConfig?: DynamicCustomConfig<CC, RC>) => Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>)
-
Parameters
-
method: Method
-
urlPrefix: string = ''
Returns (<T, RC>(url: string, data?: {}, customConfig?: DynamicCustomConfig<CC, RC>) => Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>)
-
- <T, RC>(url: string, data?: {}, customConfig?: DynamicCustomConfig<CC, RC>): Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>
-
Type Parameters
-
T = never
-
RC extends boolean = false
Parameters
-
url: string
-
data: {} = {}
-
Returns Promise<RC extends true ? AxiosResponse<ResType<T>, any> : ResType<T>>
use
- use(configs: Partial<Configs<CC>>): (<T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken"> & {
url: string;
}, customConfig?: DynamicCustomConfig<CC, RC>) => Promise<T>)
-
Returns (<T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken"> & {
url: string;
}, customConfig?: DynamicCustomConfig<CC, RC>) => Promise<T>)
-
- <T, RC>(requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken"> & {
url: string;
}, customConfig?: DynamicCustomConfig<CC, RC>): Promise<T>
-
Type Parameters
-
T = never
-
RC extends boolean = false
Parameters
-
requestConfig: Omit<AxiosRequestConfig<any>, "url" | "cancelToken"> & {
url: string;
}
-
Returns Promise<T>
deleteCacheByTag
- deleteCacheByTag(tag: Tag): void
-
Returns void
clearCache
- clearCache(): void
-
Returns void
请求封装基类
使用模板方法模式处理请求封装抽象类, 具体类可实现protected的方法替换掉原有方法,需要实现抽象方法
可参考
@request-template/axios
@request-template/wechat
See
Example