@zwa73/utils 1.0.255 → 1.0.257

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.
@@ -45,5 +45,5 @@ declare class _UtilFunc {
45
45
  /**获取当前公网ipv4 */
46
46
  static getPublicIpv4(): Promise<string>;
47
47
  }
48
- export declare const UtilFunc: import("@zwa73/modular-mixer").ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "preset" | "getTime" | "initField" | "initObject" | "afterward" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "lazyFunction" | "splitToChunk" | "mergeFromChunk" | "structEqual" | "concurrent" | "dynamicImport" | "createInjectable" | "checkSharpSchema" | "outcome" | "extractOutcome" | "isStatus" | "eitherize" | "parseSrtTime" | "formatSrtTime" | "parseSrt" | "createSrt" | "ivk" | "s2l" | "l2s">;
48
+ export declare const UtilFunc: import("@zwa73/modular-mixer").ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "preset" | "mergeOption" | "getTime" | "initField" | "initObject" | "afterward" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "lazyFunction" | "splitToChunk" | "mergeFromChunk" | "structEqual" | "concurrent" | "dynamicImport" | "createInjectable" | "checkSharpSchema" | "outcome" | "extractOutcome" | "isStatus" | "eitherize" | "parseSrtTime" | "formatSrtTime" | "parseSrt" | "createSrt" | "ivk" | "s2l" | "l2s">;
49
49
  export type UtilFunc = typeof UtilFunc;
@@ -2,7 +2,7 @@ import { AnyString, JToken, MPromise, PartialOption, StatusVerifyFn } from "./Ut
2
2
  import http from 'http';
3
3
  import { PromiseRetries } from "./UtilFunctions";
4
4
  import FormData from "form-data";
5
- import { PromiseQueue } from "@zwa73/js-utils";
5
+ import { LogLevel, PromiseQueue } from "@zwa73/js-utils";
6
6
  /**网络请求返回值 */
7
7
  export type RequestResult<T> = {
8
8
  /**响应头 */
@@ -14,6 +14,8 @@ export type RequestResult<T> = {
14
14
  };
15
15
  /**网络请求选项 */
16
16
  export type RequestOption = {
17
+ /**请求的log等级 默认http 不影响警告 */
18
+ logLevel?: LogLevel;
17
19
  /**请求协议 */
18
20
  protocol: 'http:' | 'https:';
19
21
  /**超时时间/毫秒 最小为10_000 默认无限 */
@@ -41,7 +43,7 @@ export type QueryRequestData = NodeJS.Dict<string | number | boolean | readonly
41
43
  /**请求处理函数 需调用req.end() */
42
44
  export type RequestProcFn = (req: http.ClientRequest) => MPromise<void>;
43
45
  /**结果处理函数 */
44
- export type RequestParseFn<D, R> = (result: RequestResult<D> | undefined) => MPromise<R>;
46
+ export type RequestParseFn<D, R> = (result: RequestResult<D> | undefined, opt: RequestOption) => MPromise<R>;
45
47
  /**数据处理函数 */
46
48
  export type RequestReduceFn<T> = (acc: T, data: string) => MPromise<T>;
47
49
  /**数据初始化函数 */
package/dist/UtilHttp.js CHANGED
@@ -16,6 +16,10 @@ const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
16
16
  const js_utils_1 = require("@zwa73/js-utils");
17
17
  const fs_1 = __importDefault(require("fs"));
18
18
  const pathe_1 = __importDefault(require("pathe"));
19
+ const RequestDefOption = {
20
+ logLevel: 'http',
21
+ timeout: 0,
22
+ };
19
23
  const SendNoneProc = {
20
24
  proc: req => void req.end()
21
25
  };
@@ -162,7 +166,7 @@ class UtilHttp {
162
166
  const acceptProc = {
163
167
  init: () => '',
164
168
  reduce: (acc, curr) => acc + curr,
165
- parse: result => {
169
+ parse: (result, opt) => {
166
170
  if (result == undefined) {
167
171
  UtilLogger_1.SLogger.warn(`accept json 接收反馈错误: 响应结果无效`);
168
172
  return undefined;
@@ -174,7 +178,7 @@ class UtilHttp {
174
178
  }
175
179
  try {
176
180
  const obj = JSON.parse(data.trim());
177
- UtilLogger_1.SLogger.http(`accept json 接受信息 data:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }), `result:`, UtilFunctions_1.UtilFunc.stringifyJToken(rest, { compress: true, space: 2 }));
181
+ UtilLogger_1.SLogger.log(opt.logLevel, `accept json 接受信息 data:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }), `result:`, UtilFunctions_1.UtilFunc.stringifyJToken(rest, { compress: true, space: 2 }));
178
182
  return { ...rest, data: obj };
179
183
  }
180
184
  catch (err) {
@@ -352,7 +356,7 @@ class UtilHttp {
352
356
  * @param datas - 数据对象
353
357
  */
354
358
  async once(arg) {
355
- const fullopt = { ...this._data, ...arg.option ?? {} };
359
+ const fullopt = UtilFunctions_1.UtilFunc.mergeOption(RequestDefOption, { ...this._data, ...arg.option ?? {} });
356
360
  const sendProc = typeof this._send === 'function'
357
361
  ? await this._send(fullopt, arg)
358
362
  : this._send;
@@ -360,7 +364,7 @@ class UtilHttp {
360
364
  ? await this._accept(arg)
361
365
  : this._accept;
362
366
  const res = await UtilHttp.request(fullopt, sendProc, acceptProc);
363
- return parse(res);
367
+ return parse(res, fullopt);
364
368
  }
365
369
  /**重复发送网络请求
366
370
  * @param verify - 有效性验证函数
@@ -384,7 +388,7 @@ class UtilHttp {
384
388
  static async request(option, sendProc, acceptProc) {
385
389
  const { reduce: reqReduce, init: reqInit } = acceptProc;
386
390
  const { proc: reqProc } = sendProc;
387
- const { protocol, timeout = 0, ...baseReqOpt } = option;
391
+ const { protocol, logLevel, timeout, ...baseReqOpt } = UtilFunctions_1.UtilFunc.mergeOption(RequestDefOption, option);
388
392
  const plusTimeout = timeout + 1000;
389
393
  const hasTimeLimit = timeout >= 10_000;
390
394
  const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
@@ -241,7 +241,7 @@ class SLogger {
241
241
  timeEnd(flag, level = "info") {
242
242
  const start = SLogger.timeTable[flag];
243
243
  if (start == null) {
244
- this.warn("SLogger.timeEnd 错误 flag:" + flag + " 不存在");
244
+ this.warn(`SLogger.timeEnd 错误 flag:${flag} 不存在`);
245
245
  return;
246
246
  }
247
247
  const timelen = process.hrtime(start);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.255",
3
+ "version": "1.0.257",
4
4
  "description": "my utils",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {