@zwa73/utils 1.0.218 → 1.0.219

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 +1 @@
1
- export { Piper, Stream, Hbs, BridgeInterface, Bridge, SmartCache, DListMiddleNode, DLinkedList, PromoseQueueOption, PromiseQueue } from "@zwa73/js-utils";
1
+ export { BridgeInterface, Bridge, Hbs, DListMiddleNode, DListHeadNode, DListTailNode, DListNode, DListMaybeNode, DLinkedList, Piper, PromoseQueueOption, PromiseQueue, SmartCache, Stream } from "@zwa73/js-utils";
package/dist/UtilClass.js CHANGED
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PromiseQueue = exports.DLinkedList = exports.SmartCache = exports.Bridge = exports.Hbs = exports.Stream = exports.Piper = void 0;
3
+ exports.Stream = exports.SmartCache = exports.PromiseQueue = exports.Piper = exports.DLinkedList = exports.Hbs = exports.Bridge = void 0;
4
4
  //#region UtilClass转导
5
5
  var js_utils_1 = require("@zwa73/js-utils");
6
- Object.defineProperty(exports, "Piper", { enumerable: true, get: function () { return js_utils_1.Piper; } });
7
- Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return js_utils_1.Stream; } });
8
- Object.defineProperty(exports, "Hbs", { enumerable: true, get: function () { return js_utils_1.Hbs; } });
9
6
  Object.defineProperty(exports, "Bridge", { enumerable: true, get: function () { return js_utils_1.Bridge; } });
10
- Object.defineProperty(exports, "SmartCache", { enumerable: true, get: function () { return js_utils_1.SmartCache; } });
7
+ Object.defineProperty(exports, "Hbs", { enumerable: true, get: function () { return js_utils_1.Hbs; } });
11
8
  Object.defineProperty(exports, "DLinkedList", { enumerable: true, get: function () { return js_utils_1.DLinkedList; } });
9
+ Object.defineProperty(exports, "Piper", { enumerable: true, get: function () { return js_utils_1.Piper; } });
12
10
  Object.defineProperty(exports, "PromiseQueue", { enumerable: true, get: function () { return js_utils_1.PromiseQueue; } });
11
+ Object.defineProperty(exports, "SmartCache", { enumerable: true, get: function () { return js_utils_1.SmartCache; } });
12
+ Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return js_utils_1.Stream; } });
13
13
  //#endregion
@@ -2,6 +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 { RequiredOnly } from "@zwa73/js-utils";
5
6
  /**网络请求返回值 */
