@anjianshi/utils 3.2.0 → 3.2.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "3.2.0",
3
+ "version": "3.2.1",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -31,12 +31,12 @@
31
31
  "redis": "^5.5.6",
32
32
  "typescript": "^5.8.3",
33
33
  "vconsole": "^3.15.1",
34
- "@anjianshi/presets-eslint-base": "6.0.0",
35
- "@anjianshi/presets-eslint-node": "6.0.0",
34
+ "@anjianshi/presets-prettier": "3.0.5",
36
35
  "@anjianshi/presets-eslint-react": "6.0.0",
36
+ "@anjianshi/presets-eslint-node": "6.0.0",
37
+ "@anjianshi/presets-eslint-base": "6.0.0",
37
38
  "@anjianshi/presets-eslint-typescript": "6.0.0",
38
- "@anjianshi/presets-typescript": "3.2.5",
39
- "@anjianshi/presets-prettier": "3.0.5"
39
+ "@anjianshi/presets-typescript": "3.2.5"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@emotion/react": "^11.14.0",
@@ -10,8 +10,8 @@ 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>>;
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
15
  formatOptions(input: Options): Promise<FormattedOptions>;
16
16
  /** 请求发起前调用此方法补充 Headers 内容 */
17
17
  protected getHeaders(options: FormattedOptions, inputOptions: Options): Record<string, string> | undefined | Promise<Record<string, string> | undefined>;
package/request/client.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * 可通过继承子类来扩展其功能。
4
4
  */
5
5
  import pick from 'lodash/pick.js';
6
- import { success, failed, formatFailed, exceptionToFailed, } from '../lang/result.js';
6
+ import { success, failed, formatSuccess, formatFailed, exceptionToFailed, } from '../lang/result.js';
7
7
  import { getLogger } from '../logging/index.js';
8
8
  import { combineUrl } from '../url.js';
9
9
  /** 此基类不可直接使用,因其未对错误格式进行具体约定 */
@@ -26,7 +26,7 @@ export class BaseRequestClient {
26
26
  url: inputUrl,
27
27
  ...(inputOptions ?? {}),
28
28
  });
29
- const { url, method, headers, body, timeout, signal: manualSignal } = options;
29
+ const { url, method, headers, body, timeout, signal: manualSignal, format } = options;
30
30
  const timeoutSignal = timeout ? AbortSignal.timeout(timeout) : undefined;
31
31
  const signal = manualSignal || timeoutSignal
32
32
  ? AbortSignal.any([manualSignal, timeoutSignal].filter(Boolean))
@@ -63,7 +63,7 @@ export class BaseRequestClient {
63
63
  }
64
64
  // 解析响应内容
65
65
  const result = await this.parseResponse(options, response);
66
- return result;
66
+ return formatSuccess(result, data => (format ? format(data) : data));
67
67
  }
68
68
  // -------------------------------
69
69
  // 请求预处理
@@ -1,5 +1,5 @@
1
1
  export type * from './options.js';
2
- export * from './error.js';
3
- export * as errors from './error.js';
2
+ export type * from './error.js';
3
+ export type * as errors from './error.js';
4
4
  export * from './client.js';
5
5
  export * from './instance.js';
package/request/index.js CHANGED
@@ -1,4 +1,2 @@
1
- export * from './error.js';
2
- export * as errors from './error.js';
3
1
  export * from './client.js';
4
2
  export * from './instance.js';
@@ -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>>;
@@ -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>;
@@ -19,8 +19,9 @@ export interface Options {
19
19
  timeout?: number;
20
20
  /** 可通过此信号手动终止请求 */
21
21
  signal?: AbortSignal;
22
+ format?: (responseData: unknown) => T;
22
23
  }
23
24
  /** 可预指定的请求参数 */
24
25
  export type PredefinedOptions = Pick<Options, 'urlPrefix' | 'method' | 'headers' | 'timeout'>;
25
26
  /** 经过整理的请求参数(client 内部使用) */
26
- export type FormattedOptions = Required<Pick<Options, 'url' | 'method' | 'headers' | 'body' | 'timeout' | 'binary'>> & Pick<Options, 'signal'>;
27
+ export type FormattedOptions = Required<Pick<Options, 'url' | 'method' | 'headers' | 'body' | 'timeout' | 'binary'>> & Pick<Options, 'signal' | 'format'>;