@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/src/UtilCom.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { AnyString,JToken, MPromise, PartialOption, StatusVerifyFn } from "@/src/UtilInterfaces";
|
|
2
|
-
import https from 'https';
|
|
3
2
|
import http from 'http';
|
|
3
|
+
import https from 'https';
|
|
4
4
|
import { SLogger } from "@/src/UtilLogger";
|
|
5
|
-
import {
|
|
5
|
+
import { PromiseRetries, UtilFunc } from "@/src/UtilFunctions";
|
|
6
6
|
import qs from "querystring";
|
|
7
|
-
import { matchProc } from "./QuickExport";
|
|
8
7
|
import { Success } from "./UtilSymbol";
|
|
8
|
+
import FormData from "form-data";
|
|
9
|
+
import HttpProxyAgent from "http-proxy-agent";
|
|
10
|
+
import HttpsProxyAgent from "https-proxy-agent";
|
|
9
11
|
|
|
10
12
|
/**网络请求返回值 */
|
|
11
|
-
export type
|
|
13
|
+
export type RequestResult<T> = {
|
|
12
14
|
/**响应头 */
|
|
13
15
|
headers: http.IncomingHttpHeaders;
|
|
14
16
|
/**响应状态码 */
|
|
@@ -18,7 +20,7 @@ export type ComResp<T> = {
|
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
/**网络请求选项 */
|
|
21
|
-
export type
|
|
23
|
+
export type RequestOption = {
|
|
22
24
|
/**请求协议 */
|
|
23
25
|
protocol: 'http:'|'https:';
|
|
24
26
|
/**超时时间/毫秒 最小为10_000 默认无限 */
|
|
@@ -34,14 +36,14 @@ export type ComRequestOption = {
|
|
|
34
36
|
/**请求头 */
|
|
35
37
|
headers?: {
|
|
36
38
|
/**内容类型 */
|
|
37
|
-
'Content-Type'?: 'application/json'|AnyString;
|
|
39
|
+
'Content-Type'?: 'application/json'|'multipart/form-data'|AnyString;
|
|
38
40
|
/**内容长度 一般无需填写 应为buffer长度而非字符串长度 */
|
|
39
41
|
'Content-Length'?: number;
|
|
40
42
|
};
|
|
41
43
|
}&http.RequestOptions;
|
|
42
44
|
|
|
43
|
-
/**
|
|
44
|
-
export type
|
|
45
|
+
/**getquery请求所允许的数据 */
|
|
46
|
+
export type QueryRequestData = NodeJS.Dict<
|
|
45
47
|
| string
|
|
46
48
|
| number
|
|
47
49
|
| boolean
|
|
@@ -52,36 +54,37 @@ export type GetReqData = NodeJS.Dict<
|
|
|
52
54
|
>;
|
|
53
55
|
|
|
54
56
|
/**请求处理函数 需调用req.end() */
|
|
55
|
-
export type
|
|
57
|
+
export type RequestProcFn = ((req:http.ClientRequest)=>MPromise<void>);
|
|
56
58
|
/**数据处理函数 */
|
|
57
|
-
export type
|
|
59
|
+
export type RequestReduceFn<T> = (acc:T,data:string)=>MPromise<T>;
|
|
58
60
|
|
|
59
61
|
|
|
60
|
-
const AcceptTypeList = ["json"]as const;
|
|
62
|
+
const AcceptTypeList = ["json","string"]as const;
|
|
61
63
|
/**可用的接受类型 */
|
|
62
64
|
type AcceptType = typeof AcceptTypeList[number];
|
|
63
|
-
const SendTypeList = ["json","none"] as const;
|
|
65
|
+
const SendTypeList = ["json","query","formData","none"] as const;
|
|
64
66
|
/**可用的发送类型 */
|
|
65
67
|
type SendType = typeof SendTypeList[number];
|
|
66
68
|
|
|
67
69
|
/**accept处理数据 */
|
|
68
70
|
type AcceptProc<D,T> = {
|
|
69
|
-
init:D;
|
|
70
|
-
reduce:
|
|
71
|
-
parse:(result:
|
|
71
|
+
init :D;
|
|
72
|
+
reduce:RequestReduceFn<D>,
|
|
73
|
+
parse :(result:RequestResult<D>|undefined)=>MPromise<T>
|
|
72
74
|
}
|
|
73
75
|
/**send处理数据 */
|
|
74
76
|
type SendProc<T extends any[]> = {
|
|
75
|
-
proc:(opt:
|
|
77
|
+
proc:(opt:RequestOption,...args:T)=>MPromise<RequestProcFn>,
|
|
76
78
|
}
|
|
77
|
-
const
|
|
78
|
-
proc:()=>(req
|
|
79
|
+
const SendNoneProc:SendProc<[]> = {
|
|
80
|
+
proc:(opt)=>(req)=>void req.end()
|
|
79
81
|
}
|
|
80
|
-
const
|
|
81
|
-
init:'',
|
|
82
|
+
const AcceptStringProc:AcceptProc<string,RequestResult<string>|undefined> = {
|
|
83
|
+
init :'',
|
|
82
84
|
reduce:(acc:string,dat:string)=>acc+dat,
|
|
83
|
-
parse:(result:
|
|
85
|
+
parse :(result:RequestResult<string>|undefined)=>result
|
|
84
86
|
}
|
|
87
|
+
|
|
85
88
|
/**send处理的参数 */
|
|
86
89
|
type SendParams<T extends SendProc<any>> = T extends SendProc<infer T>
|
|
87
90
|
? T : never;
|
|
@@ -89,57 +92,112 @@ type SendParams<T extends SendProc<any>> = T extends SendProc<infer T>
|
|
|
89
92
|
type ParseResult<D extends AcceptProc<any,any>> = D extends AcceptProc<any,infer T>
|
|
90
93
|
? Awaited<T> : never;
|
|
91
94
|
|
|
92
|
-
/**根据方式自动推断json请求的可用数据类型 */
|
|
93
|
-
type JsonType<OPT extends Record<string,any>> = OPT['method'] extends "POST" ? JToken :GetReqData;
|
|
94
|
-
|
|
95
95
|
/**网络请求工具 */
|
|
96
96
|
export class UtilCom<
|
|
97
|
-
D extends Partial<
|
|
97
|
+
D extends Partial<RequestOption>&Required<Pick<RequestOption,'protocol'>>,
|
|
98
98
|
S extends SendProc<any>,
|
|
99
99
|
A extends AcceptProc<any,any>,
|
|
100
100
|
>{
|
|
101
101
|
private constructor(private _data:D,private _send:S, private _accept:A){}
|
|
102
102
|
|
|
103
|
-
//#region
|
|
103
|
+
//#region 流式创建
|
|
104
104
|
/**设为https请求 */
|
|
105
105
|
static https(){
|
|
106
|
-
return new UtilCom({protocol:'https:'} as const,
|
|
106
|
+
return new UtilCom({protocol:'https:'} as const,SendNoneProc,AcceptStringProc);
|
|
107
107
|
}
|
|
108
108
|
/**设为http请求 */
|
|
109
109
|
static http(){
|
|
110
|
-
return new UtilCom({protocol:'http:'} as const,
|
|
110
|
+
return new UtilCom({protocol:'http:'} as const,SendNoneProc,AcceptStringProc);
|
|
111
111
|
}
|
|
112
112
|
/**设为get方式的请求 */
|
|
113
113
|
get(){
|
|
114
|
-
|
|
114
|
+
this._data.method = 'GET';
|
|
115
|
+
return this as any as UtilCom<D & {method:'GET'},S,A>;
|
|
115
116
|
}
|
|
116
117
|
/**设为Post方式的请求 */
|
|
117
118
|
post(){
|
|
118
|
-
|
|
119
|
+
this._data.method = 'POST';
|
|
120
|
+
return this as any as UtilCom<D & {method:'POST'},S,A>;
|
|
121
|
+
}
|
|
122
|
+
/**补充参数
|
|
123
|
+
* 将会替换对应字段, 修改headers请用header函数
|
|
124
|
+
*/
|
|
125
|
+
option<OPT extends Partial<RequestOption>>(option:OPT){
|
|
126
|
+
this._data = {...this._data,...option};
|
|
127
|
+
return this as any as UtilCom<D & OPT,S,A>;
|
|
119
128
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
129
|
+
/**补充header */
|
|
130
|
+
header<HAD extends http.OutgoingHttpHeaders>(headers:HAD){
|
|
131
|
+
this._data.headers = {
|
|
132
|
+
...this._data.headers,
|
|
133
|
+
...headers,
|
|
134
|
+
};
|
|
135
|
+
return this as any as UtilCom<D & {headers:HAD} ,S,A>;
|
|
136
|
+
}
|
|
137
|
+
/**设置agent */
|
|
138
|
+
proxyAgent(url:string){
|
|
139
|
+
this._data.agent = UtilFunc.matchProc(this._data['protocol'],{
|
|
140
|
+
'http:' :()=>HttpProxyAgent(url),
|
|
141
|
+
'https:':()=>HttpsProxyAgent(url),
|
|
142
|
+
})
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
/**添加一段query */
|
|
146
|
+
query(data:QueryRequestData){
|
|
147
|
+
this._data.path = UtilCom.buildQuery(this._data.path??'',data);
|
|
148
|
+
return this;
|
|
123
149
|
}
|
|
124
150
|
//#endregion
|
|
125
151
|
|
|
126
152
|
//#region 快速预设
|
|
153
|
+
/**收发皆为json的预设 */
|
|
154
|
+
json(){
|
|
155
|
+
return this.sendJson().acceptJson();
|
|
156
|
+
}
|
|
127
157
|
/**收发皆为json的post预设 */
|
|
128
158
|
postJson(){
|
|
129
159
|
return this.post().json();
|
|
130
160
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return this.
|
|
161
|
+
/**无查询参数获取json的get预设 */
|
|
162
|
+
getJson(){
|
|
163
|
+
return this.get().sendNone().acceptJson();
|
|
164
|
+
}
|
|
165
|
+
/**有查询参数获取json的get预设 */
|
|
166
|
+
queryJson(){
|
|
167
|
+
return this.get().sendQuery().acceptJson();
|
|
168
|
+
}
|
|
169
|
+
/**收发皆为json的https-post预设 */
|
|
170
|
+
static httpsPostJson(){
|
|
171
|
+
return UtilCom.https().postJson();
|
|
172
|
+
}
|
|
173
|
+
/**收发皆为json的http-post预设 */
|
|
174
|
+
static httpPostJson(){
|
|
175
|
+
return UtilCom.http().postJson();
|
|
176
|
+
}
|
|
177
|
+
/**无查询参数获取json的https-get预设 */
|
|
178
|
+
static httpsGetJson(){
|
|
179
|
+
return UtilCom.https().getJson();
|
|
180
|
+
}
|
|
181
|
+
/**有查询参数获取json的https-get预设 */
|
|
182
|
+
static httpsQueryJson(){
|
|
183
|
+
return UtilCom.http().queryJson();
|
|
184
|
+
}
|
|
185
|
+
/**无查询参数获取json的http-get预设 */
|
|
186
|
+
static httpGetJson(){
|
|
187
|
+
return UtilCom.http().getJson();
|
|
188
|
+
}
|
|
189
|
+
/**有查询参数获取json的http-get预设 */
|
|
190
|
+
static httpQueryJson(){
|
|
191
|
+
return UtilCom.http().queryJson();
|
|
134
192
|
}
|
|
135
193
|
//#endregion
|
|
136
194
|
|
|
137
195
|
//#region 接收数据类型
|
|
138
|
-
|
|
196
|
+
/**预设的接收数据类型*/
|
|
139
197
|
accept<T extends AcceptType>(t:T){
|
|
140
198
|
const map = {
|
|
141
|
-
'json':this.acceptJson(),
|
|
142
|
-
'
|
|
199
|
+
'json' :this.acceptJson(),
|
|
200
|
+
'string':this.acceptString(),
|
|
143
201
|
} as const;
|
|
144
202
|
return map[t];
|
|
145
203
|
}
|
|
@@ -152,66 +210,59 @@ A extends AcceptProc<any,any>,
|
|
|
152
210
|
const {data,...rest} = result;
|
|
153
211
|
|
|
154
212
|
if(data.trim()==""){
|
|
155
|
-
SLogger.warn(`json accept 接收反馈错误:
|
|
213
|
+
SLogger.warn(`json accept 接收反馈错误: 原始字符串为空`,UtilFunc.stringifyJToken(result,{compress:true,space:2}));
|
|
156
214
|
return {...result,raw:"",data:null};
|
|
157
215
|
}
|
|
158
216
|
try{
|
|
159
217
|
const obj = JSON.parse(data.trim()) as JToken;
|
|
160
|
-
SLogger.http(
|
|
218
|
+
SLogger.http(
|
|
219
|
+
`json accept 接受信息 data:`,
|
|
220
|
+
UtilFunc.stringifyJToken(obj,{compress:true,space:2}),
|
|
221
|
+
`result:`,
|
|
222
|
+
UtilFunc.stringifyJToken(rest,{compress:true,space:2})
|
|
223
|
+
);
|
|
161
224
|
return{...rest,data:obj};
|
|
162
225
|
}
|
|
163
226
|
catch(e){
|
|
164
|
-
SLogger.warn(`json accept 接收反馈错误:${e}
|
|
227
|
+
SLogger.warn(`json accept 接收反馈错误:${e}`,UtilFunc.stringifyJToken(result,{compress:true,space:2}));
|
|
165
228
|
return {...result,raw:data,data:null};
|
|
166
229
|
}
|
|
167
230
|
}
|
|
168
231
|
}
|
|
169
|
-
|
|
232
|
+
this._accept = proc as any;
|
|
233
|
+
return this as any as UtilCom<D,S,typeof proc>;
|
|
170
234
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return data;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
return new UtilCom(this._data,this._send,proc)
|
|
235
|
+
acceptString(){
|
|
236
|
+
this._accept = AcceptStringProc as any;
|
|
237
|
+
return this as any as UtilCom<D,S,typeof AcceptStringProc>;
|
|
238
|
+
}
|
|
239
|
+
/**自定的接收数据类型*/
|
|
240
|
+
acceptRaw<AD,AT>(proc:AcceptProc<AD,AT>){
|
|
241
|
+
this._accept = proc as any;
|
|
242
|
+
return this as any as UtilCom<D,S,typeof proc>;
|
|
183
243
|
}
|
|
184
244
|
//#endregion
|
|
185
245
|
|
|
186
246
|
//#region 发送数据类型
|
|
187
|
-
|
|
247
|
+
/**预设的发送数据类型*/
|
|
188
248
|
send<T extends SendType>(t:T){
|
|
189
249
|
const map = {
|
|
190
|
-
'json':this.sendJson(),
|
|
191
|
-
|
|
250
|
+
'json' :this.sendJson(),
|
|
251
|
+
'query' :this.sendQuery(),
|
|
252
|
+
'formData' :this.sendFormData(),
|
|
253
|
+
'none' :this.sendNone(),
|
|
192
254
|
} as const;
|
|
193
255
|
return map[t];
|
|
194
256
|
}
|
|
257
|
+
/**利用 req.write 发送一段json */
|
|
195
258
|
sendJson(){
|
|
196
|
-
const proc:SendProc<[
|
|
197
|
-
proc:(opt:
|
|
259
|
+
const proc:SendProc<[JToken]>={
|
|
260
|
+
proc:(opt:RequestOption,reqData:JToken)=>{
|
|
198
261
|
const {method} = opt;
|
|
199
262
|
const isPost = (method=="POST");
|
|
200
263
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
if (queryString) {
|
|
204
|
-
// 检查当前路径是否已经包含问号
|
|
205
|
-
const separator = opt?.path?.includes('?') ? '&' : '?';
|
|
206
|
-
opt.path += separator + queryString;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
if(isPost){
|
|
210
|
-
this._data.headers??={
|
|
211
|
-
'Content-Type': 'application/json',
|
|
212
|
-
'Content-Length': Buffer.from(JSON.stringify(reqData)).length,
|
|
213
|
-
}
|
|
214
|
-
}
|
|
264
|
+
this._data.headers??={};
|
|
265
|
+
this._data.headers['Content-Length'] = Buffer.byteLength(JSON.stringify(reqData));
|
|
215
266
|
|
|
216
267
|
const procReq = (req:http.ClientRequest)=>{
|
|
217
268
|
if(isPost) req.write(JSON.stringify(reqData));
|
|
@@ -220,70 +271,107 @@ A extends AcceptProc<any,any>,
|
|
|
220
271
|
return procReq;
|
|
221
272
|
}
|
|
222
273
|
}
|
|
223
|
-
|
|
274
|
+
this._send = proc as any;
|
|
275
|
+
this._data.headers??={};
|
|
276
|
+
this._data.headers['Content-Type'] = 'application/json';
|
|
277
|
+
return this as any as UtilCom<D&{
|
|
278
|
+
headers:{ 'Content-Type': 'application/json' }
|
|
279
|
+
},typeof proc,A>;
|
|
224
280
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
281
|
+
/**利用 appendQuery 直接将数据附加在path上发送请求 */
|
|
282
|
+
sendQuery(){
|
|
283
|
+
const proc:SendProc<[QueryRequestData]>={
|
|
284
|
+
proc:(opt:RequestOption,reqData:QueryRequestData)=>{
|
|
285
|
+
opt.path = UtilCom.buildQuery(opt.path??'',reqData)
|
|
286
|
+
const procReq = (req:http.ClientRequest)=>void req.end();
|
|
231
287
|
return procReq;
|
|
232
288
|
}
|
|
233
289
|
}
|
|
234
|
-
|
|
290
|
+
this._send = proc as any;
|
|
291
|
+
return this as any as UtilCom<D,typeof proc,A>;
|
|
292
|
+
}
|
|
293
|
+
sendFormData() {
|
|
294
|
+
const proc: SendProc<[FormData]> = {
|
|
295
|
+
proc: (opt: RequestOption, formData: FormData) => {
|
|
296
|
+
opt.headers = formData.getHeaders();
|
|
297
|
+
const procReq = (req: http.ClientRequest) => {
|
|
298
|
+
formData.pipe(req);
|
|
299
|
+
req.end();
|
|
300
|
+
};
|
|
301
|
+
return procReq;
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
this._send = proc as any;
|
|
305
|
+
this._data.headers??={};
|
|
306
|
+
this._data.headers['Content-Type'] = 'multipart/form-data';
|
|
307
|
+
return this as any as UtilCom<D&{
|
|
308
|
+
headers:{ 'Content-Type': 'multipart/form-data' }
|
|
309
|
+
}, typeof proc, A>;
|
|
310
|
+
}
|
|
311
|
+
sendNone(){
|
|
312
|
+
this._send = SendNoneProc as any;
|
|
313
|
+
return this as any as UtilCom<D,typeof SendNoneProc,A>;
|
|
314
|
+
}
|
|
315
|
+
/**自定的发送数据类型*/
|
|
316
|
+
sendRaw<T extends any[]>(proc:SendProc<T>){
|
|
317
|
+
this._send = proc as any;
|
|
318
|
+
return this as any as UtilCom<D, typeof proc, A>;
|
|
235
319
|
}
|
|
236
320
|
//#endregion
|
|
237
321
|
|
|
238
|
-
/**发送请求
|
|
239
|
-
* @param option
|
|
240
|
-
* @param datas
|
|
322
|
+
/**发送请求
|
|
323
|
+
* @param option - 网络请求选项
|
|
324
|
+
* @param datas - 数据对象
|
|
241
325
|
*/
|
|
242
|
-
async once(option:PartialOption<
|
|
243
|
-
const fullopt = Object.assign({},this._data,option) as
|
|
244
|
-
const
|
|
326
|
+
async once(option:PartialOption<RequestOption,D>,...datas:SendParams<S>){
|
|
327
|
+
const fullopt = Object.assign({},this._data,option) as RequestOption;
|
|
328
|
+
const proc = await this._send.proc(fullopt,...datas as any[]);
|
|
245
329
|
const {reduce,init,parse} = this._accept;
|
|
246
|
-
const res = await UtilCom.
|
|
247
|
-
return parse(res)
|
|
330
|
+
const res = await UtilCom.request(fullopt,proc,reduce,init);
|
|
331
|
+
return parse(res) as ParseResult<A>;
|
|
248
332
|
}
|
|
249
333
|
/**重复发送网络请求
|
|
250
|
-
* @param option
|
|
251
|
-
* @param
|
|
252
|
-
* @param
|
|
253
|
-
* @param datas
|
|
334
|
+
* @param option - 网络请求选项
|
|
335
|
+
* @param verify - 有效性验证函数
|
|
336
|
+
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
337
|
+
* @param datas - 数据对象
|
|
254
338
|
*/
|
|
255
|
-
async
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
339
|
+
async retry(
|
|
340
|
+
opt:{
|
|
341
|
+
option :PartialOption<RequestOption,D>,
|
|
342
|
+
verify? :StatusVerifyFn<ParseResult<A>>,
|
|
343
|
+
retries?:PromiseRetries,
|
|
344
|
+
},
|
|
259
345
|
...datas:SendParams<S>
|
|
260
346
|
){
|
|
261
|
-
|
|
347
|
+
let {option,retries,verify} = opt;
|
|
348
|
+
retries??={};
|
|
349
|
+
retries.tryDelay = retries.tryDelay??1000;
|
|
262
350
|
const procFn = ()=>this.once(option,...datas);
|
|
263
|
-
return UtilFunc.
|
|
351
|
+
return UtilFunc.retryPromise(procFn,verify,retries);
|
|
264
352
|
}
|
|
265
353
|
|
|
266
|
-
|
|
267
|
-
* @param
|
|
268
|
-
* @param
|
|
269
|
-
* @param
|
|
270
|
-
* @param
|
|
354
|
+
/**发送网络请求
|
|
355
|
+
* @param option - 网络请求选项
|
|
356
|
+
* @param proc - 请求处理函数 需调用req.end()
|
|
357
|
+
* @param reduce - 数据处理函数
|
|
358
|
+
* @param init - 初始数据
|
|
271
359
|
*/
|
|
272
|
-
static async
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
360
|
+
static async request<T>(
|
|
361
|
+
option:RequestOption,
|
|
362
|
+
proc:RequestProcFn,
|
|
363
|
+
reduce:RequestReduceFn<T>,
|
|
364
|
+
init:T,
|
|
277
365
|
){
|
|
278
|
-
const {protocol,timeout,...baseReqOpt} =
|
|
366
|
+
const {protocol,timeout,...baseReqOpt} = option;
|
|
279
367
|
|
|
280
368
|
const hasTimeLimit = (timeout ? timeout>=10_000 : false );
|
|
281
369
|
|
|
282
|
-
const flagName = `UtilCom.
|
|
370
|
+
const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunc.genUUID()}`;
|
|
283
371
|
|
|
284
372
|
let dataPromise:Promise<any>|null = null;
|
|
285
373
|
|
|
286
|
-
return new Promise<
|
|
374
|
+
return new Promise<RequestResult<T>|undefined>(async (resolve, rejecte)=>{
|
|
287
375
|
const resFunc = (res:http.IncomingMessage)=>{
|
|
288
376
|
try{
|
|
289
377
|
//请求超时
|
|
@@ -294,10 +382,10 @@ A extends AcceptProc<any,any>,
|
|
|
294
382
|
});
|
|
295
383
|
}
|
|
296
384
|
|
|
297
|
-
let mergedata:T =
|
|
385
|
+
let mergedata:T = init;
|
|
298
386
|
res.setEncoding('utf8');
|
|
299
387
|
res.on('data', chunk => {
|
|
300
|
-
dataPromise = UtilFunc.queueProc(flagName,async()=>mergedata=await
|
|
388
|
+
dataPromise = UtilFunc.queueProc(flagName,async()=>mergedata=await reduce(mergedata,chunk))
|
|
301
389
|
});
|
|
302
390
|
|
|
303
391
|
res.on('error',(e)=>{
|
|
@@ -321,8 +409,8 @@ A extends AcceptProc<any,any>,
|
|
|
321
409
|
};
|
|
322
410
|
//路由 http/https
|
|
323
411
|
const req:http.ClientRequest= protocol=="https:"
|
|
324
|
-
? https.request(baseReqOpt
|
|
325
|
-
: http.request(baseReqOpt
|
|
412
|
+
? https.request(baseReqOpt, resFunc)
|
|
413
|
+
: http .request(baseReqOpt, resFunc);
|
|
326
414
|
|
|
327
415
|
//请求超时
|
|
328
416
|
if(hasTimeLimit){
|
|
@@ -337,18 +425,33 @@ A extends AcceptProc<any,any>,
|
|
|
337
425
|
resolve(undefined);
|
|
338
426
|
});
|
|
339
427
|
|
|
340
|
-
await
|
|
428
|
+
await proc(req);
|
|
341
429
|
});
|
|
342
430
|
}
|
|
431
|
+
|
|
432
|
+
/**构建query */
|
|
433
|
+
static buildQuery(base:string,data:QueryRequestData){
|
|
434
|
+
const queryString = qs.stringify(data);
|
|
435
|
+
if (queryString) {
|
|
436
|
+
// 检查当前路径是否已经包含问号
|
|
437
|
+
const separator = base.includes('?') ? '&' : '?';
|
|
438
|
+
base += separator + queryString;
|
|
439
|
+
}
|
|
440
|
+
return base;
|
|
441
|
+
}
|
|
343
442
|
}
|
|
344
443
|
|
|
345
444
|
|
|
346
445
|
if(false)((async ()=>{
|
|
347
|
-
const t = await UtilCom.https().
|
|
348
|
-
.
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
446
|
+
const t = await UtilCom.https().postJson()
|
|
447
|
+
.retry({
|
|
448
|
+
option:{
|
|
449
|
+
hostname:'httpbin.org',
|
|
450
|
+
path:'/post',
|
|
451
|
+
timeout:10000
|
|
452
|
+
},
|
|
453
|
+
verify:()=>Success,
|
|
454
|
+
retries:{}
|
|
455
|
+
},{test:1});
|
|
353
456
|
console.log(t);
|
|
354
457
|
})())
|