parent
6af2d8e5c3
commit
b05be4deca
16
README.md
16
README.md
|
@ -1,24 +1,26 @@
|
|||
<h1 align="center">axios-miniprogram</h1>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml">
|
||||
<p style="text-align: center;" align="center">
|
||||
<a style="display: inline-block;margin-left: 5px;" href="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml">
|
||||
<img src="https://github.com/zjxxxxxxxxx/axios-miniprogram/actions/workflows/ci.yml/badge.svg" alt="ci"/>
|
||||
</a>
|
||||
<a style="margin-left:5px;" href="https://codecov.io/gh/zjxxxxxxxxx/axios-miniprogram" >
|
||||
<a style="display: inline-block;margin-left: 5px;" href="https://codecov.io/gh/zjxxxxxxxxx/axios-miniprogram" >
|
||||
<img src="https://codecov.io/gh/zjxxxxxxxxx/axios-miniprogram/branch/main/graph/badge.svg?token=WIQVYX2WIK" alt="codecov"/>
|
||||
</a>
|
||||
<a style="margin-left:5px;" href="https://opensource.org/licenses/MIT">
|
||||
<a style="display: inline-block;margin-left: 5px;" href="https://opensource.org/licenses/MIT">
|
||||
<img src="https://img.shields.io/github/license/zjxxxxxxxxx/axios-miniprogram" alt="license"/>
|
||||
</a>
|
||||
<a style="margin-left:5px;" href="https://www.npmjs.org/package/axios-miniprogram">
|
||||
<a style="display: inline-block;margin-left: 5px;" href="https://www.npmjs.org/package/axios-miniprogram">
|
||||
<img src="https://img.shields.io/bundlephobia/min/axios-miniprogram" alt="npm bundle size"/>
|
||||
</a>
|
||||
<a style="margin-left:5px;" href="https://www.npmjs.org/package/axios-miniprogram">
|
||||
<a style="display: inline-block;" href="https://www.npmjs.org/package/axios-miniprogram">
|
||||
<img alt="npm" src="https://img.shields.io/npm/dt/axios-miniprogram"/>
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<p align="center"><a href='https://axios-miniprogram.com'>中文文档</a></p>
|
||||
<p style="text-align: center;" align="center">
|
||||
<a href='https://axios-miniprogram.com'>中文文档</a>
|
||||
</p>
|
||||
|
||||
## axios-miniprogram 是什么?
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { linkSync } from 'node:fs';
|
||||
import { linkSync, unlinkSync } from 'node:fs';
|
||||
import { resolve } from '../../../scripts/utils';
|
||||
|
||||
const readmePath = resolve('README.md');
|
||||
|
@ -6,6 +6,9 @@ const introPath = resolve('docs/pages/guide/intro.md');
|
|||
|
||||
export function linkIntro() {
|
||||
try {
|
||||
linkSync(readmePath, introPath);
|
||||
} catch {}
|
||||
unlinkSync(introPath);
|
||||
} catch {
|
||||
//
|
||||
}
|
||||
linkSync(readmePath, introPath);
|
||||
}
|
||||
|
|
|
@ -354,14 +354,16 @@ export interface AxiosConstructor {
|
|||
|
||||
export default class Axios {
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* 父级实例
|
||||
*/
|
||||
#parent?: Axios;
|
||||
private declare parent?: Axios;
|
||||
|
||||
/**
|
||||
* 默认请求配置
|
||||
*/
|
||||
defaults: AxiosRequestConfig;
|
||||
declare defaults: AxiosRequestConfig;
|
||||
|
||||
/**
|
||||
* 拦截器
|
||||
|
@ -378,54 +380,56 @@ export default class Axios {
|
|||
};
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* 中间件
|
||||
*/
|
||||
#middleware = new MiddlewareManager();
|
||||
private middleware = new MiddlewareManager();
|
||||
|
||||
/**
|
||||
* 发送 options 请求
|
||||
*/
|
||||
options!: AxiosRequestMethodFn;
|
||||
declare options: AxiosRequestMethodFn;
|
||||
|
||||
/**
|
||||
* 发送 get 请求
|
||||
*/
|
||||
get!: AxiosRequestMethodFnWithParams;
|
||||
declare get: AxiosRequestMethodFnWithParams;
|
||||
|
||||
/**
|
||||
* 发送 head 请求
|
||||
*/
|
||||
head!: AxiosRequestMethodFnWithParams;
|
||||
declare head: AxiosRequestMethodFnWithParams;
|
||||
|
||||
/**
|
||||
* 发送 post 请求
|
||||
*/
|
||||
post!: AxiosRequestMethodFnWithData;
|
||||
declare post: AxiosRequestMethodFnWithData;
|
||||
|
||||
/**
|
||||
* 发送 put 请求
|
||||
*/
|
||||
put!: AxiosRequestMethodFnWithData;
|
||||
declare put: AxiosRequestMethodFnWithData;
|
||||
|
||||
/**
|
||||
* 发送 patch 请求
|
||||
*/
|
||||
patch!: AxiosRequestMethodFnWithData;
|
||||
declare patch: AxiosRequestMethodFnWithData;
|
||||
|
||||
/**
|
||||
* 发送 delete 请求
|
||||
*/
|
||||
delete!: AxiosRequestMethodFnWithParams;
|
||||
declare delete: AxiosRequestMethodFnWithParams;
|
||||
|
||||
/**
|
||||
* 发送 trace 请求
|
||||
*/
|
||||
trace!: AxiosRequestMethodFn;
|
||||
declare trace: AxiosRequestMethodFn;
|
||||
|
||||
/**
|
||||
* 发送 connect 请求
|
||||
*/
|
||||
connect!: AxiosRequestMethodFn;
|
||||
declare connect: AxiosRequestMethodFn;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -434,7 +438,7 @@ export default class Axios {
|
|||
*/
|
||||
constructor(config: AxiosRequestConfig, parent?: Axios) {
|
||||
this.defaults = config;
|
||||
this.#parent = parent;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -454,7 +458,7 @@ export default class Axios {
|
|||
'get') as AxiosRequestMethod;
|
||||
|
||||
const requestHandler = {
|
||||
resolved: this.#handleRequest,
|
||||
resolved: this.handleRequest,
|
||||
};
|
||||
const errorHandler = {
|
||||
rejected: config.errorHandler,
|
||||
|
@ -464,11 +468,11 @@ export default class Axios {
|
|||
| Partial<Interceptor<AxiosResponse>>
|
||||
)[] = [];
|
||||
|
||||
this.#eachRequestInterceptors((requestInterceptor) => {
|
||||
this.eachRequestInterceptors((requestInterceptor) => {
|
||||
chain.unshift(requestInterceptor);
|
||||
});
|
||||
chain.push(requestHandler);
|
||||
this.#eachResponseInterceptors((responseInterceptor) => {
|
||||
this.eachResponseInterceptors((responseInterceptor) => {
|
||||
chain.push(responseInterceptor);
|
||||
});
|
||||
chain.push(errorHandler);
|
||||
|
@ -484,17 +488,27 @@ export default class Axios {
|
|||
) as Promise<AxiosResponse>;
|
||||
};
|
||||
|
||||
#eachRequestInterceptors(executor: InterceptorExecutor<AxiosRequestConfig>) {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private eachRequestInterceptors(
|
||||
executor: InterceptorExecutor<AxiosRequestConfig>,
|
||||
) {
|
||||
this.interceptors.request.forEach(executor);
|
||||
if (this.#parent) {
|
||||
this.#parent.#eachRequestInterceptors(executor);
|
||||
if (this.parent) {
|
||||
this.parent.eachRequestInterceptors(executor);
|
||||
}
|
||||
}
|
||||
|
||||
#eachResponseInterceptors(executor: InterceptorExecutor<AxiosResponse>) {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private eachResponseInterceptors(
|
||||
executor: InterceptorExecutor<AxiosResponse>,
|
||||
) {
|
||||
this.interceptors.response.forEach(executor);
|
||||
if (this.#parent) {
|
||||
this.#parent.#eachResponseInterceptors(executor);
|
||||
if (this.parent) {
|
||||
this.parent.eachResponseInterceptors(executor);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -526,28 +540,37 @@ export default class Axios {
|
|||
* ```
|
||||
*/
|
||||
use = (middleware: MiddlewareCallback) => {
|
||||
this.#middleware.use(middleware);
|
||||
this.middleware.use(middleware);
|
||||
return this;
|
||||
};
|
||||
|
||||
#handleRequest = async (config: AxiosRequestConfig) => {
|
||||
const ctx = this.#middleware.createContext(config);
|
||||
await this.#run(ctx, this.#handleResponse);
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private 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) => {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private async handleResponse(ctx: MiddlewareContext) {
|
||||
ctx.res = await dispatchRequest(ctx.req);
|
||||
};
|
||||
}
|
||||
|
||||
#run = (
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private run = (
|
||||
ctx: MiddlewareContext,
|
||||
respond: MiddlewareCallback,
|
||||
): Promise<void> => {
|
||||
if (!this.#parent) {
|
||||
return this.#middleware.run(ctx, respond);
|
||||
if (!this.parent) {
|
||||
return this.middleware.run(ctx, respond);
|
||||
}
|
||||
return this.#middleware.enhanceRun(this.#parent.#run)(ctx, respond);
|
||||
return this.middleware.enhanceRun(this.parent.run)(ctx, respond);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -29,14 +29,18 @@ export interface InterceptorExecutor<T = unknown> {
|
|||
*/
|
||||
export default class InterceptorManager<T = unknown> {
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* 生成拦截器标识符
|
||||
*/
|
||||
#id = 0;
|
||||
private id = 0;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* 拦截器缓存池
|
||||
*/
|
||||
#interceptors = new Map<number, Interceptor<T>>();
|
||||
private interceptors = new Map<number, Interceptor<T>>();
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
@ -44,7 +48,7 @@ export default class InterceptorManager<T = unknown> {
|
|||
* 拦截器数量
|
||||
*/
|
||||
get size() {
|
||||
return this.#interceptors.size;
|
||||
return this.interceptors.size;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,12 +62,12 @@ export default class InterceptorManager<T = unknown> {
|
|||
resolved: InterceptorResolved<T>,
|
||||
rejected?: InterceptorRejected<T>,
|
||||
): number {
|
||||
this.#interceptors.set(++this.#id, {
|
||||
this.interceptors.set(++this.id, {
|
||||
resolved,
|
||||
rejected,
|
||||
});
|
||||
|
||||
return this.#id;
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,14 +76,14 @@ export default class InterceptorManager<T = unknown> {
|
|||
* @param id 拦截器标识符
|
||||
*/
|
||||
eject(id: number): boolean {
|
||||
return this.#interceptors.delete(id);
|
||||
return this.interceptors.delete(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空拦截器
|
||||
*/
|
||||
clear() {
|
||||
this.#interceptors.clear();
|
||||
this.interceptors.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,6 +94,6 @@ export default class InterceptorManager<T = unknown> {
|
|||
* @param executor 执行器
|
||||
*/
|
||||
forEach(executor: InterceptorExecutor<T>): void {
|
||||
this.#interceptors.forEach(executor);
|
||||
this.interceptors.forEach(executor);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,16 +34,18 @@ export interface MiddlewareCallback {
|
|||
*/
|
||||
export default class MiddlewareManager {
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* 中间件缓存池
|
||||
*/
|
||||
#middlewares: MiddlewareCallback[] = [];
|
||||
private middlewares: MiddlewareCallback[] = [];
|
||||
|
||||
/**
|
||||
* 注册中间件
|
||||
*/
|
||||
use(middleware: MiddlewareCallback) {
|
||||
assert(isFunction(middleware), 'middleware 不是一个 function');
|
||||
this.#middlewares.push(middleware);
|
||||
this.middlewares.push(middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -67,7 +69,7 @@ export default class MiddlewareManager {
|
|||
* @param respond 目标函数
|
||||
*/
|
||||
run(ctx: MiddlewareContext, respond: MiddlewareCallback) {
|
||||
const middlewares = [...this.#middlewares, respond];
|
||||
const middlewares = [...this.middlewares, respond];
|
||||
async function next() {
|
||||
await middlewares.shift()!(ctx, next);
|
||||
}
|
||||
|
|
|
@ -39,12 +39,10 @@ export function mergeConfig(
|
|||
}
|
||||
// 深度合并 config1 和 config2 中的对象
|
||||
else if (deepMergeConfigMap[key]) {
|
||||
if (isPlainObject(val1) && isPlainObject(val2)) {
|
||||
config[key] = deepMerge(val1, val2);
|
||||
if (isPlainObject(val1)) {
|
||||
config[key] = deepMerge(val1, isPlainObject(val2) ? val2 : {});
|
||||
} else if (isPlainObject(val2)) {
|
||||
config[key] = val2;
|
||||
} else if (isPlainObject(val1)) {
|
||||
config[key] = val1;
|
||||
config[key] = deepMerge(val2, {});
|
||||
}
|
||||
}
|
||||
// 优先从 config2 中取值,如果没有值就从 config1 中取值
|
||||
|
|
|
@ -44,24 +44,27 @@ export function isCancel(value: unknown): value is Cancel {
|
|||
}
|
||||
|
||||
export class CancelToken {
|
||||
#reason?: Cancel;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private declare reason?: Cancel;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
onCancel: Promise<Cancel>['then'];
|
||||
declare onCancel: Promise<Cancel>['then'];
|
||||
|
||||
constructor(executor: CancelExecutor) {
|
||||
let action!: CancelAction;
|
||||
const promise = new Promise<Cancel>((resolve) => {
|
||||
action = (message) => {
|
||||
if (this.#reason) {
|
||||
if (this.reason) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.#reason = new Cancel(message);
|
||||
this.reason = new Cancel(message);
|
||||
|
||||
resolve(this.#reason);
|
||||
resolve(this.reason);
|
||||
};
|
||||
});
|
||||
|
||||
|
@ -86,8 +89,8 @@ export class CancelToken {
|
|||
* @internal
|
||||
*/
|
||||
throwIfRequested(): void {
|
||||
if (this.#reason) {
|
||||
throw this.#reason;
|
||||
if (this.reason) {
|
||||
throw this.reason;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -127,6 +127,25 @@ describe('src/core/mergeConfig.ts', () => {
|
|||
expect(mergeConfig(c1 as any, c2 as any)).toEqual({});
|
||||
});
|
||||
|
||||
test('深度合并应该返回新的对象', () => {
|
||||
const c1 = {
|
||||
headers: { t: 1 },
|
||||
params: {},
|
||||
};
|
||||
const c2 = {
|
||||
headers: {},
|
||||
params: { t: 1 },
|
||||
};
|
||||
const c3 = mergeConfig(c1 as any, c2 as any);
|
||||
|
||||
expect(c3.headers === c1.headers).toBeFalsy();
|
||||
expect(c3.params === c1.params).toBeFalsy();
|
||||
expect(c3.headers === c2.headers).toBeFalsy();
|
||||
expect(c3.params === c2.params).toBeFalsy();
|
||||
expect(c3.headers).toEqual(c1.headers);
|
||||
expect(c3.params).toEqual(c2.params);
|
||||
});
|
||||
|
||||
test('应该优先取 config2', () => {
|
||||
const c1 = {
|
||||
adapter: vi.fn(),
|
||||
|
|
Loading…
Reference in New Issue