@zwa73/utils 1.0.195 → 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.
- package/dist/UtilClass.js +1 -0
- package/dist/UtilCom.d.ts +95 -121
- package/dist/UtilCom.js +192 -193
- package/dist/UtilFunctions.d.ts +2 -2
- package/dist/UtilInterfaces.d.ts +6 -6
- package/package.json +1 -1
- package/src/UtilClass.ts +6 -0
- package/src/UtilCom.ts +277 -274
- package/src/UtilCom_bak.txt +345 -0
- package/src/UtilFunctions.ts +3 -3
- package/src/UtilInterfaces.ts +6 -6
package/dist/UtilClass.js
CHANGED
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AnyString,
|
|
1
|
+
import { AnyString, JToken, MPromise, PartialOption, StatusVerifyFn } from "./UtilInterfaces";
|
|
2
2
|
import http from 'http';
|
|
3
|
-
import { RepeatPromiseOpt
|
|
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
|
|
14
|
+
export type ComRequestOption = {
|
|
15
15
|
/**请求协议 */
|
|
16
|
-
protocol: 'http' | 'https';
|
|
16
|
+
protocol: 'http:' | 'https:';
|
|
17
17
|
/**超时时间/毫秒 最小为10_000 默认无限 */
|
|
18
|
-
|
|
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
|
|
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
|
-
|
|
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 {};
|
package/dist/UtilCom.js
CHANGED
|
@@ -9,37 +9,203 @@ const http_1 = __importDefault(require("http"));
|
|
|
9
9
|
const UtilLogger_1 = require("./UtilLogger");
|
|
10
10
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
11
11
|
const querystring_1 = __importDefault(require("querystring"));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const UtilSymbol_1 = require("./UtilSymbol");
|
|
13
|
+
const AcceptTypeList = ["json"];
|
|
14
|
+
const SendTypeList = ["json", "none"];
|
|
15
|
+
const defSend = {
|
|
16
|
+
proc: () => (req) => void req.end()
|
|
17
|
+
};
|
|
18
|
+
const defAccept = {
|
|
19
|
+
init: '',
|
|
20
|
+
reduce: (acc, dat) => acc + dat,
|
|
21
|
+
parse: (result) => void console.log(result)
|
|
22
|
+
};
|
|
23
|
+
/**网络请求工具 */
|
|
24
|
+
class UtilCom {
|
|
25
|
+
_data;
|
|
26
|
+
_send;
|
|
27
|
+
_accept;
|
|
28
|
+
constructor(_data, _send, _accept) {
|
|
29
|
+
this._data = _data;
|
|
30
|
+
this._send = _send;
|
|
31
|
+
this._accept = _accept;
|
|
32
|
+
}
|
|
33
|
+
//#region 快速流式创建
|
|
34
|
+
/**设为https请求 */
|
|
35
|
+
static https() {
|
|
36
|
+
return new UtilCom({ protocol: 'https:' }, defSend, defAccept);
|
|
37
|
+
}
|
|
38
|
+
/**设为http请求 */
|
|
39
|
+
static http() {
|
|
40
|
+
return new UtilCom({ protocol: 'http:' }, defSend, defAccept);
|
|
41
|
+
}
|
|
42
|
+
/**设为get方式的请求 */
|
|
43
|
+
get() {
|
|
44
|
+
return new UtilCom({ ...this._data, method: 'GET' }, this._send, this._accept);
|
|
45
|
+
}
|
|
46
|
+
/**设为Post方式的请求 */
|
|
47
|
+
post() {
|
|
48
|
+
return new UtilCom({ ...this._data, method: 'POST' }, this._send, this._accept);
|
|
49
|
+
}
|
|
50
|
+
/**补充参数 */
|
|
51
|
+
option(opt) {
|
|
52
|
+
return new UtilCom({ ...this._data, ...opt }, this._send, this._accept);
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region 接收数据类型
|
|
56
|
+
/**接收数据类型*/
|
|
57
|
+
accept(t) {
|
|
58
|
+
const map = {
|
|
59
|
+
'json': this.acceptJson(),
|
|
60
|
+
'log': this.acceptLog(),
|
|
61
|
+
};
|
|
62
|
+
return map[t];
|
|
63
|
+
}
|
|
64
|
+
acceptJson() {
|
|
65
|
+
const proc = {
|
|
66
|
+
init: '',
|
|
67
|
+
reduce: (acc, curr) => acc + curr,
|
|
68
|
+
parse: (result) => {
|
|
69
|
+
if (result == undefined)
|
|
70
|
+
return undefined;
|
|
71
|
+
const { data, ...rest } = result;
|
|
72
|
+
if (data.trim() == "") {
|
|
73
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误: 原始字符串为空`);
|
|
74
|
+
return { ...result, raw: "", data: null };
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const obj = JSON.parse(data.trim());
|
|
78
|
+
UtilLogger_1.SLogger.http(`json accept 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }));
|
|
79
|
+
return { ...rest, data: obj };
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:${e}\n原始字符串:${data}`);
|
|
83
|
+
return { ...result, raw: data, data: null };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return new UtilCom(this._data, this._send, proc);
|
|
88
|
+
}
|
|
89
|
+
acceptLog() {
|
|
90
|
+
const proc = {
|
|
91
|
+
init: '',
|
|
92
|
+
reduce: (acc, curr) => acc + curr,
|
|
93
|
+
parse: (result) => {
|
|
94
|
+
UtilLogger_1.SLogger.http(`log accept 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
95
|
+
if (result == undefined)
|
|
96
|
+
return '';
|
|
97
|
+
const { data, ...rest } = result;
|
|
98
|
+
return data;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
return new UtilCom(this._data, this._send, proc);
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region 发送数据类型
|
|
105
|
+
/**发送数据类型*/
|
|
106
|
+
send(t) {
|
|
107
|
+
const map = {
|
|
108
|
+
'json': this.sendJson(),
|
|
109
|
+
"none": this.sendNone(),
|
|
110
|
+
};
|
|
111
|
+
return map[t];
|
|
112
|
+
}
|
|
113
|
+
sendJson() {
|
|
114
|
+
const proc = {
|
|
115
|
+
proc: (opt, reqData) => {
|
|
116
|
+
const { method } = opt;
|
|
117
|
+
const isPost = (method == "POST");
|
|
118
|
+
if (!isPost && reqData != undefined) {
|
|
119
|
+
const queryString = querystring_1.default.stringify(reqData);
|
|
120
|
+
if (queryString) {
|
|
121
|
+
// 检查当前路径是否已经包含问号
|
|
122
|
+
const separator = opt?.path?.includes('?') ? '&' : '?';
|
|
123
|
+
opt.path += separator + queryString;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (isPost) {
|
|
127
|
+
this._data.headers ??= {
|
|
128
|
+
'Content-Type': 'application/json',
|
|
129
|
+
'Content-Length': Buffer.from(JSON.stringify(reqData)).length,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const procReq = (req) => {
|
|
133
|
+
if (isPost)
|
|
134
|
+
req.write(JSON.stringify(reqData));
|
|
135
|
+
req.end();
|
|
136
|
+
};
|
|
137
|
+
return procReq;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
return new UtilCom(this._data, proc, this._accept);
|
|
141
|
+
}
|
|
142
|
+
sendNone() {
|
|
143
|
+
const proc = {
|
|
144
|
+
proc: (opt) => {
|
|
145
|
+
const procReq = (req) => {
|
|
146
|
+
req.end();
|
|
147
|
+
};
|
|
148
|
+
return procReq;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
return new UtilCom(this._data, proc, this._accept);
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
/**发送请求
|
|
155
|
+
* @param option - 网络请求选项
|
|
156
|
+
* @param datas - 数据对象
|
|
157
|
+
*/
|
|
158
|
+
async once(option, ...datas) {
|
|
159
|
+
const fullopt = Object.assign({}, this._data, option);
|
|
160
|
+
const procReq = await this._send.proc(fullopt, ...datas);
|
|
161
|
+
const { reduce, init, parse } = this._accept;
|
|
162
|
+
const res = await UtilCom.comReq(fullopt, procReq, reduce, init);
|
|
163
|
+
return parse(res);
|
|
164
|
+
}
|
|
165
|
+
/**重复发送网络请求
|
|
166
|
+
* @param option - 网络请求选项
|
|
167
|
+
* @param verifyFn - 有效性验证函数
|
|
168
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
169
|
+
* @param datas - 数据对象
|
|
170
|
+
*/
|
|
171
|
+
async repeat(option, verifyFn, repeatOpt = {}, ...datas) {
|
|
172
|
+
repeatOpt.tryDelay = repeatOpt.tryDelay ?? 1000;
|
|
173
|
+
const procFn = () => this.once(option, ...datas);
|
|
174
|
+
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatOpt);
|
|
175
|
+
}
|
|
15
176
|
/**网络请求
|
|
16
177
|
* @param comReqOpt - 网络请求选项
|
|
17
178
|
* @param procReq - 请求处理函数 需调用req.end()
|
|
18
179
|
* @param reduceData - 数据处理函数
|
|
19
180
|
* @param initData - 初始数据
|
|
20
181
|
*/
|
|
21
|
-
async
|
|
22
|
-
const { protocol,
|
|
23
|
-
const hasTimeLimit = (
|
|
24
|
-
const flagName =
|
|
182
|
+
static async comReq(comReqOpt, procReq, reduceData, initData) {
|
|
183
|
+
const { protocol, timeout, ...baseReqOpt } = comReqOpt;
|
|
184
|
+
const hasTimeLimit = (timeout ? timeout >= 10_000 : false);
|
|
185
|
+
const flagName = `UtilCom.${protocol}${baseReqOpt.method}`;
|
|
186
|
+
const postflag = `comReq:${UtilFunctions_1.UtilFunc.genUUID()}`;
|
|
187
|
+
let dataPromise = null;
|
|
25
188
|
return new Promise(async (resolve, rejecte) => {
|
|
26
189
|
const resFunc = (res) => {
|
|
27
190
|
try {
|
|
28
191
|
//请求超时
|
|
29
192
|
if (hasTimeLimit) {
|
|
30
|
-
res.setTimeout(
|
|
31
|
-
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈超时: ${
|
|
193
|
+
res.setTimeout(timeout, () => {
|
|
194
|
+
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈超时: ${timeout} ms`);
|
|
32
195
|
resolve(undefined);
|
|
33
196
|
});
|
|
34
197
|
}
|
|
35
198
|
let mergedata = initData;
|
|
36
199
|
res.setEncoding('utf8');
|
|
37
|
-
res.on('data', chunk =>
|
|
200
|
+
res.on('data', chunk => {
|
|
201
|
+
dataPromise = UtilFunctions_1.UtilFunc.queueProc(postflag, async () => mergedata = await reduceData(mergedata, chunk));
|
|
202
|
+
});
|
|
38
203
|
res.on('error', (e) => {
|
|
39
204
|
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈错误:${e}`);
|
|
40
205
|
resolve(undefined);
|
|
41
206
|
});
|
|
42
|
-
res.on('end', () => {
|
|
207
|
+
res.on('end', async () => {
|
|
208
|
+
await dataPromise;
|
|
43
209
|
resolve({
|
|
44
210
|
headers: res.headers,
|
|
45
211
|
statusCode: res.statusCode,
|
|
@@ -54,19 +220,15 @@ var UtilCom;
|
|
|
54
220
|
}
|
|
55
221
|
};
|
|
56
222
|
//路由 http/https
|
|
57
|
-
const req = protocol == "https"
|
|
223
|
+
const req = protocol == "https:"
|
|
58
224
|
? https_1.default.request(baseReqOpt, resFunc)
|
|
59
225
|
: http_1.default.request(baseReqOpt, resFunc);
|
|
60
226
|
//请求超时
|
|
61
227
|
if (hasTimeLimit) {
|
|
62
|
-
req.setTimeout(
|
|
63
|
-
UtilLogger_1.SLogger.warn(`${flagName} 发送请求超时: ${
|
|
228
|
+
req.setTimeout(timeout, () => {
|
|
229
|
+
UtilLogger_1.SLogger.warn(`${flagName} 发送请求超时: ${timeout} ms`);
|
|
64
230
|
req.destroy();
|
|
65
231
|
});
|
|
66
|
-
//req.on('timeout', ()=>{
|
|
67
|
-
// SLogger.warn(`${flagName} 发送请求超时(timeout): ${timeLimit} ms`);
|
|
68
|
-
// req.destroy();
|
|
69
|
-
//});
|
|
70
232
|
}
|
|
71
233
|
req.on('error', (e) => {
|
|
72
234
|
UtilLogger_1.SLogger.warn(`${flagName} 发送请求错误:${e}`);
|
|
@@ -75,178 +237,15 @@ var UtilCom;
|
|
|
75
237
|
await procReq(req);
|
|
76
238
|
});
|
|
77
239
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// 检查当前路径是否已经包含问号
|
|
91
|
-
const separator = comReqOpt?.path?.includes('?') ? '&' : '?';
|
|
92
|
-
comReqOpt.path += separator + queryString;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const procReq = (req) => {
|
|
96
|
-
if (isPost)
|
|
97
|
-
req.write(JSON.stringify(reqData));
|
|
98
|
-
req.end();
|
|
99
|
-
};
|
|
100
|
-
const reduceData = (acc, data) => acc + data;
|
|
101
|
-
const result = await comReq(comReqOpt, procReq, reduceData, "");
|
|
102
|
-
if (result == undefined)
|
|
103
|
-
return undefined;
|
|
104
|
-
const { data, ...rest } = result;
|
|
105
|
-
if (data.trim() == "") {
|
|
106
|
-
UtilLogger_1.SLogger.warn(`${jsonReq.name} 接收反馈错误: 原始字符串为空`);
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
try {
|
|
110
|
-
const obj = JSON.parse(data.trim());
|
|
111
|
-
UtilLogger_1.SLogger.http(`${jsonReq.name} 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }));
|
|
112
|
-
return { ...rest, data: obj };
|
|
113
|
-
}
|
|
114
|
-
catch (e) {
|
|
115
|
-
UtilLogger_1.SLogger.warn(`${jsonReq.name} 接收反馈错误:${e}\n原始字符串:${data}`);
|
|
116
|
-
return undefined;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**重复发送json的网络请求
|
|
120
|
-
* @async
|
|
121
|
-
* @param comReqOpt - 网络请求选项
|
|
122
|
-
* @param reqData - 数据对象
|
|
123
|
-
* @param verifyFn - 有效性验证函数
|
|
124
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
125
|
-
* @returns 结果 undefined 为未能成功接收
|
|
126
|
-
*/
|
|
127
|
-
async function repeatJsonComReq(comReqOpt, reqData, verifyFn, repeatOpt = {}) {
|
|
128
|
-
repeatOpt.tryDelay = repeatOpt.tryDelay ?? 1000;
|
|
129
|
-
const procFn = () => jsonReq(comReqOpt, reqData);
|
|
130
|
-
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatOpt);
|
|
131
|
-
}
|
|
132
|
-
/**发送一个 https POST 请求并接受数据
|
|
133
|
-
* @async
|
|
134
|
-
* @param comReqOpt - 请求参数
|
|
135
|
-
* @param reqData - 数据对象
|
|
136
|
-
* @returns 结果 undefined 为未能成功接收
|
|
137
|
-
*/
|
|
138
|
-
function httpsPost(comReqOpt, reqData) {
|
|
139
|
-
return jsonReq({
|
|
140
|
-
...comReqOpt,
|
|
141
|
-
method: "POST",
|
|
142
|
-
protocol: "https",
|
|
143
|
-
}, reqData);
|
|
144
|
-
}
|
|
145
|
-
UtilCom.httpsPost = httpsPost;
|
|
146
|
-
/**发送一个 http POST 请求并接受数据
|
|
147
|
-
* @async
|
|
148
|
-
* @param comReqOpt - 请求参数
|
|
149
|
-
* @param reqData - 数据对象
|
|
150
|
-
* @returns 结果 undefined 为未能成功接收
|
|
151
|
-
*/
|
|
152
|
-
function httpPost(comReqOpt, reqData) {
|
|
153
|
-
return jsonReq({
|
|
154
|
-
...comReqOpt,
|
|
155
|
-
method: "POST",
|
|
156
|
-
protocol: "http",
|
|
157
|
-
}, reqData);
|
|
158
|
-
}
|
|
159
|
-
UtilCom.httpPost = httpPost;
|
|
160
|
-
/**发送一个 https GET 请求并接受数据
|
|
161
|
-
* @async
|
|
162
|
-
* @param comReqOpt - 请求参数
|
|
163
|
-
* @param reqData - 数据对象
|
|
164
|
-
* @returns 结果 undefined 为未能成功接收
|
|
165
|
-
*/
|
|
166
|
-
function httpsGet(comReqOpt, reqData) {
|
|
167
|
-
return jsonReq({
|
|
168
|
-
...comReqOpt,
|
|
169
|
-
method: "GET",
|
|
170
|
-
protocol: "https",
|
|
171
|
-
}, reqData);
|
|
172
|
-
}
|
|
173
|
-
UtilCom.httpsGet = httpsGet;
|
|
174
|
-
/**发送一个 http GET 请求并接受数据
|
|
175
|
-
* @async
|
|
176
|
-
* @param comReqOpt - 请求参数
|
|
177
|
-
* @param reqData - 数据对象
|
|
178
|
-
* @returns 结果 undefined 为未能成功接收
|
|
179
|
-
*/
|
|
180
|
-
function httpGet(comReqOpt, reqData) {
|
|
181
|
-
return jsonReq({
|
|
182
|
-
...comReqOpt,
|
|
183
|
-
method: "GET",
|
|
184
|
-
protocol: "http",
|
|
185
|
-
}, reqData);
|
|
186
|
-
}
|
|
187
|
-
UtilCom.httpGet = httpGet;
|
|
188
|
-
/**重复一个 https POST请求并接受数据
|
|
189
|
-
* @async
|
|
190
|
-
* @param comReqOpt - 网络请求选项
|
|
191
|
-
* @param reqData - 数据对象
|
|
192
|
-
* @param verifyFn - 有效性验证函数
|
|
193
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
194
|
-
* @returns 结果 undefined 为未能成功接收
|
|
195
|
-
*/
|
|
196
|
-
function httpsRepeatPost(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
197
|
-
return repeatJsonComReq({
|
|
198
|
-
...comReqOpt,
|
|
199
|
-
method: "POST",
|
|
200
|
-
protocol: "https",
|
|
201
|
-
}, reqData, verifyFn, repeatOpt);
|
|
202
|
-
}
|
|
203
|
-
UtilCom.httpsRepeatPost = httpsRepeatPost;
|
|
204
|
-
/**重复一个 http POST请求并接受数据
|
|
205
|
-
* @async
|
|
206
|
-
* @param comReqOpt - 网络请求选项
|
|
207
|
-
* @param reqData - 数据对象
|
|
208
|
-
* @param verifyFn - 有效性验证函数
|
|
209
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
210
|
-
* @returns 结果 undefined 为未能成功接收
|
|
211
|
-
*/
|
|
212
|
-
function httpRepeatPost(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
213
|
-
return repeatJsonComReq({
|
|
214
|
-
...comReqOpt,
|
|
215
|
-
method: "POST",
|
|
216
|
-
protocol: "http",
|
|
217
|
-
}, reqData, verifyFn, repeatOpt);
|
|
218
|
-
}
|
|
219
|
-
UtilCom.httpRepeatPost = httpRepeatPost;
|
|
220
|
-
/**重复一个 https GET 请求并接受数据
|
|
221
|
-
* @async
|
|
222
|
-
* @param comReqOpt - 网络请求选项
|
|
223
|
-
* @param reqData - 数据对象
|
|
224
|
-
* @param verifyFn - 有效性验证函数
|
|
225
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
226
|
-
* @returns 结果 undefined 为未能成功接收
|
|
227
|
-
*/
|
|
228
|
-
function httpsRepeatGet(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
229
|
-
return repeatJsonComReq({
|
|
230
|
-
...comReqOpt,
|
|
231
|
-
method: "GET",
|
|
232
|
-
protocol: "https",
|
|
233
|
-
}, reqData, verifyFn, repeatOpt);
|
|
234
|
-
}
|
|
235
|
-
UtilCom.httpsRepeatGet = httpsRepeatGet;
|
|
236
|
-
/**重复一个 http GET 请求并接受数据
|
|
237
|
-
* @async
|
|
238
|
-
* @param comReqOpt - 网络请求选项
|
|
239
|
-
* @param reqData - 数据对象
|
|
240
|
-
* @param verifyFn - 有效性验证函数
|
|
241
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
242
|
-
* @returns 结果 undefined 为未能成功接收
|
|
243
|
-
*/
|
|
244
|
-
function httpRepeatGet(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
245
|
-
return repeatJsonComReq({
|
|
246
|
-
...comReqOpt,
|
|
247
|
-
method: "GET",
|
|
248
|
-
protocol: "http",
|
|
249
|
-
}, reqData, verifyFn, repeatOpt);
|
|
250
|
-
}
|
|
251
|
-
UtilCom.httpRepeatGet = httpRepeatGet;
|
|
252
|
-
})(UtilCom || (exports.UtilCom = UtilCom = {}));
|
|
240
|
+
}
|
|
241
|
+
exports.UtilCom = UtilCom;
|
|
242
|
+
if (false)
|
|
243
|
+
((async () => {
|
|
244
|
+
const t = await UtilCom.https().post().accept('json').send('json')
|
|
245
|
+
.repeat({
|
|
246
|
+
hostname: 'httpbin.org',
|
|
247
|
+
path: '/post',
|
|
248
|
+
timeout: 10000
|
|
249
|
+
}, () => UtilSymbol_1.Success, {}, { test: 1 });
|
|
250
|
+
console.log(t);
|
|
251
|
+
})());
|