@anjianshi/utils 3.3.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/request/client.d.ts +3 -3
- package/request/client.js +3 -2
- package/request/instance.d.ts +1 -1
- package/request/options.d.ts +3 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -32,11 +32,11 @@
|
|
|
32
32
|
"typescript": "^5.8.3",
|
|
33
33
|
"vconsole": "^3.15.1",
|
|
34
34
|
"@anjianshi/presets-eslint-node": "6.0.0",
|
|
35
|
-
"@anjianshi/presets-
|
|
35
|
+
"@anjianshi/presets-eslint-base": "6.0.0",
|
|
36
36
|
"@anjianshi/presets-eslint-typescript": "6.0.0",
|
|
37
|
-
"@anjianshi/presets-
|
|
37
|
+
"@anjianshi/presets-prettier": "3.0.5",
|
|
38
38
|
"@anjianshi/presets-eslint-react": "6.0.0",
|
|
39
|
-
"@anjianshi/presets-
|
|
39
|
+
"@anjianshi/presets-typescript": "3.2.5"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@emotion/react": "^11.14.0",
|
package/request/client.d.ts
CHANGED
|
@@ -10,9 +10,9 @@ export declare abstract class BaseRequestClient<FailedT extends Failed> {
|
|
|
10
10
|
loggerName?: string;
|
|
11
11
|
});
|
|
12
12
|
/** 生成一个快捷方式函数,调用它相当于调用 client.request() */
|
|
13
|
-
asFunction(): <T>(inputUrl: string, inputOptions?: Options) => Promise<Result<T, FailedT>>;
|
|
14
|
-
request<T>(inputUrl: string, inputOptions?: Options): Promise<Result<T, FailedT>>;
|
|
15
|
-
formatOptions(input: Options): Promise<FormattedOptions
|
|
13
|
+
asFunction(): <T>(inputUrl: string, inputOptions?: Options<T>) => Promise<Result<T, FailedT>>;
|
|
14
|
+
request<T>(inputUrl: string, inputOptions?: Options<T>): Promise<Result<T, FailedT>>;
|
|
15
|
+
formatOptions<T>(input: Options<T>): Promise<FormattedOptions<T>>;
|
|
16
16
|
/** 请求发起前调用此方法补充 Headers 内容 */
|
|
17
17
|
protected getHeaders(options: FormattedOptions, inputOptions: Options): Record<string, string> | undefined | Promise<Record<string, string> | undefined>;
|
|
18
18
|
/**
|
package/request/client.js
CHANGED
|
@@ -38,7 +38,7 @@ export class BaseRequestClient {
|
|
|
38
38
|
}
|
|
39
39
|
catch (error) {
|
|
40
40
|
// 失败情形“手动取消”
|
|
41
|
-
if (manualSignal
|
|
41
|
+
if (manualSignal?.aborted && manualSignal.reason === error) {
|
|
42
42
|
const reason = error instanceof DOMException && error.name === 'AbortError' ? null : error;
|
|
43
43
|
return this.handleError(failed('Request Aborted', 'RequestAborted', { options, reason }));
|
|
44
44
|
}
|
|
@@ -99,7 +99,7 @@ export class BaseRequestClient {
|
|
|
99
99
|
// -------------------------------
|
|
100
100
|
async formatOptions(input) {
|
|
101
101
|
const predefined = this.prefefinedOptions;
|
|
102
|
-
const { urlPrefix = predefined.urlPrefix ?? '', url: rawUrl, query = {}, method = predefined.method ?? 'GET', headers: rawHeaders = {}, body: rawBody = null, data, timeout = predefined.timeout ?? 0, binary = false, signal, } = input;
|
|
102
|
+
const { urlPrefix = predefined.urlPrefix ?? '', url: rawUrl, query = {}, method = predefined.method ?? 'GET', headers: rawHeaders = {}, body: rawBody = null, data, timeout = predefined.timeout ?? 0, binary = false, signal, format, } = input;
|
|
103
103
|
const headers = {
|
|
104
104
|
...(predefined.headers ?? {}),
|
|
105
105
|
...rawHeaders,
|
|
@@ -123,6 +123,7 @@ export class BaseRequestClient {
|
|
|
123
123
|
timeout,
|
|
124
124
|
binary,
|
|
125
125
|
signal,
|
|
126
|
+
format,
|
|
126
127
|
};
|
|
127
128
|
Object.assign(options.headers, await this.getHeaders(options, input));
|
|
128
129
|
return options;
|
package/request/instance.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { RequestClient } from './client.js';
|
|
2
2
|
/** 提供一个即取即用的 RequestClient 实例及其函数版本 */
|
|
3
3
|
export declare const client: RequestClient;
|
|
4
|
-
export declare const request: <T>(inputUrl: string, inputOptions?: import("./options.js").Options) => Promise<import("../index.js").Result<T, import("./error.js").RequestFailed>>;
|
|
4
|
+
export declare const request: <T>(inputUrl: string, inputOptions?: import("./options.js").Options<T> | undefined) => Promise<import("../index.js").Result<T, import("./error.js").RequestFailed>>;
|
package/request/options.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 请求参数
|
|
3
3
|
*/
|
|
4
|
-
export interface Options {
|
|
4
|
+
export interface Options<T = unknown> {
|
|
5
5
|
urlPrefix?: string;
|
|
6
6
|
url?: string;
|
|
7
7
|
query?: Record<string, string | number | undefined>;
|
|
@@ -20,9 +20,9 @@ export interface Options {
|
|
|
20
20
|
/** 可通过此信号手动终止请求 */
|
|
21
21
|
signal?: AbortSignal;
|
|
22
22
|
/** 对请求成功时得到的数据进行最终格式化 */
|
|
23
|
-
format?:
|
|
23
|
+
format?: (responseData: any) => T;
|
|
24
24
|
}
|
|
25
25
|
/** 可预指定的请求参数 */
|
|
26
26
|
export type PredefinedOptions = Pick<Options, 'urlPrefix' | 'method' | 'headers' | 'timeout'>;
|
|
27
27
|
/** 经过整理的请求参数(client 内部使用) */
|
|
28
|
-
export type FormattedOptions = Required<Pick<Options, 'url' | 'method' | 'headers' | 'body' | 'timeout' | 'binary'>> & Pick<Options
|
|
28
|
+
export type FormattedOptions<T = unknown> = Required<Pick<Options, 'url' | 'method' | 'headers' | 'body' | 'timeout' | 'binary'>> & Pick<Options<T>, 'signal' | 'format'>;
|