@anjianshi/utils 3.8.4 → 3.8.6

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.
@@ -1,3 +1,4 @@
1
+ import util from 'node:util';
1
2
  import { type LogInfo, LogLevel, LogHandler } from '../../logging/index.js';
2
3
  /**
3
4
  * 向 console 输出日志
@@ -37,6 +38,7 @@ export interface FileHandlerOptions {
37
38
  maxLength: number;
38
39
  flushLength: number;
39
40
  flushInterval: number;
41
+ format: util.InspectOptions;
40
42
  }
41
43
  export declare class FileHandler extends LogHandler {
42
44
  readonly options: FileHandlerOptions;
@@ -69,6 +69,7 @@ export class FileHandler extends LogHandler {
69
69
  maxLength: 10000,
70
70
  flushLength: 100000,
71
71
  flushInterval: 1000,
72
+ format: {},
72
73
  ...(options ?? {}),
73
74
  };
74
75
  this.initLogDir();
@@ -105,7 +106,7 @@ export class FileHandler extends LogHandler {
105
106
  if (typeof item === 'string')
106
107
  item = item.replace(/\x1b\[\d+m/g, '');
107
108
  // 利用 util.format() 获得和 console.log() 相同的输出(因为 console.log() 底层也是用的 util.format())
108
- return util.format(item);
109
+ return util.formatWithOptions(this.options.format, item);
109
110
  }
110
111
  // Handle buffer & flush
111
112
  buffer = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "3.8.4",
3
+ "version": "3.8.6",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -38,12 +38,12 @@
38
38
  "redis": "^5.5.6",
39
39
  "typescript": "^5.8.3",
40
40
  "vconsole": "^3.15.1",
41
- "@anjianshi/presets-prettier": "3.2.0",
42
41
  "@anjianshi/presets-eslint-base": "6.1.2",
43
- "@anjianshi/presets-eslint-react": "6.1.3",
44
42
  "@anjianshi/presets-eslint-node": "6.1.2",
45
- "@anjianshi/presets-typescript": "3.2.5",
46
- "@anjianshi/presets-eslint-typescript": "6.1.2"
43
+ "@anjianshi/presets-eslint-react": "6.1.3",
44
+ "@anjianshi/presets-prettier": "3.2.0",
45
+ "@anjianshi/presets-eslint-typescript": "6.1.2",
46
+ "@anjianshi/presets-typescript": "3.2.5"
47
47
  },
48
48
  "prettier": "@anjianshi/presets-prettier/prettierrc"
49
49
  }
package/request/client.js CHANGED
@@ -2,6 +2,7 @@
2
2
  * 实现一个功能完善、异常处理逻辑全面的请求发起器。
3
3
  * 可通过继承子类来扩展其功能。
4
4
  */
5
+ import isPlainObject from 'lodash/isPlainObject.js';
5
6
  import pick from 'lodash/pick.js';
6
7
  import { success, failed, formatFailed, exceptionToFailed, } from '../lang/result.js';
7
8
  import { getLogger } from '../logging/index.js';
@@ -115,7 +116,8 @@ export class BaseRequestClient {
115
116
  // -------------------------------
116
117
  async formatOptions(input) {
117
118
  const predefined = this.prefefinedOptions;
118
- const { urlPrefix = predefined.urlPrefix ?? '', url: rawUrl, query = {}, method = predefined.method ?? 'GET', headers: rawHeaders = {}, body: rawBody = null, data, binary = false, timeout = predefined.timeout ?? 0, signal, retry = predefined.retry ?? 0, format, } = input;
119
+ const { urlPrefix = predefined.urlPrefix ?? '', url: rawUrl, query = {}, method = predefined.method ?? 'GET', headers: rawHeaders = {}, body: rawBody = null, data, // eslint-disable-line @typescript-eslint/no-unsafe-assignment
120
+ binary = false, timeout = predefined.timeout ?? 0, signal, retry = predefined.retry ?? 0, format, } = input;
119
121
  const headers = {
120
122
  ...(predefined.headers ?? {}),
121
123
  ...rawHeaders,
@@ -123,7 +125,9 @@ export class BaseRequestClient {
123
125
  let body = rawBody;
124
126
  if (data !== undefined) {
125
127
  if (method === 'GET') {
126
- Object.assign(query, data);
128
+ if (isPlainObject(data)) {
129
+ Object.assign(query, data);
130
+ }
127
131
  }
128
132
  else {
129
133
  body = data instanceof FormData ? data : JSON.stringify(data);
@@ -9,10 +9,13 @@ export interface Options<T = unknown> {
9
9
  headers?: Record<string, string>;
10
10
  body?: string | FormData | null;
11
11
  /**
12
- * 向后端传递的数据。对于 GET 请求,会合并到 query 中;对于 POST 请求,会作为 POST body,代替 body 参数
13
- * 注意:为了支持传入 interface 类型的值,Record 只能定义成 Record<string, any>
12
+ * 向后端传递的数据。
13
+ * 1. 对于 GET 请求:若 data 为 plain object 会合并到 query 中,否则会被忽略。
14
+ * 2. 对于 POST 请求:会作为 POST body,代替 body 参数。
15
+ * 其中若为 FormData,会原样交给 fetch();否则进行 JSON 化。
16
+ * 3. 为了支持传入 interface 类型的值,Record 只能定义成 Record<string, any>
14
17
  */
15
- data?: FormData | Record<string, any>;
18
+ data?: any;
16
19
  /** 是否把响应内容作为二进制处理(结果是 blob) */
17
20
  binary?: boolean;
18
21
  /** 超时时间,不指定或设为 0 代表不限 */