6
7
  export type RequestResult<T> = {
7
8
  /**响应头 */
@@ -64,48 +65,64 @@ declare const AcceptNoneProc: AcceptProc<undefined, undefined>;
64
65
  type SendParams<P extends SendProc<any>> = P extends SendProc<infer T> ? T : never;
65
66
  /**accept处理的结果 */
66
67
  type ParseResult<P extends AcceptProc<any, any>> = P extends AcceptProc<any, infer T> ? Awaited<T> : never;
67
- /**网络请求工具 */
68
- export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<RequestOption, 'protocol'>>, S extends SendProc<any>, A extends AcceptProc<any, any>> {
68
+ /**http请求工具 */
69
+ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<RequestOption, 'protocol'>>, S extends SendProc<any>, A extends AcceptProc<any, any>> {
69
70
  private _data;
70
71
  private _send;
71
72
  private _accept;
72
73
  private constructor();
73
74
  /**设为https请求 */
74
- static https(): UtilCom<{
75
+ static https(): UtilHttp<{
75
76
  readonly protocol: "https:";
76
77
  }, SendProc<[]>, AcceptProc<string, RequestResult<string> | undefined>>;
77
78
  /**设为http请求 */
78
- static http(): UtilCom<{
79
+ static http(): UtilHttp<{
79
80
  readonly protocol: "http:";
80
81
  }, SendProc<[]>, AcceptProc<string, RequestResult<string> | undefined>>;
82
+ /**从url创建 */
83
+ static url(urlStr: `${'http:' | 'https:'}//${string}`): UtilHttp<{
84
+ protocol: "http:" | "https:";
85
+ } & {
86
+ hostname: string;
87
+ path: string;
88
+ port: number | undefined;
89
+ }, SendProc<[]>, AcceptProc<string, RequestResult<string> | undefined>>;
81
90
  /**设为get方式的请求 */
82
- get(): UtilCom<D & {
91
+ get(): UtilHttp<D & {
83
92
  method: "GET";
84
93
  }, S, A>;
85
94
  /**设为Post方式的请求 */
86
- post(): UtilCom<D & {
95
+ post(): UtilHttp<D & {
87
96
  method: "POST";
88
97
  }, S, A>;
89
98
  /**补充参数
99
+ * 不会检查必要参数完整性
100
+ * 将会替换对应字段, 修改headers请用header函数
101
+ */
102
+ option<OPT extends Partial<RequestOption>>(option: OPT): UtilHttp<D & OPT, S, A>;
103
+ /**完成参数
104
+ * 会检查必要参数完整性
90
105
  * 将会替换对应字段, 修改headers请用header函数
91
106
  */
92
- option<OPT extends Partial<RequestOption>>(option: OPT): UtilCom<D & OPT, S, A>;
107
+ finalize<OPT extends PartialOption<RequestOption, D>>(option: OPT): UtilHttp<D & OPT, S, A>;
93
108
  /**补充header */
94
- header<HAD extends http.OutgoingHttpHeaders>(headers: HAD): UtilCom<D & {
109
+ header<HAD extends http.OutgoingHttpHeaders>(headers: HAD): UtilHttp<D & {
95
110
  headers: HAD;
96
111
  }, S, A>;
97
112
  /**设置agent */
98
113
  proxyAgent(url: string): this;
99
114
  /**添加一段query */
100
115
  query(data: QueryRequestData): this;
116
+ /**克隆 */
117
+ clone(): UtilHttp<D, S, A>;
101
118
  /**收发皆为json的预设 */
102
- json(): UtilCom<D & {
119
+ json(): UtilHttp<D & {
103
120
  headers: {
104
121
  "Content-Type": "application/json";
105
122
  };
106
123
  }, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
107
124
  /**收发皆为json的post预设 */
108
- postJson(): UtilCom<D & {
125
+ postJson(): UtilHttp<D & {
109
126
  method: "POST";
110
127
  } & {
111
128
  headers: {
@@ -113,15 +130,15 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
113
130
  };
114
131
  }, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
115
132
  /**无查询参数获取json的get预设 */
116
- getJson(): UtilCom<D & {
133
+ getJson(): UtilHttp<D & {
117
134
  method: "GET";
118
135
  }, SendProc<[]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
119
136
  /**有查询参数获取json的get预设 */
120
- queryJson(): UtilCom<D & {
137
+ queryJson(): UtilHttp<D & {
121
138
  method: "GET";
122
139
  }, SendProc<[QueryRequestData]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
123
140
  /**收发皆为json的https-post预设 */
124
- static httpsPostJson(): UtilCom<{
141
+ static httpsPostJson(): UtilHttp<{
125
142
  readonly protocol: "https:";
126
143
  } & {
127
144
  method: "POST";
@@ -131,7 +148,7 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
131
148
  };
132
149
  }, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
133
150
  /**收发皆为json的http-post预设 */
134
- static httpPostJson(): UtilCom<{
151
+ static httpPostJson(): UtilHttp<{
135
152
  readonly protocol: "http:";
136
153
  } & {
137
154
  method: "POST";
@@ -141,76 +158,75 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
141
158
  };
142
159
  }, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
143
160
  /**无查询参数获取json的https-get预设 */
144
- static httpsGetJson(): UtilCom<{
161
+ static httpsGetJson(): UtilHttp<{
145
162
  readonly protocol: "https:";
146
163
  } & {
147
164
  method: "GET";
148
165
  }, SendProc<[]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
149
166
  /**有查询参数获取json的https-get预设 */
150
- static httpsQueryJson(): UtilCom<{
167
+ static httpsQueryJson(): UtilHttp<{
151
168
  readonly protocol: "http:";
152
169
  } & {
153
170
  method: "GET";
154
171
  }, SendProc<[QueryRequestData]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
155
172
  /**无查询参数获取json的http-get预设 */
156
- static httpGetJson(): UtilCom<{
173
+ static httpGetJson(): UtilHttp<{
157
174
  readonly protocol: "http:";
158
175
  } & {
159
176
  method: "GET";
160
177
  }, SendProc<[]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
161
178
  /**有查询参数获取json的http-get预设 */
162
- static httpQueryJson(): UtilCom<{
179
+ static httpQueryJson(): UtilHttp<{
163
180
  readonly protocol: "http:";
164
181
  } & {
165
182
  method: "GET";
166
183
  }, SendProc<[QueryRequestData]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
167
184
  /**预设的接收数据类型*/
168
185
  accept<T extends AcceptType>(t: T): {
169
- readonly json: UtilCom<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
170
- readonly string: UtilCom<D, S, AcceptProc<string, RequestResult<string> | undefined>>;
171
- readonly none: UtilCom<D, S, AcceptProc<undefined, undefined>>;
186
+ readonly json: UtilHttp<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
187
+ readonly string: UtilHttp<D, S, AcceptProc<string, RequestResult<string> | undefined>>;
188
+ readonly none: UtilHttp<D, S, AcceptProc<undefined, undefined>>;
172
189
  }[T];
173
- acceptJson(): UtilCom<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
174
- acceptString(): UtilCom<D, S, typeof AcceptStringProc>;
175
- acceptNone(): UtilCom<D, S, typeof AcceptNoneProc>;
190
+ acceptJson(): UtilHttp<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
191
+ acceptString(): UtilHttp<D, S, typeof AcceptStringProc>;
192
+ acceptNone(): UtilHttp<D, S, typeof AcceptNoneProc>;
176
193
  /**自定的接收数据类型*/
177
- acceptRaw<AD, AT>(proc: AcceptProc<AD, AT>): UtilCom<D, S, typeof proc>;
194
+ acceptRaw<AD, AT>(proc: AcceptProc<AD, AT>): UtilHttp<D, S, typeof proc>;
178
195
  /**预设的发送数据类型*/
179
196
  send<T extends SendType>(t: T): {
180
- readonly json: UtilCom<D & {
197
+ readonly json: UtilHttp<D & {
181
198
  headers: {
182
199
  "Content-Type": "application/json";
183
200
  };
184
201
  }, SendProc<[JToken]>, A>;
185
- readonly query: UtilCom<D, SendProc<[QueryRequestData]>, A>;
186
- readonly formData: UtilCom<D & {
202
+ readonly query: UtilHttp<D, SendProc<[QueryRequestData]>, A>;
203
+ readonly formData: UtilHttp<D & {
187
204
  headers: {
188
205
  "Content-Type": "multipart/form-data";
189
206
  };
190
207
  }, SendProc<[FormData]>, A>;
191
- readonly none: UtilCom<D, SendProc<[]>, A>;
208
+ readonly none: UtilHttp<D, SendProc<[]>, A>;
192
209
  }[T];
193
210
  /**利用 req.write 发送一段json */
194
- sendJson(): UtilCom<D & {
211
+ sendJson(): UtilHttp<D & {
195
212
  headers: {
196
213
  "Content-Type": "application/json";
197
214
  };
198
215
  }, SendProc<[JToken]>, A>;
199
216
  /**利用 appendQuery 直接将数据附加在path上发送请求 */
200
- sendQuery(): UtilCom<D, SendProc<[QueryRequestData]>, A>;
201
- sendFormData(): UtilCom<D & {
217
+ sendQuery(): UtilHttp<D, SendProc<[QueryRequestData]>, A>;
218
+ sendFormData(): UtilHttp<D & {
202
219
  headers: {
203
220
  "Content-Type": "multipart/form-data";
204
221
  };
205
222
  }, SendProc<[FormData]>, A>;
206
- sendNone(): UtilCom<D, typeof SendNoneProc, A>;
223
+ sendNone(): UtilHttp<D, typeof SendNoneProc, A>;
207
224
  /**自定的发送数据类型*/
208
- sendRaw<T extends any[]>(proc: SendProc<T>): UtilCom<D, typeof proc, A>;
225
+ sendRaw<T extends any[]>(proc: SendProc<T>): UtilHttp<D, typeof proc, A>;
209
226
  /**发送请求
210
- * @param option - 网络请求选项
211
227
  * @param datas - 数据对象
212
228
  */
213
- once(option: PartialOption<RequestOption, D>, ...datas: SendParams<S>): Promise<ParseResult<A>>;
229
+ once(...datas: {} extends RequiredOnly<PartialOption<RequestOption, D>> ? SendParams<S> : [Error & "RequestOption不完整, 请使用 finalize 完成必要项"]): Promise<ParseResult<A>>;
214
230
  /**重复发送网络请求
215
231
  * @param option - 网络请求选项
216
232
  * @param verify - 有效性验证函数
@@ -218,10 +234,9 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
218
234
  * @param datas - 数据对象
219
235
  */
220
236
  retry(opt: {
221
- option: PartialOption<RequestOption, D>;
222
237
  verify?: StatusVerifyFn<ParseResult<A>>;
223
238
  retries?: PromiseRetries;
224
- }, ...datas: SendParams<S>): Promise<import("@zwa73/js-utils").PromiseRetryResult<ParseResult<A>>>;
239
+ }, ...datas: {} extends RequiredOnly<PartialOption<RequestOption, D>> ? SendParams<S> : [Error & "RequestOption不完整, 请使用 finalize 完成必要项"]): Promise<import("@zwa73/js-utils").PromiseRetryResult<ParseResult<A>>>;
225
240
  /**发送网络请求
226
241
  * @param option - 网络请求选项
227
242
  * @param proc - 请求处理函数 需调用req.end()
@@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.UtilCom = void 0;
6
+ exports.UtilHttp = void 0;
7
7
  const http_1 = __importDefault(require("http"));
8
8
  const https_1 = __importDefault(require("https"));
9
+ const url_1 = require("url");
9
10
  const UtilLogger_1 = require("./UtilLogger");
10
11
  const UtilFunctions_1 = require("./UtilFunctions");
11
12
  const querystring_1 = __importDefault(require("querystring"));
@@ -27,8 +28,8 @@ const AcceptNoneProc = {
27
28
  reduce: () => undefined,
28
29
  parse: () => undefined
29
30
  };
30
- /**网络请求工具 */
31
- class UtilCom {
31
+ /**http请求工具 */
32
+ class UtilHttp {
32
33
  _data;
33
34
  _send;
34
35
  _accept;
@@ -40,11 +41,23 @@ class UtilCom {
40
41
  //#region 流式创建
41
42
  /**设为https请求 */
42
43
  static https() {
43
- return new UtilCom({ protocol: 'https:' }, SendNoneProc, AcceptStringProc);
44
+ return new UtilHttp({ protocol: 'https:' }, SendNoneProc, AcceptStringProc);
44
45
  }
45
46
  /**设为http请求 */
46
47
  static http() {
47
- return new UtilCom({ protocol: 'http:' }, SendNoneProc, AcceptStringProc);
48
+ return new UtilHttp({ protocol: 'http:' }, SendNoneProc, AcceptStringProc);
49
+ }
50
+ /**从url创建 */
51
+ static url(urlStr) {
52
+ const { protocol, hostname, port, pathname } = new url_1.URL(urlStr);
53
+ if (!['http:', 'https:'].includes(protocol))
54
+ UtilFunctions_1.UtilFunc.throwError(`url协议错误: ${urlStr}`);
55
+ const req = new UtilHttp({ protocol: protocol }, SendNoneProc, AcceptStringProc);
56
+ return req.option({
57
+ hostname,
58
+ path: pathname,
59
+ port: port ? parseInt(port) : undefined,
60
+ });
48
61
  }
49
62
  /**设为get方式的请求 */
50
63
  get() {
@@ -57,12 +70,21 @@ class UtilCom {
57
70
  return this;
58
71
  }
59
72
  /**补充参数
73
+ * 不会检查必要参数完整性
60
74
  * 将会替换对应字段, 修改headers请用header函数
61
75
  */
62
76
  option(option) {
63
77
  this._data = { ...this._data, ...option };
64
78
  return this;
65
79
  }
80
+ /**完成参数
81
+ * 会检查必要参数完整性
82
+ * 将会替换对应字段, 修改headers请用header函数
83
+ */
84
+ finalize(option) {
85
+ this._data = { ...this._data, ...option };
86
+ return this;
87
+ }
66
88
  /**补充header */
67
89
  header(headers) {
68
90
  this._data.headers = {
@@ -81,9 +103,13 @@ class UtilCom {
81
103
  }
82
104
  /**添加一段query */
83
105
  query(data) {
84
- this._data.path = UtilCom.buildQuery(this._data.path ?? '', data);
106
+ this._data.path = UtilHttp.buildQuery(this._data.path ?? '', data);
85
107
  return this;
86
108
  }
109
+ /**克隆 */
110
+ clone() {
111
+ return new UtilHttp({ ...this._data }, this._send, this._accept);
112
+ }
87
113
  //#endregion
88
114
  //#region 快速预设
89
115
  /**收发皆为json的预设 */
@@ -104,27 +130,27 @@ class UtilCom {
104
130
  }
105
131
  /**收发皆为json的https-post预设 */
106
132
  static httpsPostJson() {
107
- return UtilCom.https().postJson();
133
+ return UtilHttp.https().postJson();
108
134
  }
109
135
  /**收发皆为json的http-post预设 */
110
136
  static httpPostJson() {
111
- return UtilCom.http().postJson();
137
+ return UtilHttp.http().postJson();
112
138
  }
113
139
  /**无查询参数获取json的https-get预设 */
114
140
  static httpsGetJson() {
115
- return UtilCom.https().getJson();
141
+ return UtilHttp.https().getJson();
116
142
  }
117
143
  /**有查询参数获取json的https-get预设 */
118
144
  static httpsQueryJson() {
119
- return UtilCom.http().queryJson();
145
+ return UtilHttp.http().queryJson();
120
146
  }
121
147
  /**无查询参数获取json的http-get预设 */
122
148
  static httpGetJson() {
123
- return UtilCom.http().getJson();
149
+ return UtilHttp.http().getJson();
124
150
  }
125
151
  /**有查询参数获取json的http-get预设 */
126
152
  static httpQueryJson() {
127
- return UtilCom.http().queryJson();
153
+ return UtilHttp.http().queryJson();
128
154
  }
129
155
  //#endregion
130
156
  //#region 接收数据类型
@@ -215,7 +241,7 @@ class UtilCom {
215
241
  sendQuery() {
216
242
  const proc = {
217
243
  proc: (opt, reqData) => {
218
- opt.path = UtilCom.buildQuery(opt.path ?? '', reqData);
244
+ opt.path = UtilHttp.buildQuery(opt.path ?? '', reqData);
219
245
  const procReq = (req) => void req.end();
220
246
  return procReq;
221
247
  }
@@ -250,14 +276,13 @@ class UtilCom {
250
276
  }
251
277
  //#endregion
252
278
  /**发送请求
253
- * @param option - 网络请求选项
254
279
  * @param datas - 数据对象
255
280
  */
256
- async once(option, ...datas) {
257
- const fullopt = Object.assign({}, this._data, option);
281
+ async once(...datas) {
282
+ const fullopt = this._data;
258
283
  const proc = await this._send.proc(fullopt, ...datas);
259
284
  const { reduce, init, parse } = this._accept;
260
- const res = await UtilCom.request(fullopt, proc, reduce, init);
285
+ const res = await UtilHttp.request(fullopt, proc, reduce, init);
261
286
  return parse(res);
262
287
  }
263
288
  /**重复发送网络请求
@@ -267,10 +292,10 @@ class UtilCom {
267
292
  * @param datas - 数据对象
268
293
  */
269
294
  async retry(opt, ...datas) {
270
- let { option, retries, verify } = opt;
295
+ let { retries, verify } = opt;
271
296
  retries ??= {};
272
297
  retries.tryDelay = retries.tryDelay ?? 1000;
273
- const procFn = async () => this.once(option, ...datas);
298
+ const procFn = async () => this.once(...datas);
274
299
  return UtilFunctions_1.UtilFunc.retryPromise(procFn, verify, retries);
275
300
  }
276
301
  /**发送网络请求
@@ -359,14 +384,13 @@ class UtilCom {
359
384
  return base;
360
385
  }
361
386
  }
362
- exports.UtilCom = UtilCom;
387
+ exports.UtilHttp = UtilHttp;
363
388
  if (false)
364
389
  void ((async () => {
365
- const t = await UtilCom.https().postJson()
366
- .once({
367
- hostname: 'httpbin.org',
368
- path: '/post',
369
- timeout: 10000
370
- }, { test: 1 });
390
+ const t = await UtilHttp
391
+ .url('https://httpbin.org/post')
392
+ .postJson()
393
+ .finalize({ timeout: 10000 })
394
+ .once({ test: 1 });
371
395
  console.log(t);
372
396
  })());
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export * from './UtilFunctions';
2
2
  export * from './UtilInterfaces';
3
3
  export * from './UtilSymbol';
4
4
  export * from './UtilClass';
5
- export * from './UtilCom';
5
+ export * from './UtilHttp';
6
6
  export * from './UtilCodecs';
7
7
  export * from './UtilDecorators';
8
8
  export * from './UtilFileTools';
package/dist/index.js CHANGED
@@ -35,7 +35,7 @@ __exportStar(require("./UtilFunctions"), exports);
35
35
  __exportStar(require("./UtilInterfaces"), exports);
36
36
  __exportStar(require("./UtilSymbol"), exports);
37
37
  __exportStar(require("./UtilClass"), exports);
38
- __exportStar(require("./UtilCom"), exports);
38
+ __exportStar(require("./UtilHttp"), exports);
39
39
  __exportStar(require("./UtilCodecs"), exports);
40
40
  __exportStar(require("./UtilDecorators"), exports);
41
41
  __exportStar(require("./UtilFileTools"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.218",
3
+ "version": "1.0.219",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {