@zwa73/utils 1.0.198 → 1.0.200
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/dist/UtilCom.d.ts +150 -61
- package/dist/UtilCom.js +178 -86
- package/dist/UtilFileTools.d.ts +0 -1
- package/dist/UtilFileTools.js +0 -1
- package/dist/UtilFunctions.d.ts +11 -7
- package/dist/UtilFunctions.js +27 -23
- package/dist/UtilInterfaces.d.ts +7 -7
- package/package.json +2 -1
- package/src/UtilCom.macro.ts +28 -0
- package/src/UtilCom.ts +227 -124
- package/src/UtilFileTools.ts +0 -1
- package/src/UtilFunctions.ts +39 -27
- package/src/UtilInterfaces.ts +9 -11
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { AnyString, JToken, MPromise, PartialOption, StatusVerifyFn } from "./UtilInterfaces";
|
|
2
2
|
import http from 'http';
|
|
3
|
-
import {
|
|
3
|
+
import { PromiseRetries } from "./UtilFunctions";
|
|
4
|
+
import FormData from "form-data";
|
|
4
5
|
/**网络请求返回值 */
|
|
5
|
-
export type
|
|
6
|
+
export type RequestResult<T> = {
|
|
6
7
|
/**响应头 */
|
|
7
8
|
headers: http.IncomingHttpHeaders;
|
|
8
9
|
/**响应状态码 */
|
|
@@ -11,7 +12,7 @@ export type ComResp<T> = {
|
|
|
11
12
|
data: T;
|
|
12
13
|
};
|
|
13
14
|
/**网络请求选项 */
|
|
14
|
-
export type
|
|
15
|
+
export type RequestOption = {
|
|
15
16
|
/**请求协议 */
|
|
16
17
|
protocol: 'http:' | 'https:';
|
|
17
18
|
/**超时时间/毫秒 最小为10_000 默认无限 */
|
|
@@ -27,41 +28,41 @@ export type ComRequestOption = {
|
|
|
27
28
|
/**请求头 */
|
|
28
29
|
headers?: {
|
|
29
30
|
/**内容类型 */
|
|
30
|
-
'Content-Type'?: 'application/json' | AnyString;
|
|
31
|
+
'Content-Type'?: 'application/json' | 'multipart/form-data' | AnyString;
|
|
31
32
|
/**内容长度 一般无需填写 应为buffer长度而非字符串长度 */
|
|
32
33
|
'Content-Length'?: number;
|
|
33
34
|
};
|
|
34
35
|
} & http.RequestOptions;
|
|
35
|
-
/**
|
|
36
|
-
export type
|
|
36
|
+
/**getquery请求所允许的数据 */
|
|
37
|
+
export type QueryRequestData = NodeJS.Dict<string | number | boolean | readonly string[] | readonly number[] | readonly boolean[] | null>;
|
|
37
38
|
/**请求处理函数 需调用req.end() */
|
|
38
|
-
export type
|
|
39
|
+
export type RequestProcFn = ((req: http.ClientRequest) => MPromise<void>);
|
|
39
40
|
/**数据处理函数 */
|
|
40
|
-
export type
|
|
41
|
-
declare const AcceptTypeList: readonly ["json"];
|
|
41
|
+
export type RequestReduceFn<T> = (acc: T, data: string) => MPromise<T>;
|
|
42
|
+
declare const AcceptTypeList: readonly ["json", "string"];
|
|
42
43
|
/**可用的接受类型 */
|
|
43
44
|
type AcceptType = typeof AcceptTypeList[number];
|
|
44
|
-
declare const SendTypeList: readonly ["json", "none"];
|
|
45
|
+
declare const SendTypeList: readonly ["json", "query", "formData", "none"];
|
|
45
46
|
/**可用的发送类型 */
|
|
46
47
|
type SendType = typeof SendTypeList[number];
|
|
47
48
|
/**accept处理数据 */
|
|
48
49
|
type AcceptProc<D, T> = {
|
|
49
50
|
init: D;
|
|
50
|
-
reduce:
|
|
51
|
-
parse: (result:
|
|
51
|
+
reduce: RequestReduceFn<D>;
|
|
52
|
+
parse: (result: RequestResult<D> | undefined) => MPromise<T>;
|
|
52
53
|
};
|
|
53
54
|
/**send处理数据 */
|
|
54
55
|
type SendProc<T extends any[]> = {
|
|
55
|
-
proc: (opt:
|
|
56
|
+
proc: (opt: RequestOption, ...args: T) => MPromise<RequestProcFn>;
|
|
56
57
|
};
|
|
58
|
+
declare const SendNoneProc: SendProc<[]>;
|
|
59
|
+
declare const AcceptStringProc: AcceptProc<string, RequestResult<string> | undefined>;
|
|
57
60
|
/**send处理的参数 */
|
|
58
61
|
type SendParams<T extends SendProc<any>> = T extends SendProc<infer T> ? T : never;
|
|
59
62
|
/**accept处理的结果 */
|
|
60
63
|
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
|
/**网络请求工具 */
|
|
64
|
-
export declare class UtilCom<D extends Partial<
|
|
65
|
+
export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<RequestOption, 'protocol'>>, S extends SendProc<any>, A extends AcceptProc<any, any>> {
|
|
65
66
|
private _data;
|
|
66
67
|
private _send;
|
|
67
68
|
private _accept;
|
|
@@ -69,73 +70,161 @@ export declare class UtilCom<D extends Partial<ComRequestOption>, S extends Send
|
|
|
69
70
|
/**设为https请求 */
|
|
70
71
|
static https(): UtilCom<{
|
|
71
72
|
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
|
-
}>;
|
|
73
|
+
}, SendProc<[]>, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
79
74
|
/**设为http请求 */
|
|
80
75
|
static http(): UtilCom<{
|
|
81
76
|
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
|
-
}>;
|
|
77
|
+
}, SendProc<[]>, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
89
78
|
/**设为get方式的请求 */
|
|
90
79
|
get(): UtilCom<D & {
|
|
91
|
-
|
|
80
|
+
method: "GET";
|
|
92
81
|
}, S, A>;
|
|
93
82
|
/**设为Post方式的请求 */
|
|
94
83
|
post(): UtilCom<D & {
|
|
95
|
-
|
|
84
|
+
method: "POST";
|
|
96
85
|
}, S, A>;
|
|
97
|
-
/**补充参数
|
|
98
|
-
|
|
86
|
+
/**补充参数
|
|
87
|
+
* 将会替换对应字段, 修改headers请用header函数
|
|
88
|
+
*/
|
|
89
|
+
option<OPT extends Partial<RequestOption>>(option: OPT): UtilCom<D & OPT, S, A>;
|
|
90
|
+
/**补充header */
|
|
91
|
+
header<HAD extends http.OutgoingHttpHeaders>(headers: HAD): UtilCom<D & {
|
|
92
|
+
headers: HAD;
|
|
93
|
+
}, S, A>;
|
|
94
|
+
/**设置agent */
|
|
95
|
+
proxyAgent(url: string): this;
|
|
96
|
+
/**添加一段query */
|
|
97
|
+
query(data: QueryRequestData): this;
|
|
98
|
+
/**收发皆为json的预设 */
|
|
99
|
+
json(): UtilCom<D & {
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json";
|
|
102
|
+
};
|
|
103
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
99
104
|
/**收发皆为json的post预设 */
|
|
100
105
|
postJson(): UtilCom<D & {
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
method: "POST";
|
|
107
|
+
} & {
|
|
108
|
+
headers: {
|
|
109
|
+
'Content-Type': "application/json";
|
|
110
|
+
};
|
|
111
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
112
|
+
/**无查询参数获取json的get预设 */
|
|
113
|
+
getJson(): UtilCom<D & {
|
|
114
|
+
method: "GET";
|
|
115
|
+
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
116
|
+
/**有查询参数获取json的get预设 */
|
|
117
|
+
queryJson(): UtilCom<D & {
|
|
118
|
+
method: "GET";
|
|
119
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
120
|
+
/**收发皆为json的https-post预设 */
|
|
121
|
+
static httpsPostJson(): UtilCom<{
|
|
122
|
+
readonly protocol: "https:";
|
|
123
|
+
} & {
|
|
124
|
+
method: "POST";
|
|
125
|
+
} & {
|
|
126
|
+
headers: {
|
|
127
|
+
'Content-Type': "application/json";
|
|
128
|
+
};
|
|
129
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
130
|
+
/**收发皆为json的http-post预设 */
|
|
131
|
+
static httpPostJson(): UtilCom<{
|
|
132
|
+
readonly protocol: "http:";
|
|
133
|
+
} & {
|
|
134
|
+
method: "POST";
|
|
135
|
+
} & {
|
|
136
|
+
headers: {
|
|
137
|
+
'Content-Type': "application/json";
|
|
138
|
+
};
|
|
139
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
140
|
+
/**无查询参数获取json的https-get预设 */
|
|
141
|
+
static httpsGetJson(): UtilCom<{
|
|
142
|
+
readonly protocol: "https:";
|
|
143
|
+
} & {
|
|
144
|
+
method: "GET";
|
|
145
|
+
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
146
|
+
/**有查询参数获取json的https-get预设 */
|
|
147
|
+
static httpsQueryJson(): UtilCom<{
|
|
148
|
+
readonly protocol: "http:";
|
|
149
|
+
} & {
|
|
150
|
+
method: "GET";
|
|
151
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
152
|
+
/**无查询参数获取json的http-get预设 */
|
|
153
|
+
static httpGetJson(): UtilCom<{
|
|
154
|
+
readonly protocol: "http:";
|
|
155
|
+
} & {
|
|
156
|
+
method: "GET";
|
|
157
|
+
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
158
|
+
/**有查询参数获取json的http-get预设 */
|
|
159
|
+
static httpQueryJson(): UtilCom<{
|
|
160
|
+
readonly protocol: "http:";
|
|
161
|
+
} & {
|
|
162
|
+
method: "GET";
|
|
163
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
164
|
+
/**预设的接收数据类型*/
|
|
108
165
|
accept<T extends AcceptType>(t: T): {
|
|
109
166
|
readonly json: UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
110
|
-
readonly
|
|
167
|
+
readonly string: UtilCom<D, S, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
111
168
|
}[T];
|
|
112
169
|
acceptJson(): UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
113
|
-
|
|
114
|
-
|
|
170
|
+
acceptString(): UtilCom<D, S, typeof AcceptStringProc>;
|
|
171
|
+
/**自定的接收数据类型*/
|
|
172
|
+
acceptRaw<AD, AT>(proc: AcceptProc<AD, AT>): UtilCom<D, S, typeof proc>;
|
|
173
|
+
/**预设的发送数据类型*/
|
|
115
174
|
send<T extends SendType>(t: T): {
|
|
116
|
-
readonly json: UtilCom<D
|
|
175
|
+
readonly json: UtilCom<D & {
|
|
176
|
+
headers: {
|
|
177
|
+
"Content-Type": "application/json";
|
|
178
|
+
};
|
|
179
|
+
}, SendProc<[JToken]>, A>;
|
|
180
|
+
readonly query: UtilCom<D, SendProc<[QueryRequestData]>, A>;
|
|
181
|
+
readonly formData: UtilCom<D & {
|
|
182
|
+
headers: {
|
|
183
|
+
"Content-Type": "multipart/form-data";
|
|
184
|
+
};
|
|
185
|
+
}, SendProc<[FormData]>, A>;
|
|
117
186
|
readonly none: UtilCom<D, SendProc<[]>, A>;
|
|
118
187
|
}[T];
|
|
119
|
-
|
|
120
|
-
|
|
188
|
+
/**利用 req.write 发送一段json */
|
|
189
|
+
sendJson(): UtilCom<D & {
|
|
190
|
+
headers: {
|
|
191
|
+
"Content-Type": "application/json";
|
|
192
|
+
};
|
|
193
|
+
}, SendProc<[JToken]>, A>;
|
|
194
|
+
/**利用 appendQuery 直接将数据附加在path上发送请求 */
|
|
195
|
+
sendQuery(): UtilCom<D, SendProc<[QueryRequestData]>, A>;
|
|
196
|
+
sendFormData(): UtilCom<D & {
|
|
197
|
+
headers: {
|
|
198
|
+
"Content-Type": "multipart/form-data";
|
|
199
|
+
};
|
|
200
|
+
}, SendProc<[FormData]>, A>;
|
|
201
|
+
sendNone(): UtilCom<D, typeof SendNoneProc, A>;
|
|
202
|
+
/**自定的发送数据类型*/
|
|
203
|
+
sendRaw<T extends any[]>(proc: SendProc<T>): UtilCom<D, typeof proc, A>;
|
|
121
204
|
/**发送请求
|
|
122
|
-
* @param option
|
|
123
|
-
* @param datas
|
|
205
|
+
* @param option - 网络请求选项
|
|
206
|
+
* @param datas - 数据对象
|
|
124
207
|
*/
|
|
125
|
-
once(option: PartialOption<
|
|
208
|
+
once(option: PartialOption<RequestOption, D>, ...datas: SendParams<S>): Promise<ParseResult<A>>;
|
|
126
209
|
/**重复发送网络请求
|
|
127
|
-
* @param option
|
|
128
|
-
* @param
|
|
129
|
-
* @param
|
|
130
|
-
* @param datas
|
|
210
|
+
* @param option - 网络请求选项
|
|
211
|
+
* @param verify - 有效性验证函数
|
|
212
|
+
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
213
|
+
* @param datas - 数据对象
|
|
131
214
|
*/
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
215
|
+
retry(opt: {
|
|
216
|
+
option: PartialOption<RequestOption, D>;
|
|
217
|
+
verify?: StatusVerifyFn<ParseResult<A>>;
|
|
218
|
+
retries?: PromiseRetries;
|
|
219
|
+
}, ...datas: SendParams<S>): Promise<import("./UtilFunctions").PromiseRetryResult<ParseResult<A>>>;
|
|
220
|
+
/**发送网络请求
|
|
221
|
+
* @param option - 网络请求选项
|
|
222
|
+
* @param proc - 请求处理函数 需调用req.end()
|
|
223
|
+
* @param reduce - 数据处理函数
|
|
224
|
+
* @param init - 初始数据
|
|
138
225
|
*/
|
|
139
|
-
static
|
|
226
|
+
static request<T>(option: RequestOption, proc: RequestProcFn, reduce: RequestReduceFn<T>, init: T): Promise<RequestResult<T> | undefined>;
|
|
227
|
+
/**构建query */
|
|
228
|
+
static buildQuery(base: string, data: QueryRequestData): string;
|
|
140
229
|
}
|
|
141
230
|
export {};
|