@zwa73/utils 1.0.194 → 1.0.196

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,4 +1,4 @@
1
- import { Keyable, Literal } from "./UtilInterfaces";
1
+ import { Keyable, Literal, MPromise } from "./UtilInterfaces";
2
2
  import Handlebars from 'handlebars';
3
3
  /**函数管道器 */
4
4
  export declare class Piper<Arg, Result = Arg> {
@@ -18,7 +18,7 @@ export declare class Piper<Arg, Result = Arg> {
18
18
  /**直接调用 */
19
19
  invoke(arg: Arg): Result;
20
20
  }
21
- type StreamOperation<T, U> = (item: T) => Promise<U> | U;
21
+ type StreamOperation<T, U> = (item: T) => MPromise<U>;
22
22
  /**并行流 */
23
23
  export declare class Stream<T> implements Iterable<T> {
24
24
  /**并发数*/
@@ -27,8 +27,11 @@ export declare class Stream<T> implements Iterable<T> {
27
27
  /**加工函数列表*/
28
28
  private _operation;
29
29
  private constructor();
30
- /**从arraylike创建流 */
31
- static from<T>(args: Parameters<typeof Array.from<T>>[0], concurrent?: number): Stream<T>;
30
+ /**从arraylike创建流
31
+ * @param args - arraylike
32
+ * @param concurrent - 并发数 默认 1
33
+ */
34
+ static from<T>(args: ArrayLike<T>, concurrent?: number): Stream<T>;
32
35
  /**从数组创建流 */
33
36
  static of<T>(...args: T[]): Stream<T>;
34
37
  /**设置并发数 */
package/dist/UtilClass.js CHANGED
@@ -49,7 +49,10 @@ class Stream {
49
49
  this._list = new Array(...base);
50
50
  this._concurrent = concurrent;
51
51
  }
52
- /**从arraylike创建流 */
52
+ /**从arraylike创建流
53
+ * @param args - arraylike
54
+ * @param concurrent - 并发数 默认 1
55
+ */
53
56
  static from(args, concurrent = 1) {
54
57
  return new Stream(Array.from(args), concurrent);
55
58
  }
@@ -200,6 +203,7 @@ class Stream {
200
203
  }
201
204
  }
202
205
  exports.Stream = Stream;
206
+ //#endregion
203
207
  /**文本模板渲染器
204
208
  * @template T - 上下文对象的类型,默认为记录类型
205
209
  */
package/dist/UtilCom.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { AnyString, JObject, ReqVerifyFn } from "./UtilInterfaces";
1
+ import { AnyString, JToken, MPromise, PartialOption, StatusVerifyFn } from "./UtilInterfaces";
2
2
  import http from 'http';
3
- import { RepeatPromiseOpt, RepeatPromiseResult } from "./UtilFunctions";
3
+ import { RepeatPromiseOpt } from "./UtilFunctions";
4
4
  /**网络请求返回值 */
5
5
  export type ComResp<T> = {
6
6
  /**响应头 */
@@ -11,12 +11,11 @@ export type ComResp<T> = {
11
11
  data: T;
12
12
  };
13
13
  /**网络请求选项 */
14
- export type ComReqOpt = {
14
+ export type ComRequestOption = {
15
15
  /**请求协议 */
16
- protocol: 'http' | 'https';
16
+ protocol: 'http:' | 'https:';
17
17
  /**超时时间/毫秒 最小为10_000 默认无限 */
18
- timeLimit?: number;
19
- } & {
18
+ timeout?: number;
20
19
  /**请求域名 */
21
20
  hostname: string;
22
21
  /**请求路径 */
@@ -35,125 +34,100 @@ export type ComReqOpt = {
35
34
  } & http.RequestOptions;
36
35
  /**get请求所允许的数据 */
37
36
  export type GetReqData = NodeJS.Dict<string | number | boolean | readonly string[] | readonly number[] | readonly boolean[] | null>;
38
- /**网络工具 */
39
- export declare namespace UtilCom {
37
+ /**请求处理函数 需调用req.end() */
38
+ export type ProcReqFn = ((req: http.ClientRequest) => MPromise<void>);
39
+ /**数据处理函数 */
40
+ export type ReduceDataFn<T> = (acc: T, data: string) => MPromise<T>;
41
+ declare const AcceptTypeList: readonly ["json"];
42
+ /**可用的接受类型 */
43
+ type AcceptType = typeof AcceptTypeList[number];
44
+ declare const SendTypeList: readonly ["json", "none"];
45
+ /**可用的发送类型 */
46
+ type SendType = typeof SendTypeList[number];
47
+ /**accept处理数据 */
48
+ type AcceptProc<D, T> = {
49
+ init: D;
50
+ reduce: ReduceDataFn<D>;
51
+ parse: (result: ComResp<D> | undefined) => MPromise<T>;
52
+ };
53
+ /**send处理数据 */
54
+ type SendProc<T extends any[]> = {
55
+ proc: (opt: ComRequestOption, ...args: T) => MPromise<ProcReqFn>;
56
+ };
57
+ /**send处理的参数 */
58
+ type SendParams<T extends SendProc<any>> = T extends SendProc<infer T> ? T : never;
59
+ /**accept处理的结果 */
60
+ type ParseResult<D extends AcceptProc<any, any>> = D extends AcceptProc<any, infer T> ? Awaited<T> : never;
61
+ /**根据方式自动推断json请求的可用数据类型 */
62
+ type JsonType<OPT extends Record<string, any>> = OPT['method'] extends "POST" ? JToken : GetReqData;
63
+ /**网络请求工具 */
64
+ export declare class UtilCom<D extends Partial<ComRequestOption>, S extends SendProc<any>, A extends AcceptProc<any, any>> {
65
+ private _data;
66
+ private _send;
67
+ private _accept;
68
+ private constructor();
69
+ /**设为https请求 */
70
+ static https(): UtilCom<{
71
+ readonly protocol: "https:";
72
+ }, {
73
+ proc: () => (req: http.ClientRequest) => undefined;
74
+ }, {
75
+ init: string;
76
+ reduce: (acc: string, dat: string) => string;
77
+ parse: (result: ComResp<string> | undefined) => undefined;
78
+ }>;
79
+ /**设为http请求 */
80
+ static http(): UtilCom<{
81
+ readonly protocol: "http:";
82
+ }, {
83
+ proc: () => (req: http.ClientRequest) => undefined;
84
+ }, {
85
+ init: string;
86
+ reduce: (acc: string, dat: string) => string;
87
+ parse: (result: ComResp<string> | undefined) => undefined;
88
+ }>;
89
+ /**设为get方式的请求 */
90
+ get(): UtilCom<D & {
91
+ readonly method: "GET";
92
+ }, S, A>;
93
+ /**设为Post方式的请求 */
94
+ post(): UtilCom<D & {
95
+ readonly method: "POST";
96
+ }, S, A>;
97
+ /**补充参数 */
98
+ option<OPT extends Partial<ComRequestOption>>(opt: OPT): UtilCom<D & OPT, S, A>;
99
+ /**接收数据类型*/
100
+ accept<T extends AcceptType>(t: T): {
101
+ readonly json: UtilCom<D, S, AcceptProc<string, JToken>>;
102
+ readonly log: UtilCom<D, S, AcceptProc<string, string>>;
103
+ }[T];
104
+ acceptJson(): UtilCom<D, S, AcceptProc<string, JToken>>;
105
+ acceptLog(): UtilCom<D, S, AcceptProc<string, string>>;
106
+ /**发送数据类型*/
107
+ send<T extends SendType>(t: T): {
108
+ readonly json: UtilCom<D, SendProc<[JsonType<D>]>, A>;
109
+ readonly none: UtilCom<D, SendProc<[]>, A>;
110
+ }[T];
111
+ sendJson(): UtilCom<D, SendProc<[JsonType<D>]>, A>;
112
+ sendNone(): UtilCom<D, SendProc<[]>, A>;
113
+ /**发送请求
114
+ * @param option - 网络请求选项
115
+ * @param datas - 数据对象
116
+ */
117
+ once(option: PartialOption<ComRequestOption, D>, ...datas: SendParams<S>): Promise<any>;
118
+ /**重复发送网络请求
119
+ * @param option - 网络请求选项
120
+ * @param verifyFn - 有效性验证函数
121
+ * @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
122
+ * @param datas - 数据对象
123
+ */
124
+ repeat(option: PartialOption<ComRequestOption, D>, verifyFn?: StatusVerifyFn<ParseResult<A>>, repeatOpt?: RepeatPromiseOpt, ...datas: SendParams<S>): Promise<import("./UtilFunctions").RepeatPromiseResult<ParseResult<A>>>;
40
125
  /**网络请求
41
126
  * @param comReqOpt - 网络请求选项
42
127
  * @param procReq - 请求处理函数 需调用req.end()
43
128
  * @param reduceData - 数据处理函数
44
129
  * @param initData - 初始数据
45
130
  */
46
- function comReq<T>(comReqOpt: ComReqOpt, procReq: ((req: http.ClientRequest) => void | Promise<void>), reduceData: (acc: T, data: string) => T, initData: T): Promise<ComResp<T> | undefined>;
47
- /**发送一个 https POST 请求并接受数据
48
- * @async
49
- * @param comReqOpt - 请求参数
50
- * @param reqData - 数据对象
51
- * @returns 结果 undefined 为未能成功接收
52
- */
53
- function httpsPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: JObject): Promise<{
54
- data: JObject;
55
- /**响应头 */
56
- headers: http.IncomingHttpHeaders;
57
- /**响应状态码 */
58
- statusCode?: number;
59
- } | undefined>;
60
- /**发送一个 http POST 请求并接受数据
61
- * @async
62
- * @param comReqOpt - 请求参数
63
- * @param reqData - 数据对象
64
- * @returns 结果 undefined 为未能成功接收
65
- */
66
- function httpPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: JObject): Promise<{
67
- data: JObject;
68
- /**响应头 */
69
- headers: http.IncomingHttpHeaders;
70
- /**响应状态码 */
71
- statusCode?: number;
72
- } | undefined>;
73
- /**发送一个 https GET 请求并接受数据
74
- * @async
75
- * @param comReqOpt - 请求参数
76
- * @param reqData - 数据对象
77
- * @returns 结果 undefined 为未能成功接收
78
- */
79
- function httpsGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: GetReqData): Promise<{
80
- data: JObject;
81
- /**响应头 */
82
- headers: http.IncomingHttpHeaders;
83
- /**响应状态码 */
84
- statusCode?: number;
85
- } | undefined>;
86
- /**发送一个 http GET 请求并接受数据
87
- * @async
88
- * @param comReqOpt - 请求参数
89
- * @param reqData - 数据对象
90
- * @returns 结果 undefined 为未能成功接收
91
- */
92
- function httpGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: GetReqData): Promise<{
93
- data: JObject;
94
- /**响应头 */
95
- headers: http.IncomingHttpHeaders;
96
- /**响应状态码 */
97
- statusCode?: number;
98
- } | undefined>;
99
- /**重复一个 https POST请求并接受数据
100
- * @async
101
- * @param comReqOpt - 网络请求选项
102
- * @param reqData - 数据对象
103
- * @param verifyFn - 有效性验证函数
104
- * @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
105
- * @returns 结果 undefined 为未能成功接收
106
- */
107
- function httpsRepeatPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: JObject, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
108
- data: JObject;
109
- /**响应头 */
110
- headers: http.IncomingHttpHeaders;
111
- /**响应状态码 */
112
- statusCode?: number;
113
- } | undefined>>;
114
- /**重复一个 http POST请求并接受数据
115
- * @async
116
- * @param comReqOpt - 网络请求选项
117
- * @param reqData - 数据对象
118
- * @param verifyFn - 有效性验证函数
119
- * @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
120
- * @returns 结果 undefined 为未能成功接收
121
- */
122
- function httpRepeatPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: JObject, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
123
- data: JObject;
124
- /**响应头 */
125
- headers: http.IncomingHttpHeaders;
126
- /**响应状态码 */
127
- statusCode?: number;
128
- } | undefined>>;
129
- /**重复一个 https GET 请求并接受数据
130
- * @async
131
- * @param comReqOpt - 网络请求选项
132
- * @param reqData - 数据对象
133
- * @param verifyFn - 有效性验证函数
134
- * @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
135
- * @returns 结果 undefined 为未能成功接收
136
- */
137
- function httpsRepeatGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: GetReqData, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
138
- data: JObject;
139
- /**响应头 */
140
- headers: http.IncomingHttpHeaders;
141
- /**响应状态码 */
142
- statusCode?: number;
143
- } | undefined>>;
144
- /**重复一个 http GET 请求并接受数据
145
- * @async
146
- * @param comReqOpt - 网络请求选项
147
- * @param reqData - 数据对象
148
- * @param verifyFn - 有效性验证函数
149
- * @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
150
- * @returns 结果 undefined 为未能成功接收
151
- */
152
- function httpRepeatGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: GetReqData, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
153
- data: JObject;
154
- /**响应头 */
155
- headers: http.IncomingHttpHeaders;
156
- /**响应状态码 */
157
- statusCode?: number;
158
- } | undefined>>;
131
+ static comReq<T>(comReqOpt: ComRequestOption, procReq: ProcReqFn, reduceData: ReduceDataFn<T>, initData: T): Promise<ComResp<T> | undefined>;
159
132
  }
133
+ export {};