axios-miniprogram/src/core/Axios.ts

255 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-03-28 21:32:54 +08:00
import { buildURL } from '../helpers/buildURL';
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
import { combineURL } from '../helpers/combineURL';
2023-03-23 20:09:00 +08:00
import { mergeConfig } from './mergeConfig';
import {
AxiosAdapter,
2023-03-28 20:35:40 +08:00
AxiosAdapterRequestMethod,
2023-03-23 20:09:00 +08:00
AxiosAdapterTask,
2023-03-28 20:35:40 +08:00
AxiosAdapterResponse,
AxiosAdapterRequestConfig,
2023-04-05 13:31:48 +08:00
AxiosAdapterResponseError,
2023-03-24 20:15:51 +08:00
} from '../adapter';
2023-03-23 20:09:00 +08:00
import { CancelToken } from './cancel';
import dispatchRequest from './dispatchRequest';
import InterceptorManager from './InterceptorManager';
import { AxiosTransformer } from './transformData';
import AxiosDomain from './AxiosDomain';
2023-03-23 20:09:00 +08:00
2023-03-28 20:35:40 +08:00
export type AxiosRequestMethod =
| AxiosAdapterRequestMethod
2023-03-23 20:09:00 +08:00
| 'options'
| 'get'
| 'head'
| 'post'
| 'put'
| 'delete'
| 'trace'
| 'connect';
2023-03-28 20:35:40 +08:00
export interface AxiosRequestHeaders extends AnyObject {
2023-04-06 15:16:12 +08:00
/**
*
*/
common?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* options
*/
2023-03-28 20:35:40 +08:00
options?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* get
*/
2023-03-28 20:35:40 +08:00
get?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* head
*/
2023-03-28 20:35:40 +08:00
head?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* post
*/
2023-03-28 20:35:40 +08:00
post?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* put
*/
2023-03-28 20:35:40 +08:00
put?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* delete
*/
2023-03-28 20:35:40 +08:00
delete?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* trace
*/
2023-03-28 20:35:40 +08:00
trace?: AnyObject;
2023-04-06 15:16:12 +08:00
/**
* connect
*/
2023-03-28 20:35:40 +08:00
connect?: AnyObject;
}
2023-03-23 20:09:00 +08:00
2023-03-28 20:35:40 +08:00
export interface AxiosRequestFormData extends AnyObject {
2023-04-06 15:16:12 +08:00
/**
*
*/
name: string;
/**
*
*/
2023-03-23 20:09:00 +08:00
filePath: string;
}
2023-04-06 15:16:12 +08:00
export type AxiosRequestData = AnyObject | AxiosRequestFormData;
2023-03-23 20:09:00 +08:00
export interface AxiosProgressEvent {
progress: number;
totalBytesSent: number;
totalBytesExpectedToSend: number;
}
export interface AxiosProgressCallback {
(event: AxiosProgressEvent): void;
}
2023-03-28 20:35:40 +08:00
export interface AxiosRequestConfig
extends Omit<
Partial<AxiosAdapterRequestConfig>,
'type' | 'method' | 'success' | 'fail'
> {
/**
*
*/
2023-03-23 20:09:00 +08:00
adapter?: AxiosAdapter;
2023-03-28 20:35:40 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
baseURL?: string;
2023-03-28 20:35:40 +08:00
/**
*
*/
params?: AnyObject;
/**
*
*/
2023-04-06 15:16:12 +08:00
data?: AxiosRequestData;
2023-03-28 20:35:40 +08:00
/**
*
*/
headers?: AxiosRequestHeaders;
/**
*
*/
method?: AxiosRequestMethod;
/**
*
*/
2023-03-23 20:09:00 +08:00
cancelToken?: CancelToken;
2023-03-28 20:35:40 +08:00
/**
*
*/
upload?: boolean;
/**
*
*/
2023-03-23 20:09:00 +08:00
download?: boolean;
2023-03-28 20:35:40 +08:00
/**
*
*/
transformRequest?: AxiosTransformer | AxiosTransformer[];
/**
*
*/
transformResponse?: AxiosTransformer | AxiosTransformer[];
/**
*
2023-03-28 20:35:40 +08:00
*/
errorHandler?: (error: unknown) => Promise<AxiosResponse>;
2023-03-28 20:35:40 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
onUploadProgress?: AxiosProgressCallback;
2023-03-28 20:35:40 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
onDownloadProgress?: AxiosProgressCallback;
2023-03-28 20:35:40 +08:00
/**
*
*/
paramsSerializer?: (params?: AnyObject) => string;
/**
*
*/
2023-03-23 20:09:00 +08:00
validateStatus?: (status: number) => boolean;
}
2023-03-28 20:35:40 +08:00
export interface AxiosResponse<TData = unknown>
extends AxiosAdapterResponse<TData> {
2023-04-06 15:16:12 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
config?: AxiosRequestConfig;
2023-04-06 15:16:12 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
request?: AxiosAdapterTask;
}
2023-03-28 20:35:40 +08:00
export interface AxiosResponseError extends AxiosAdapterResponseError {
2023-04-06 15:16:12 +08:00
/**
* fail
*/
2023-04-05 13:31:48 +08:00
isFail: true;
2023-04-06 15:16:12 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
config?: AxiosRequestConfig;
2023-04-06 15:16:12 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
request?: AxiosAdapterTask;
}
export interface AxiosConstructor {
new (config: AxiosRequestConfig): Axios;
}
export default class Axios extends AxiosDomain {
2023-04-06 15:16:12 +08:00
/**
*
*/
interceptors = {
2023-04-06 15:16:12 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
request: new InterceptorManager<AxiosRequestConfig>(),
2023-04-06 15:16:12 +08:00
/**
*
*/
2023-03-23 20:09:00 +08:00
response: new InterceptorManager<AxiosResponse>(),
};
constructor(defaults: AxiosRequestConfig = {}) {
super(defaults, (config) => this.#processRequest(config));
2023-03-23 20:09:00 +08:00
}
getUri(config: AxiosRequestConfig): string {
2023-03-23 20:09:00 +08:00
const { url, params, paramsSerializer } = mergeConfig(
this.defaults,
config,
);
return buildURL(url, params, paramsSerializer).replace(/^\?/, '');
}
2023-04-06 15:16:12 +08:00
/**
*
2023-04-06 15:16:12 +08:00
*/
fork(defaults: AxiosRequestConfig) {
const { baseURL = '' } = defaults;
if (!isAbsoluteURL(baseURL)) {
2023-04-07 12:39:23 +08:00
defaults.baseURL = combineURL(this.defaults.baseURL ?? '', baseURL);
}
return new AxiosDomain(
mergeConfig(this.defaults, defaults),
(config) => this.#processRequest(config),
);
}
2023-04-07 12:39:23 +08:00
#processRequest<TData = unknown>(config: AxiosRequestConfig) {
2023-04-05 08:40:00 +08:00
const { request, response } = this.interceptors;
2023-03-23 20:09:00 +08:00
let promiseRequest = Promise.resolve(config);
2023-04-05 08:40:00 +08:00
request.forEach(({ resolved, rejected }) => {
2023-03-23 20:09:00 +08:00
promiseRequest = promiseRequest.then(
resolved,
rejected,
) as Promise<AxiosRequestConfig>;
2023-04-05 08:40:00 +08:00
}, true);
2023-03-23 20:09:00 +08:00
let promiseResponse = promiseRequest.then(dispatchRequest);
2023-04-05 08:40:00 +08:00
response.forEach(({ resolved, rejected }) => {
2023-03-23 20:09:00 +08:00
promiseResponse = promiseResponse.then(resolved, rejected) as Promise<
AxiosResponse<unknown>
>;
});
return promiseResponse as Promise<AxiosResponse<TData>>;
2023-04-07 12:39:23 +08:00
}
2023-03-23 20:09:00 +08:00
}