Compare commits

...

3 Commits
v2.7.1 ... main

Author SHA1 Message Date
zjx0905 9c9debbc17 chore: release v2.7.2 2024-01-31 19:15:27 +08:00
zjxxxxxxxxx c0352a5d6a
docs: 修复安装方式错误 (#58)
#57
2024-01-25 19:12:17 +08:00
zjxxxxxxxxx 5d45cd9670
fix: 修复设置请求 headers 影响了全局配置 (#56)
#55
2024-01-10 19:33:33 +08:00
12 changed files with 132 additions and 78 deletions

View File

@ -1,3 +1,12 @@
## [2.7.2](https://github.com/zjxxxxxxxxx/axios-miniprogram/compare/v2.7.1...v2.7.2) (2024-01-31)
### Bug Fixes
* 修复设置请求 headers 影响了全局配置 ([#56](https://github.com/zjxxxxxxxxx/axios-miniprogram/issues/56)) ([5d45cd9](https://github.com/zjxxxxxxxxx/axios-miniprogram/commit/5d45cd9670c3bc8b58437d2defc58753ebc6392e)), closes [#55](https://github.com/zjxxxxxxxxx/axios-miniprogram/issues/55)
## [2.7.1](https://github.com/zjxxxxxxxxx/axios-miniprogram/compare/v2.7.0...v2.7.1) (2023-12-16)

View File

@ -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 是什么?

View File

@ -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 {
unlinkSync(introPath);
} catch {
//
}
linkSync(readmePath, introPath);
} catch {}
}

View File

@ -9,15 +9,15 @@ title: 开始
:::: code-group
```bash [NPM]
$ npm install -D axios-miniprogram
$ npm install axios-miniprogram
```
```bash [YARN]
$ yarn add -D axios-miniprogram
$ yarn add axios-miniprogram
```
```bash [PNPM]
$ pnpm install -D axios-miniprogram
$ pnpm install axios-miniprogram
```
::::

View File

@ -1,6 +1,6 @@
{
"name": "axios-miniprogram",
"version": "2.7.1",
"version": "2.7.2",
"description": "基于 Promise 的 HTTP 请求库,适用于各大小程序平台。",
"main": "dist/axios-miniprogram.cjs.js",
"module": "dist/axios-miniprogram.esm.js",

View File

@ -354,14 +354,16 @@ export interface AxiosConstructor {
export default class Axios {
/**
* @internal
*
*
*/
#parent?: Axios;
private declare parent?: Axios;
/**
*
*/
defaults: AxiosRequestConfig;
declare defaults: AxiosRequestConfig;
/**
*
@ -378,63 +380,64 @@ 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;
/**
*
* @param config
* @param parent
*/
constructor(config: AxiosRequestConfig, parent?: Axios) {
this.defaults = config;
this.#parent = parent;
this.parent = parent;
}
/**
@ -454,7 +457,7 @@ export default class Axios {
'get') as AxiosRequestMethod;
const requestHandler = {
resolved: this.#handleRequest,
resolved: this.handleRequest,
};
const errorHandler = {
rejected: config.errorHandler,
@ -464,12 +467,12 @@ export default class Axios {
| Partial<Interceptor<AxiosResponse>>
)[] = [];
this.#eachRequestInterceptors((requestInterceptor) => {
chain.unshift(requestInterceptor);
this.eachInterceptors('request', (interceptor) => {
chain.unshift(interceptor);
});
chain.push(requestHandler);
this.#eachResponseInterceptors((responseInterceptor) => {
chain.push(responseInterceptor);
this.eachInterceptors('response', (interceptor) => {
chain.push(interceptor);
});
chain.push(errorHandler);
@ -484,17 +487,19 @@ export default class Axios {
) 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);
/**
* @internal
*/
private eachInterceptors<T extends 'request' | 'response'>(
type: T,
executor: InterceptorExecutor<
T extends 'request' ? AxiosRequestConfig : AxiosResponse
>,
) {
// @ts-ignore
this.interceptors[type].forEach(executor);
if (this.parent) {
this.parent.eachInterceptors(type, executor);
}
}
@ -526,28 +531,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);
};
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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 中取值

View File

@ -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;
}
}
}

View File

@ -1 +1 @@
export const version = '2.7.1';
export const version = '2.7.2';

View File

@ -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(),