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