axios-miniprogram/src/core/Axios.ts

584 lines
11 KiB
TypeScript
Raw Normal View History

2023-04-26 21:03:00 +08:00
import {
2023-05-05 10:59:37 +08:00
PLAIN_METHODS,
WITH_DATA_METHODS,
WITH_PARAMS_METHODS,
} from '../constants/methods';
2023-05-07 23:16:28 +08:00
import { isString } from '../helpers/types';
2023-05-05 10:59:37 +08:00
import { dispatchRequest } from '../request/dispatchRequest';
import { CancelToken } from '../request/cancel';
import { AxiosTransformer } from '../request/transformData';
import { deepMerge } from '../helpers/deepMerge';
2023-03-23 20:09:00 +08:00
import {
2023-05-05 10:59:37 +08:00
AxiosAdapter,
AxiosAdapterRequestMethod,
AxiosAdapterPlatformTask,
AxiosAdapterRequestConfig,
AxiosAdapterResponseData,
} from '../adpater/createAdapter';
import InterceptorManager, {
2023-05-05 10:59:37 +08:00
Interceptor,
InterceptorExecutor,
} from './InterceptorManager';
2023-04-25 14:28:28 +08:00
import MiddlewareManager, {
2023-05-05 10:59:37 +08:00
MiddlewareCallback,
MiddlewareContext,
} from './MiddlewareManager';
import { mergeConfig } from './mergeConfig';
2023-03-23 20:09:00 +08:00
/**
*
*/
2023-03-28 20:35:40 +08:00
export type AxiosRequestMethod =
2023-05-05 10:59:37 +08:00
| AxiosAdapterRequestMethod
| 'options'
| 'get'
| 'head'
| 'post'
| 'put'
| 'patch'
| 'delete'
| 'trace'
| 'connect';
2023-03-23 20:09:00 +08:00
/**
*
*/
2023-03-28 20:35:40 +08:00
export interface AxiosRequestHeaders extends AnyObject {
2023-05-05 10:59:37 +08:00
/**
*
*/
common?: AnyObject;
/**
* options
*/
options?: AnyObject;
/**
* get
*/
get?: AnyObject;
/**
* head
*/
head?: AnyObject;
/**
* post
*/
post?: AnyObject;
/**
* put
*/
put?: AnyObject;
/**
* delete
*/
delete?: AnyObject;
/**
* trace
*/
trace?: AnyObject;
/**
* connect
*/
connect?: AnyObject;
2023-03-28 20:35:40 +08:00
}
2023-03-23 20:09:00 +08:00
/**
*
*/
2023-03-28 20:35:40 +08:00
export interface AxiosRequestFormData extends AnyObject {
2023-05-05 10:59:37 +08:00
/**
*
*/
name: string;
/**
*
*/
filePath: string;
2023-03-23 20:09:00 +08:00
}
/**
*
*/
export type AxiosRequestData =
2023-05-05 10:59:37 +08:00
| string
| AnyObject
| ArrayBuffer
| AxiosRequestFormData;
2023-04-06 15:16:12 +08:00
/**
*
*/
2023-05-05 10:59:37 +08:00
export type AxiosResponseData = number | AxiosAdapterResponseData;
2023-04-09 15:20:10 +08:00
/**
*
*/
export interface AxiosProgressEvent extends AnyObject {
2023-05-05 10:59:37 +08:00
/**
*
*/
progress: number;
}
/**
*
*/
export interface AxiosDownloadProgressEvent extends AxiosProgressEvent {
2023-05-05 10:59:37 +08:00
/**
* Bytes
*/
totalBytesWritten: number;
/**
* Bytes
*/
totalBytesExpectedToWrite: number;
}
/**
*
*/
export interface AxiosDownloadProgressCallback {
2023-05-05 10:59:37 +08:00
(event: AxiosDownloadProgressEvent): void;
}
/**
*
*/
export interface AxiosUploadProgressEvent extends AxiosProgressEvent {
2023-05-05 10:59:37 +08:00
/**
* Bytes
*/
totalBytesSent: number;
/**
* Bytes
*/
totalBytesExpectedToSend: number;
2023-03-23 20:09:00 +08:00
}
/**
*
*/
export interface AxiosUploadProgressCallback {
2023-05-05 10:59:37 +08:00
(event: AxiosUploadProgressEvent): void;
2023-03-23 20:09:00 +08:00
}
/**
*
*/
2023-03-28 20:35:40 +08:00
export interface AxiosRequestConfig
2023-05-05 10:59:37 +08:00
extends Partial<
Omit<AxiosAdapterRequestConfig, 'type' | 'success' | 'fail'>
> {
/**
*
*/
adapter?: AxiosAdapter;
/**
*
*/
baseURL?: string;
/**
* URL
*/
url?: string;
/**
*
*/
params?: AnyObject;
/**
*
*/
data?: AxiosRequestData;
/**
*
*/
headers?: AxiosRequestHeaders;
/**
*
*/
method?: AxiosRequestMethod;
/**
*
*/
cancelToken?: CancelToken;
/**
*
*/
download?: boolean;
/**
*
*/
upload?: boolean;
/**
*
*/
paramsSerializer?: (params?: AnyObject) => string;
/**
*
*/
validateStatus?: (status: number) => boolean;
/**
*
*/
transformRequest?: AxiosTransformer<AxiosRequestData>;
/**
*
*/
transformResponse?: AxiosTransformer<AxiosResponseData>;
/**
*
*/
errorHandler?: (error: unknown) => Promise<AxiosResponse>;
/**
*
*/
onDownloadProgress?: AxiosUploadProgressCallback;
/**
*
*/
onUploadProgress?: AxiosUploadProgressCallback;
2023-03-23 20:09:00 +08:00
}
/**
*
*/
2023-04-09 15:20:10 +08:00
export interface AxiosResponse<
2023-05-05 10:59:37 +08:00
TData extends AxiosResponseData = AxiosResponseData,
2023-04-18 11:14:57 +08:00
> extends AnyObject {
2023-05-05 10:59:37 +08:00
/**
*
*/
status: number;
/**
*
*/
statusText: string;
/**
*
*/
headers: AnyObject;
/**
*
*/
data: TData;
/**
*
*/
config: AxiosRequestConfig;
/**
*
*/
request?: AxiosAdapterPlatformTask;
2023-03-23 20:09:00 +08:00
}
/**
*
*/
2023-04-18 11:14:57 +08:00
export interface AxiosResponseError extends AnyObject {
2023-05-05 10:59:37 +08:00
/**
*
*/
status: number;
/**
*
*/
statusText: string;
/**
*
*/
headers: AnyObject;
/**
*
*/
data: AnyObject;
/**
*
*/
isFail: true;
/**
*
*/
config: AxiosRequestConfig;
/**
*
*/
request?: AxiosAdapterPlatformTask;
2023-03-23 20:09:00 +08:00
}
export interface AxiosRequest {
2023-05-05 10:59:37 +08:00
<TData extends AxiosResponseData>(config: AxiosRequestConfig): Promise<
AxiosResponse<TData>
>;
<TData extends AxiosResponseData>(
url: string,
config?: AxiosRequestConfig,
): Promise<AxiosResponse<TData>>;
}
/**
*
*/
export type AxiosRequestMethodFn = <TData extends AxiosResponseData>(
2023-05-05 10:59:37 +08:00
url: string,
config?: AxiosRequestConfig,
) => Promise<AxiosResponse<TData>>;
/**
*
*/
export type AxiosRequestMethodFnWithParams = <TData extends AxiosResponseData>(
2023-05-05 10:59:37 +08:00
url: string,
params?: AnyObject,
config?: AxiosRequestConfig,
) => Promise<AxiosResponse<TData>>;
/**
*
*/
export type AxiosRequestMethodFnWithData = <TData extends AxiosResponseData>(
2023-05-05 10:59:37 +08:00
url: string,
data?: AxiosRequestData,
config?: AxiosRequestConfig,
) => Promise<AxiosResponse<TData>>;
export interface AxiosDomainRequestHandler {
2023-05-05 10:59:37 +08:00
(config: AxiosRequestConfig): Promise<AxiosResponse>;
}
/**
* Axios
*/
2023-03-23 20:09:00 +08:00
export interface AxiosConstructor {
2023-05-05 10:59:37 +08:00
new (config: AxiosRequestConfig): Axios;
2023-03-23 20:09:00 +08:00
}
export default class Axios {
2023-05-05 10:59:37 +08:00
/**
*
*/
#parent?: Axios;
/**
*
*/
defaults: AxiosRequestConfig;
/**
*
*/
interceptors = {
/**
*
*/
request: new InterceptorManager<AxiosRequestConfig>(),
/**
*
*/
response: new InterceptorManager<AxiosResponse>(),
};
/**
*
*/
#middleware = new MiddlewareManager();
/**
* options
*/
options!: AxiosRequestMethodFn;
/**
* get
*/
get!: AxiosRequestMethodFnWithParams;
/**
* head
*/
head!: AxiosRequestMethodFnWithParams;
/**
* post
*/
post!: AxiosRequestMethodFnWithData;
/**
* put
*/
put!: AxiosRequestMethodFnWithData;
/**
* patch
*/
patch!: AxiosRequestMethodFnWithData;
/**
* delete
*/
delete!: AxiosRequestMethodFnWithParams;
/**
* trace
*/
trace!: AxiosRequestMethodFn;
/**
* connect
*/
connect!: AxiosRequestMethodFn;
/**
*
* @param config
* @param parent
*/
constructor(config: AxiosRequestConfig, parent?: Axios) {
this.defaults = config;
this.#parent = parent;
}
/**
*
*/
request: AxiosRequest = (
urlOrConfig: string | AxiosRequestConfig,
config: AxiosRequestConfig = {},
) => {
if (isString(urlOrConfig)) {
config.url = urlOrConfig;
} else {
config = urlOrConfig;
}
config = mergeConfig(this.defaults, config);
config.method = (config.method?.toLowerCase() ??
'get') as AxiosRequestMethod;
const requestHandler = {
resolved: this.#handleRequest,
};
const errorHandler = {
rejected: config.errorHandler,
};
const chain: (
| Partial<Interceptor<AxiosRequestConfig>>
| Partial<Interceptor<AxiosResponse>>
)[] = [];
this.#eachRequestInterceptors((requestInterceptor) => {
chain.unshift(requestInterceptor);
});
chain.push(requestHandler);
this.#eachResponseInterceptors((responseInterceptor) => {
chain.push(responseInterceptor);
});
chain.push(errorHandler);
return chain.reduce(
(next, { resolved, rejected }) =>
next.then(
// @ts-ignore
resolved,
rejected,
),
Promise.resolve(config),
) as Promise<AxiosResponse>;
};
#eachRequestInterceptors(executor: InterceptorExecutor<AxiosRequestConfig>) {
this.interceptors.request.forEach(executor);
if (this.#parent) {
this.#parent.#eachRequestInterceptors(executor);
}
}
#eachResponseInterceptors(executor: InterceptorExecutor<AxiosResponse>) {
this.interceptors.response.forEach(executor);
if (this.#parent) {
this.#parent.#eachResponseInterceptors(executor);
}
}
/**
*
*
2023-05-14 21:44:08 +08:00
* 1
* ```ts
* axios.use(async function middleware(ctx, next) {
* console.log(ctx.req);
* await next();
* console.log(ctx.res);
* });
* ```
*
* 2
* ```ts
* axios
* .use(async function middleware1(ctx, next) {
* console.log(ctx.req);
* await next();
* console.log(ctx.res);
* })
* .use(async function middleware2(ctx, next) {
* console.log(ctx.req);
* await next();
* console.log(ctx.res);
* });
* ```
2023-05-05 10:59:37 +08:00
*/
use = (middleware: MiddlewareCallback) => {
this.#middleware.use(middleware);
return this;
};
#handleRequest = async (config: AxiosRequestConfig) => {
const ctx = this.#middleware.createContext(config);
await this.#run(ctx, this.#handleResponse);
return ctx.res as AxiosResponse;
};
#handleResponse = async (ctx: MiddlewareContext) => {
ctx.res = await dispatchRequest(ctx.req);
};
#run = (
ctx: MiddlewareContext,
respond: MiddlewareCallback,
): Promise<void> => {
if (!this.#parent) {
return this.#middleware.run(ctx, respond);
}
return this.#middleware.enhanceRun(this.#parent.#run)(ctx, respond);
};
}
for (const method of PLAIN_METHODS) {
2023-05-05 10:59:37 +08:00
Axios.prototype[method] = function processRequestMethod(url, config = {}) {
config.method = method;
return this.request(url, config);
};
}
for (const method of WITH_PARAMS_METHODS) {
2023-05-05 10:59:37 +08:00
Axios.prototype[method] = function processRequestMethodWithParams(
url,
params,
config = {},
) {
config.method = method;
config.params = deepMerge(params, config.params);
return this.request(url, config);
};
}
for (const method of WITH_DATA_METHODS) {
2023-05-05 10:59:37 +08:00
Axios.prototype[method] = function processRequestMethodWithData(
url,
data,
config = {},
) {
config.method = method;
config.data = data;
return this.request(url, config);
};
2023-03-23 20:09:00 +08:00
}