@zwa73/utils 1.0.170 → 1.0.172
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 +82 -40
- package/dist/UtilCom.js +152 -91
- package/dist/UtilFunctions.d.ts +1 -1
- package/dist/UtilFunctions.js +1 -1
- package/package.json +2 -1
- package/src/UtilCom.ts +171 -83
- package/src/UtilFunctions.ts +1 -1
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { AnyString, JObject, ReqVerifyFn } from "./UtilInterfaces";
|
|
2
|
+
import http from 'http';
|
|
2
3
|
import { RepeatPromiseOpt, RepeatPromiseResult } from "./UtilFunctions";
|
|
3
|
-
export type
|
|
4
|
+
export type ComRequestOption = {
|
|
5
|
+
/**超时时间/秒 最小为10秒 */
|
|
6
|
+
timeLimit?: number;
|
|
7
|
+
/**请求协议 */
|
|
8
|
+
protocol: 'http' | 'https';
|
|
4
9
|
/**请求域名 */
|
|
5
10
|
hostname: string;
|
|
6
11
|
/**请求路径 */
|
|
7
12
|
path: string;
|
|
8
|
-
|
|
9
|
-
method: 'POST';
|
|
13
|
+
/**请求方式 post 为 */
|
|
14
|
+
method: 'POST' | 'GET';
|
|
10
15
|
/**端口 */
|
|
11
16
|
port?: number;
|
|
12
17
|
/**请求头 */
|
|
@@ -17,54 +22,91 @@ export type ComPostOption = {
|
|
|
17
22
|
'Content-Length'?: number;
|
|
18
23
|
};
|
|
19
24
|
};
|
|
20
|
-
type
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
type HttpResp = {
|
|
26
|
+
/**响应头 */
|
|
27
|
+
headers: http.IncomingHttpHeaders;
|
|
28
|
+
/**响应状态码 */
|
|
29
|
+
statusCode?: number;
|
|
30
|
+
/**响应数据 */
|
|
31
|
+
data: JObject;
|
|
32
|
+
};
|
|
24
33
|
/**网络工具 */
|
|
25
34
|
export declare namespace UtilCom {
|
|
26
|
-
/**发送一个 https POST请求并接受数据
|
|
35
|
+
/**发送一个 https POST 请求并接受数据
|
|
36
|
+
* @async
|
|
37
|
+
* @param json - 数据对象
|
|
38
|
+
* @param comReqOpt - 请求参数
|
|
39
|
+
* @returns 结果 undefined 为未能成功接收
|
|
40
|
+
*/
|
|
41
|
+
function httpsPost(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>): Promise<HttpResp | undefined>;
|
|
42
|
+
/**发送一个 http POST 请求并接受数据
|
|
43
|
+
* @async
|
|
44
|
+
* @param json - 数据对象
|
|
45
|
+
* @param comReqOpt - 请求参数
|
|
46
|
+
* @returns 结果 undefined 为未能成功接收
|
|
47
|
+
*/
|
|
48
|
+
function httpPost(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>): Promise<HttpResp | undefined>;
|
|
49
|
+
/**发送一个 https GET 请求并接受数据
|
|
27
50
|
* @async
|
|
28
|
-
* @param json
|
|
29
|
-
* @param
|
|
30
|
-
* @
|
|
31
|
-
* @returns 结果 null 为未能成功接收
|
|
51
|
+
* @param json - 数据对象
|
|
52
|
+
* @param comReqOpt - 请求参数
|
|
53
|
+
* @returns 结果 undefined 为未能成功接收
|
|
32
54
|
*/
|
|
33
|
-
function
|
|
34
|
-
/**发送一个 http
|
|
55
|
+
function httpsGet(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>): Promise<HttpResp | undefined>;
|
|
56
|
+
/**发送一个 http GET 请求并接受数据
|
|
35
57
|
* @async
|
|
36
|
-
* @param json
|
|
37
|
-
* @param
|
|
38
|
-
* @
|
|
39
|
-
* @returns 结果 null 为未能成功接收
|
|
58
|
+
* @param json - 数据对象
|
|
59
|
+
* @param comReqOpt - 请求参数
|
|
60
|
+
* @returns 结果 undefined 为未能成功接收
|
|
40
61
|
*/
|
|
41
|
-
function
|
|
62
|
+
function httpGet(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>): Promise<HttpResp | undefined>;
|
|
42
63
|
/**重复一个 https POST请求并接受数据
|
|
43
64
|
* @async
|
|
44
|
-
* @param json
|
|
45
|
-
* @param
|
|
46
|
-
* @param verifyFn
|
|
47
|
-
* @param
|
|
48
|
-
* @param opt.
|
|
49
|
-
* @param opt.
|
|
50
|
-
* @param opt.
|
|
51
|
-
* @
|
|
52
|
-
* @returns 结果 null 为未能成功接收
|
|
65
|
+
* @param json - 数据对象
|
|
66
|
+
* @param comReqOpt - 请求参数
|
|
67
|
+
* @param verifyFn - 判断有效性函数
|
|
68
|
+
* @param repeatOpt - 可选参数
|
|
69
|
+
* @param opt.count - 重试次数 默认3
|
|
70
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
71
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
72
|
+
* @returns 结果 undefined 为未能成功接收
|
|
53
73
|
*/
|
|
54
|
-
function httpsRepeatPost(json: JObject,
|
|
74
|
+
function httpsRepeatPost(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<HttpResp | undefined> | undefined>;
|
|
55
75
|
/**重复一个 http POST请求并接受数据
|
|
56
|
-
* Object ()
|
|
57
76
|
* @async
|
|
58
|
-
* @param json
|
|
59
|
-
* @param
|
|
60
|
-
* @param verifyFn
|
|
61
|
-
* @param
|
|
62
|
-
* @param opt.
|
|
63
|
-
* @param opt.
|
|
64
|
-
* @param opt.
|
|
65
|
-
* @
|
|
66
|
-
|
|
77
|
+
* @param json - 数据对象
|
|
78
|
+
* @param comReqOpt - 请求参数
|
|
79
|
+
* @param verifyFn - 判断有效性函数
|
|
80
|
+
* @param repeatOpt - 可选参数
|
|
81
|
+
* @param opt.count - 重试次数 默认3
|
|
82
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
83
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
84
|
+
* @returns 结果 undefined 为未能成功接收
|
|
85
|
+
*/
|
|
86
|
+
function httpRepeatPost(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<HttpResp | undefined> | undefined>;
|
|
87
|
+
/**重复一个 https GET 请求并接受数据
|
|
88
|
+
* @async
|
|
89
|
+
* @param json - 数据对象
|
|
90
|
+
* @param comReqOpt - 请求参数
|
|
91
|
+
* @param verifyFn - 判断有效性函数
|
|
92
|
+
* @param repeatOpt - 可选参数
|
|
93
|
+
* @param opt.count - 重试次数 默认3
|
|
94
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
95
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
96
|
+
* @returns 结果 undefined 为未能成功接收
|
|
97
|
+
*/
|
|
98
|
+
function httpsRepeatGet(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<HttpResp | undefined> | undefined>;
|
|
99
|
+
/**重复一个 http GET 请求并接受数据
|
|
100
|
+
* @async
|
|
101
|
+
* @param json - 数据对象
|
|
102
|
+
* @param comReqOpt - 请求参数
|
|
103
|
+
* @param verifyFn - 判断有效性函数
|
|
104
|
+
* @param repeatOpt - 可选参数
|
|
105
|
+
* @param opt.count - 重试次数 默认3
|
|
106
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
107
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
108
|
+
* @returns 结果 undefined 为未能成功接收
|
|
67
109
|
*/
|
|
68
|
-
function
|
|
110
|
+
function httpRepeatGet(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<HttpResp | undefined> | undefined>;
|
|
69
111
|
}
|
|
70
112
|
export {};
|
package/dist/UtilCom.js
CHANGED
|
@@ -1,51 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
4
|
};
|
|
25
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
6
|
exports.UtilCom = void 0;
|
|
27
|
-
const
|
|
28
|
-
const
|
|
7
|
+
const https_1 = __importDefault(require("https"));
|
|
8
|
+
const http_1 = __importDefault(require("http"));
|
|
29
9
|
const UtilLogger_1 = require("./UtilLogger");
|
|
30
10
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
11
|
+
const querystring_1 = __importDefault(require("querystring"));
|
|
31
12
|
/**网络工具 */
|
|
32
13
|
var UtilCom;
|
|
33
14
|
(function (UtilCom) {
|
|
34
15
|
/**通用post处理
|
|
35
|
-
* @param posttype - post类型
|
|
36
16
|
* @param json - 数据对象
|
|
37
|
-
* @param
|
|
17
|
+
* @param comReqOpt - 请求参数
|
|
38
18
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
39
19
|
* @returns 结果 undefined 为未能成功接收
|
|
40
20
|
*/
|
|
41
|
-
function
|
|
21
|
+
function comReq(json, comReqOpt) {
|
|
22
|
+
let { protocol, timeLimit, ...baseOpt } = comReqOpt;
|
|
42
23
|
//转换为毫秒
|
|
43
24
|
const hasTimeLimit = (timeLimit ? timeLimit >= 10 : false);
|
|
44
25
|
if (hasTimeLimit && timeLimit != undefined)
|
|
45
26
|
timeLimit *= 1000;
|
|
46
27
|
const fixlimit = timeLimit;
|
|
47
|
-
const
|
|
48
|
-
const
|
|
28
|
+
const isPost = baseOpt.method == "POST";
|
|
29
|
+
const isHttps = protocol == "https";
|
|
30
|
+
const jsonStr = isPost ? UtilFunctions_1.UtilFunc.stringifyJToken(json) : '';
|
|
31
|
+
const funcName = `${protocol}${baseOpt.method}`;
|
|
32
|
+
if (!isPost)
|
|
33
|
+
baseOpt.path += `?${querystring_1.default.stringify(json)}`;
|
|
49
34
|
return new Promise((resolve, rejecte) => {
|
|
50
35
|
const resFunc = (res) => {
|
|
51
36
|
try {
|
|
@@ -68,14 +53,18 @@ var UtilCom;
|
|
|
68
53
|
});
|
|
69
54
|
res.on('end', () => {
|
|
70
55
|
if (resdata == "") {
|
|
71
|
-
UtilLogger_1.SLogger.warn(funcName
|
|
56
|
+
UtilLogger_1.SLogger.warn(`${funcName} 接收反馈错误: resdata 为空`);
|
|
72
57
|
resolve(undefined);
|
|
73
58
|
return;
|
|
74
59
|
}
|
|
75
60
|
try {
|
|
76
61
|
const obj = JSON.parse(resdata);
|
|
77
|
-
UtilLogger_1.SLogger.http(funcName
|
|
78
|
-
resolve(
|
|
62
|
+
UtilLogger_1.SLogger.http(`${funcName} 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj));
|
|
63
|
+
resolve({
|
|
64
|
+
headers: res.headers,
|
|
65
|
+
statusCode: res.statusCode,
|
|
66
|
+
data: obj,
|
|
67
|
+
});
|
|
79
68
|
return;
|
|
80
69
|
}
|
|
81
70
|
catch (e) {
|
|
@@ -92,11 +81,9 @@ var UtilCom;
|
|
|
92
81
|
}
|
|
93
82
|
};
|
|
94
83
|
//路由 http/https
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
else if (posttype === "http")
|
|
99
|
-
req = http.request(options, resFunc);
|
|
84
|
+
const req = isHttps
|
|
85
|
+
? https_1.default.request(baseOpt, resFunc)
|
|
86
|
+
: http_1.default.request(baseOpt, resFunc);
|
|
100
87
|
//请求超时
|
|
101
88
|
if (hasTimeLimit) {
|
|
102
89
|
req.setTimeout(fixlimit, () => {
|
|
@@ -108,83 +95,157 @@ var UtilCom;
|
|
|
108
95
|
UtilLogger_1.SLogger.warn(`${funcName} 发送请求错误:${e}`);
|
|
109
96
|
resolve(undefined);
|
|
110
97
|
});
|
|
111
|
-
|
|
98
|
+
if (isHttps)
|
|
99
|
+
req.write(jsonStr);
|
|
112
100
|
req.end();
|
|
113
101
|
});
|
|
114
102
|
}
|
|
115
|
-
|
|
103
|
+
/**通用重复post处理
|
|
116
104
|
* @async
|
|
117
105
|
* @param json - 数据对象
|
|
118
|
-
* @param
|
|
119
|
-
* @param
|
|
120
|
-
* @
|
|
106
|
+
* @param comReqOpt - 请求参数
|
|
107
|
+
* @param verifyFn - 判断有效性函数
|
|
108
|
+
* @param repeatOpt - 可选参数
|
|
109
|
+
* @param opt.count - 重试次数 默认3
|
|
110
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
111
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
112
|
+
* @returns 结果 undefined 为未能成功接收
|
|
113
|
+
*/
|
|
114
|
+
async function repeatComReq(json, comReqOpt, verifyFn, repeatOpt = {}) {
|
|
115
|
+
repeatOpt.tryDelay = repeatOpt.tryDelay ?? 1;
|
|
116
|
+
const procFn = () => comReq(json, comReqOpt);
|
|
117
|
+
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatOpt);
|
|
118
|
+
}
|
|
119
|
+
/**发送一个 https POST 请求并接受数据
|
|
120
|
+
* @async
|
|
121
|
+
* @param json - 数据对象
|
|
122
|
+
* @param comReqOpt - 请求参数
|
|
123
|
+
* @returns 结果 undefined 为未能成功接收
|
|
121
124
|
*/
|
|
122
|
-
function httpsPost(json,
|
|
123
|
-
return
|
|
125
|
+
function httpsPost(json, comReqOpt) {
|
|
126
|
+
return comReq(json, {
|
|
127
|
+
...comReqOpt,
|
|
128
|
+
method: "POST",
|
|
129
|
+
protocol: "https",
|
|
130
|
+
});
|
|
124
131
|
}
|
|
125
132
|
UtilCom.httpsPost = httpsPost;
|
|
126
|
-
/**发送一个 http POST请求并接受数据
|
|
133
|
+
/**发送一个 http POST 请求并接受数据
|
|
127
134
|
* @async
|
|
128
|
-
* @param json
|
|
129
|
-
* @param
|
|
130
|
-
* @
|
|
131
|
-
* @returns 结果 null 为未能成功接收
|
|
135
|
+
* @param json - 数据对象
|
|
136
|
+
* @param comReqOpt - 请求参数
|
|
137
|
+
* @returns 结果 undefined 为未能成功接收
|
|
132
138
|
*/
|
|
133
|
-
function httpPost(json,
|
|
134
|
-
return
|
|
139
|
+
function httpPost(json, comReqOpt) {
|
|
140
|
+
return comReq(json, {
|
|
141
|
+
...comReqOpt,
|
|
142
|
+
method: "POST",
|
|
143
|
+
protocol: "http",
|
|
144
|
+
});
|
|
135
145
|
}
|
|
136
146
|
UtilCom.httpPost = httpPost;
|
|
137
|
-
|
|
147
|
+
/**发送一个 https GET 请求并接受数据
|
|
138
148
|
* @async
|
|
139
|
-
* @param
|
|
140
|
-
* @param
|
|
141
|
-
* @
|
|
142
|
-
* @param verifyFn - 判断有效性函数
|
|
143
|
-
* @param opt - 可选参数
|
|
144
|
-
* @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
|
|
145
|
-
* @param opt.count - 重试次数 默认3
|
|
146
|
-
* @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
|
|
147
|
-
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
148
|
-
* @returns 结果 null 为未能成功接收
|
|
149
|
+
* @param json - 数据对象
|
|
150
|
+
* @param comReqOpt - 请求参数
|
|
151
|
+
* @returns 结果 undefined 为未能成功接收
|
|
149
152
|
*/
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
153
|
+
function httpsGet(json, comReqOpt) {
|
|
154
|
+
return comReq(json, {
|
|
155
|
+
...comReqOpt,
|
|
156
|
+
method: "GET",
|
|
157
|
+
protocol: "https",
|
|
158
|
+
});
|
|
156
159
|
}
|
|
160
|
+
UtilCom.httpsGet = httpsGet;
|
|
161
|
+
/**发送一个 http GET 请求并接受数据
|
|
162
|
+
* @async
|
|
163
|
+
* @param json - 数据对象
|
|
164
|
+
* @param comReqOpt - 请求参数
|
|
165
|
+
* @returns 结果 undefined 为未能成功接收
|
|
166
|
+
*/
|
|
167
|
+
function httpGet(json, comReqOpt) {
|
|
168
|
+
return comReq(json, {
|
|
169
|
+
...comReqOpt,
|
|
170
|
+
method: "GET",
|
|
171
|
+
protocol: "http",
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
UtilCom.httpGet = httpGet;
|
|
157
175
|
/**重复一个 https POST请求并接受数据
|
|
158
176
|
* @async
|
|
159
|
-
* @param json
|
|
160
|
-
* @param
|
|
161
|
-
* @param verifyFn
|
|
162
|
-
* @param
|
|
163
|
-
* @param opt.
|
|
164
|
-
* @param opt.
|
|
165
|
-
* @param opt.
|
|
166
|
-
* @
|
|
167
|
-
* @returns 结果 null 为未能成功接收
|
|
177
|
+
* @param json - 数据对象
|
|
178
|
+
* @param comReqOpt - 请求参数
|
|
179
|
+
* @param verifyFn - 判断有效性函数
|
|
180
|
+
* @param repeatOpt - 可选参数
|
|
181
|
+
* @param opt.count - 重试次数 默认3
|
|
182
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
183
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
184
|
+
* @returns 结果 undefined 为未能成功接收
|
|
168
185
|
*/
|
|
169
|
-
function httpsRepeatPost(json,
|
|
170
|
-
return
|
|
186
|
+
function httpsRepeatPost(json, comReqOpt, verifyFn, repeatOpt) {
|
|
187
|
+
return repeatComReq(json, {
|
|
188
|
+
...comReqOpt,
|
|
189
|
+
method: "POST",
|
|
190
|
+
protocol: "https",
|
|
191
|
+
}, verifyFn, repeatOpt);
|
|
171
192
|
}
|
|
172
193
|
UtilCom.httpsRepeatPost = httpsRepeatPost;
|
|
173
194
|
/**重复一个 http POST请求并接受数据
|
|
174
|
-
* Object ()
|
|
175
195
|
* @async
|
|
176
|
-
* @param json
|
|
177
|
-
* @param
|
|
178
|
-
* @param verifyFn
|
|
179
|
-
* @param
|
|
180
|
-
* @param opt.
|
|
181
|
-
* @param opt.
|
|
182
|
-
* @param opt.
|
|
183
|
-
* @
|
|
184
|
-
* @returns 结果 null 为未能成功接收
|
|
196
|
+
* @param json - 数据对象
|
|
197
|
+
* @param comReqOpt - 请求参数
|
|
198
|
+
* @param verifyFn - 判断有效性函数
|
|
199
|
+
* @param repeatOpt - 可选参数
|
|
200
|
+
* @param opt.count - 重试次数 默认3
|
|
201
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
202
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
203
|
+
* @returns 结果 undefined 为未能成功接收
|
|
185
204
|
*/
|
|
186
|
-
function httpRepeatPost(json,
|
|
187
|
-
return
|
|
205
|
+
function httpRepeatPost(json, comReqOpt, verifyFn, repeatOpt) {
|
|
206
|
+
return repeatComReq(json, {
|
|
207
|
+
...comReqOpt,
|
|
208
|
+
method: "POST",
|
|
209
|
+
protocol: "http",
|
|
210
|
+
}, verifyFn, repeatOpt);
|
|
188
211
|
}
|
|
189
212
|
UtilCom.httpRepeatPost = httpRepeatPost;
|
|
213
|
+
/**重复一个 https GET 请求并接受数据
|
|
214
|
+
* @async
|
|
215
|
+
* @param json - 数据对象
|
|
216
|
+
* @param comReqOpt - 请求参数
|
|
217
|
+
* @param verifyFn - 判断有效性函数
|
|
218
|
+
* @param repeatOpt - 可选参数
|
|
219
|
+
* @param opt.count - 重试次数 默认3
|
|
220
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
221
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
222
|
+
* @returns 结果 undefined 为未能成功接收
|
|
223
|
+
*/
|
|
224
|
+
function httpsRepeatGet(json, comReqOpt, verifyFn, repeatOpt) {
|
|
225
|
+
return repeatComReq(json, {
|
|
226
|
+
...comReqOpt,
|
|
227
|
+
method: "GET",
|
|
228
|
+
protocol: "https",
|
|
229
|
+
}, verifyFn, repeatOpt);
|
|
230
|
+
}
|
|
231
|
+
UtilCom.httpsRepeatGet = httpsRepeatGet;
|
|
232
|
+
/**重复一个 http GET 请求并接受数据
|
|
233
|
+
* @async
|
|
234
|
+
* @param json - 数据对象
|
|
235
|
+
* @param comReqOpt - 请求参数
|
|
236
|
+
* @param verifyFn - 判断有效性函数
|
|
237
|
+
* @param repeatOpt - 可选参数
|
|
238
|
+
* @param opt.count - 重试次数 默认3
|
|
239
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
240
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
241
|
+
* @returns 结果 undefined 为未能成功接收
|
|
242
|
+
*/
|
|
243
|
+
function httpRepeatGet(json, comReqOpt, verifyFn, repeatOpt) {
|
|
244
|
+
return repeatComReq(json, {
|
|
245
|
+
...comReqOpt,
|
|
246
|
+
method: "GET",
|
|
247
|
+
protocol: "http",
|
|
248
|
+
}, verifyFn, repeatOpt);
|
|
249
|
+
}
|
|
250
|
+
UtilCom.httpRepeatGet = httpRepeatGet;
|
|
190
251
|
})(UtilCom || (exports.UtilCom = UtilCom = {}));
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -113,7 +113,7 @@ export declare class UtilFunc {
|
|
|
113
113
|
* @param opt - 可选参数
|
|
114
114
|
* @param opt.count - 重试次数 默认3
|
|
115
115
|
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
116
|
-
* @param opt.tryDelay -
|
|
116
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 0
|
|
117
117
|
* @returns 结果 undefined 为全部失败/超时
|
|
118
118
|
*/
|
|
119
119
|
static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<T> | undefined>;
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -172,7 +172,7 @@ class UtilFunc {
|
|
|
172
172
|
* @param opt - 可选参数
|
|
173
173
|
* @param opt.count - 重试次数 默认3
|
|
174
174
|
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
175
|
-
* @param opt.tryDelay -
|
|
175
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 0
|
|
176
176
|
* @returns 结果 undefined 为全部失败/超时
|
|
177
177
|
*/
|
|
178
178
|
static async repeatPromise(procFn, verifyFn, opt = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.172",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"json5": "^2.2.3",
|
|
27
27
|
"pathe": "^1.1.2",
|
|
28
28
|
"public-ip": "^6.0.2",
|
|
29
|
+
"querystring": "^0.2.1",
|
|
29
30
|
"tiktoken": "^1.0.7",
|
|
30
31
|
"winston": "^3.10.0",
|
|
31
32
|
"winston-daily-rotate-file": "^4.7.1"
|
package/src/UtilCom.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import { AnyString, JObject, ReqVerifyFn } from "@src/UtilInterfaces";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import https from 'https';
|
|
3
|
+
import http from 'http';
|
|
4
4
|
import { SLogger } from "@src/UtilLogger";
|
|
5
5
|
import { RepeatPromiseOpt, RepeatPromiseResult, UtilFunc } from "@src/UtilFunctions";
|
|
6
|
+
import qs from "querystring";
|
|
6
7
|
|
|
7
|
-
export type
|
|
8
|
+
export type ComRequestOption = {
|
|
9
|
+
/**超时时间/秒 最小为10秒 */
|
|
10
|
+
timeLimit?:number
|
|
11
|
+
/**请求协议 */
|
|
12
|
+
protocol: 'http'|'https';
|
|
8
13
|
/**请求域名 */
|
|
9
14
|
hostname: string;
|
|
10
15
|
/**请求路径 */
|
|
11
16
|
path: string;
|
|
12
|
-
|
|
13
|
-
method: 'POST';
|
|
17
|
+
/**请求方式 post 为 */
|
|
18
|
+
method: 'POST'|'GET';
|
|
14
19
|
/**端口 */
|
|
15
20
|
port?:number;
|
|
16
21
|
/**请求头 */
|
|
@@ -22,29 +27,39 @@ export type ComPostOption = {
|
|
|
22
27
|
}
|
|
23
28
|
}
|
|
24
29
|
|
|
25
|
-
type
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
type HttpResp = {
|
|
31
|
+
/**响应头 */
|
|
32
|
+
headers: http.IncomingHttpHeaders;
|
|
33
|
+
/**响应状态码 */
|
|
34
|
+
statusCode?: number;
|
|
35
|
+
/**响应数据 */
|
|
36
|
+
data: JObject;
|
|
37
|
+
}
|
|
29
38
|
|
|
30
39
|
/**网络工具 */
|
|
31
40
|
export namespace UtilCom{
|
|
32
41
|
|
|
33
42
|
/**通用post处理
|
|
34
|
-
* @param posttype - post类型
|
|
35
43
|
* @param json - 数据对象
|
|
36
|
-
* @param
|
|
44
|
+
* @param comReqOpt - 请求参数
|
|
37
45
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
38
46
|
* @returns 结果 undefined 为未能成功接收
|
|
39
47
|
*/
|
|
40
|
-
function
|
|
48
|
+
function comReq(json:JObject,comReqOpt:ComRequestOption):Promise<HttpResp|undefined>{
|
|
49
|
+
let {protocol,timeLimit,...baseOpt} = comReqOpt;
|
|
50
|
+
|
|
41
51
|
//转换为毫秒
|
|
42
52
|
const hasTimeLimit = (timeLimit ? timeLimit>=10 : false );
|
|
43
53
|
if(hasTimeLimit && timeLimit!=undefined) timeLimit*=1000;
|
|
44
54
|
const fixlimit = timeLimit as number;
|
|
45
55
|
|
|
46
|
-
const
|
|
47
|
-
const
|
|
56
|
+
const isPost = baseOpt.method=="POST";
|
|
57
|
+
const isHttps = protocol=="https";
|
|
58
|
+
|
|
59
|
+
const jsonStr = isPost ? UtilFunc.stringifyJToken(json) : '';
|
|
60
|
+
const funcName = `${protocol}${baseOpt.method}`;
|
|
61
|
+
|
|
62
|
+
if(!isPost) baseOpt.path += `?${qs.stringify(json as any)}`;
|
|
48
63
|
|
|
49
64
|
return new Promise((resolve, rejecte)=>{
|
|
50
65
|
const resFunc = (res:http.IncomingMessage)=>{
|
|
@@ -71,14 +86,18 @@ function post(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLim
|
|
|
71
86
|
|
|
72
87
|
res.on('end',()=>{
|
|
73
88
|
if(resdata==""){
|
|
74
|
-
SLogger.warn(funcName
|
|
89
|
+
SLogger.warn(`${funcName} 接收反馈错误: resdata 为空`);
|
|
75
90
|
resolve(undefined);
|
|
76
91
|
return;
|
|
77
92
|
}
|
|
78
93
|
try{
|
|
79
|
-
const obj = JSON.parse(resdata);
|
|
80
|
-
SLogger.http(funcName
|
|
81
|
-
resolve(
|
|
94
|
+
const obj = JSON.parse(resdata) as JObject;
|
|
95
|
+
SLogger.http(`${funcName} 接受信息:`,UtilFunc.stringifyJToken(obj));
|
|
96
|
+
resolve({
|
|
97
|
+
headers: res.headers,
|
|
98
|
+
statusCode: res.statusCode,
|
|
99
|
+
data: obj,
|
|
100
|
+
});
|
|
82
101
|
return;
|
|
83
102
|
}
|
|
84
103
|
catch(e){
|
|
@@ -94,11 +113,9 @@ function post(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLim
|
|
|
94
113
|
}
|
|
95
114
|
};
|
|
96
115
|
//路由 http/https
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
else if(posttype === "http")
|
|
101
|
-
req = http.request(options, resFunc);
|
|
116
|
+
const req:http.ClientRequest= isHttps
|
|
117
|
+
? https.request(baseOpt, resFunc)
|
|
118
|
+
: http.request(baseOpt, resFunc);
|
|
102
119
|
|
|
103
120
|
//请求超时
|
|
104
121
|
if(hasTimeLimit){
|
|
@@ -112,93 +129,164 @@ function post(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLim
|
|
|
112
129
|
SLogger.warn(`${funcName} 发送请求错误:${e}`);
|
|
113
130
|
resolve(undefined);
|
|
114
131
|
});
|
|
115
|
-
|
|
116
|
-
req.write(jsonStr);
|
|
132
|
+
if(isHttps) req.write(jsonStr);
|
|
117
133
|
req.end();
|
|
118
134
|
});
|
|
119
135
|
}
|
|
120
136
|
|
|
121
|
-
|
|
137
|
+
/**通用重复post处理
|
|
122
138
|
* @async
|
|
123
139
|
* @param json - 数据对象
|
|
124
|
-
* @param
|
|
125
|
-
* @param
|
|
126
|
-
* @
|
|
140
|
+
* @param comReqOpt - 请求参数
|
|
141
|
+
* @param verifyFn - 判断有效性函数
|
|
142
|
+
* @param repeatOpt - 可选参数
|
|
143
|
+
* @param opt.count - 重试次数 默认3
|
|
144
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
145
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
146
|
+
* @returns 结果 undefined 为未能成功接收
|
|
127
147
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
148
|
+
async function repeatComReq(json:JObject,comReqOpt:ComRequestOption,verifyFn?:ReqVerifyFn<JObject|undefined>,repeatOpt:RepeatPromiseOpt={}):
|
|
149
|
+
Promise<RepeatPromiseResult<HttpResp|undefined>|undefined>{
|
|
150
|
+
repeatOpt.tryDelay = repeatOpt.tryDelay??1;
|
|
151
|
+
|
|
152
|
+
const procFn = ()=>comReq(json,comReqOpt);
|
|
153
|
+
return UtilFunc.repeatPromise(procFn,verifyFn,repeatOpt);
|
|
130
154
|
}
|
|
131
155
|
|
|
132
|
-
/**发送一个
|
|
156
|
+
/**发送一个 https POST 请求并接受数据
|
|
133
157
|
* @async
|
|
134
|
-
* @param json
|
|
135
|
-
* @param
|
|
136
|
-
* @
|
|
137
|
-
* @returns 结果 null 为未能成功接收
|
|
158
|
+
* @param json - 数据对象
|
|
159
|
+
* @param comReqOpt - 请求参数
|
|
160
|
+
* @returns 结果 undefined 为未能成功接收
|
|
138
161
|
*/
|
|
139
|
-
export function
|
|
140
|
-
return
|
|
162
|
+
export function httpsPost(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>):Promise<HttpResp|undefined>{
|
|
163
|
+
return comReq(json,{
|
|
164
|
+
...comReqOpt,
|
|
165
|
+
method:"POST",
|
|
166
|
+
protocol:"https",
|
|
167
|
+
});
|
|
141
168
|
}
|
|
142
169
|
|
|
170
|
+
/**发送一个 http POST 请求并接受数据
|
|
171
|
+
* @async
|
|
172
|
+
* @param json - 数据对象
|
|
173
|
+
* @param comReqOpt - 请求参数
|
|
174
|
+
* @returns 结果 undefined 为未能成功接收
|
|
175
|
+
*/
|
|
176
|
+
export function httpPost(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>):Promise<HttpResp|undefined>{
|
|
177
|
+
return comReq(json,{
|
|
178
|
+
...comReqOpt,
|
|
179
|
+
method:"POST",
|
|
180
|
+
protocol:"http",
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**发送一个 https GET 请求并接受数据
|
|
184
|
+
* @async
|
|
185
|
+
* @param json - 数据对象
|
|
186
|
+
* @param comReqOpt - 请求参数
|
|
187
|
+
* @returns 结果 undefined 为未能成功接收
|
|
188
|
+
*/
|
|
189
|
+
export function httpsGet(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>):Promise<HttpResp|undefined>{
|
|
190
|
+
return comReq(json,{
|
|
191
|
+
...comReqOpt,
|
|
192
|
+
method:"GET",
|
|
193
|
+
protocol:"https",
|
|
194
|
+
});
|
|
195
|
+
}
|
|
143
196
|
|
|
144
|
-
|
|
145
|
-
/**通用重复post处理
|
|
197
|
+
/**发送一个 http GET 请求并接受数据
|
|
146
198
|
* @async
|
|
147
|
-
* @param
|
|
148
|
-
* @param
|
|
149
|
-
* @
|
|
150
|
-
* @param verifyFn - 判断有效性函数
|
|
151
|
-
* @param opt - 可选参数
|
|
152
|
-
* @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
|
|
153
|
-
* @param opt.count - 重试次数 默认3
|
|
154
|
-
* @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
|
|
155
|
-
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
156
|
-
* @returns 结果 null 为未能成功接收
|
|
199
|
+
* @param json - 数据对象
|
|
200
|
+
* @param comReqOpt - 请求参数
|
|
201
|
+
* @returns 结果 undefined 为未能成功接收
|
|
157
202
|
*/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const procFn = ()=>post(posttype,json,options,opt.postTimeLimit);
|
|
165
|
-
return UtilFunc.repeatPromise(procFn,verifyFn,opt);
|
|
203
|
+
export function httpGet(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>):Promise<HttpResp|undefined>{
|
|
204
|
+
return comReq(json,{
|
|
205
|
+
...comReqOpt,
|
|
206
|
+
method:"GET",
|
|
207
|
+
protocol:"http",
|
|
208
|
+
});
|
|
166
209
|
}
|
|
167
210
|
|
|
168
211
|
|
|
169
212
|
/**重复一个 https POST请求并接受数据
|
|
170
213
|
* @async
|
|
171
|
-
* @param json
|
|
172
|
-
* @param
|
|
173
|
-
* @param verifyFn
|
|
174
|
-
* @param
|
|
175
|
-
* @param opt.
|
|
176
|
-
* @param opt.
|
|
177
|
-
* @param opt.
|
|
178
|
-
* @
|
|
179
|
-
* @returns 结果 null 为未能成功接收
|
|
214
|
+
* @param json - 数据对象
|
|
215
|
+
* @param comReqOpt - 请求参数
|
|
216
|
+
* @param verifyFn - 判断有效性函数
|
|
217
|
+
* @param repeatOpt - 可选参数
|
|
218
|
+
* @param opt.count - 重试次数 默认3
|
|
219
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
220
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
221
|
+
* @returns 结果 undefined 为未能成功接收
|
|
180
222
|
*/
|
|
181
|
-
export function httpsRepeatPost(json:JObject,
|
|
182
|
-
Promise<RepeatPromiseResult<
|
|
183
|
-
return
|
|
223
|
+
export function httpsRepeatPost(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>,verifyFn?:ReqVerifyFn<JObject|undefined>,repeatOpt?:RepeatPromiseOpt):
|
|
224
|
+
Promise<RepeatPromiseResult<HttpResp|undefined>|undefined>{
|
|
225
|
+
return repeatComReq(json,{
|
|
226
|
+
...comReqOpt,
|
|
227
|
+
method:"POST",
|
|
228
|
+
protocol:"https",
|
|
229
|
+
},verifyFn,repeatOpt);
|
|
184
230
|
}
|
|
185
231
|
|
|
186
232
|
/**重复一个 http POST请求并接受数据
|
|
187
|
-
* Object ()
|
|
188
233
|
* @async
|
|
189
|
-
* @param json
|
|
190
|
-
* @param
|
|
191
|
-
* @param verifyFn
|
|
192
|
-
* @param
|
|
193
|
-
* @param opt.
|
|
194
|
-
* @param opt.
|
|
195
|
-
* @param opt.
|
|
196
|
-
* @
|
|
197
|
-
|
|
234
|
+
* @param json - 数据对象
|
|
235
|
+
* @param comReqOpt - 请求参数
|
|
236
|
+
* @param verifyFn - 判断有效性函数
|
|
237
|
+
* @param repeatOpt - 可选参数
|
|
238
|
+
* @param opt.count - 重试次数 默认3
|
|
239
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
240
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
241
|
+
* @returns 结果 undefined 为未能成功接收
|
|
242
|
+
*/
|
|
243
|
+
export function httpRepeatPost(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>,verifyFn?:ReqVerifyFn<JObject|undefined>,repeatOpt?:RepeatPromiseOpt):
|
|
244
|
+
Promise<RepeatPromiseResult<HttpResp|undefined>|undefined>{
|
|
245
|
+
return repeatComReq(json,{
|
|
246
|
+
...comReqOpt,
|
|
247
|
+
method:"POST",
|
|
248
|
+
protocol:"http",
|
|
249
|
+
},verifyFn,repeatOpt);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**重复一个 https GET 请求并接受数据
|
|
253
|
+
* @async
|
|
254
|
+
* @param json - 数据对象
|
|
255
|
+
* @param comReqOpt - 请求参数
|
|
256
|
+
* @param verifyFn - 判断有效性函数
|
|
257
|
+
* @param repeatOpt - 可选参数
|
|
258
|
+
* @param opt.count - 重试次数 默认3
|
|
259
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
260
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
261
|
+
* @returns 结果 undefined 为未能成功接收
|
|
262
|
+
*/
|
|
263
|
+
export function httpsRepeatGet(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>,verifyFn?:ReqVerifyFn<JObject|undefined>,repeatOpt?:RepeatPromiseOpt):
|
|
264
|
+
Promise<RepeatPromiseResult<HttpResp|undefined>|undefined>{
|
|
265
|
+
return repeatComReq(json,{
|
|
266
|
+
...comReqOpt,
|
|
267
|
+
method:"GET",
|
|
268
|
+
protocol:"https",
|
|
269
|
+
},verifyFn,repeatOpt);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**重复一个 http GET 请求并接受数据
|
|
273
|
+
* @async
|
|
274
|
+
* @param json - 数据对象
|
|
275
|
+
* @param comReqOpt - 请求参数
|
|
276
|
+
* @param verifyFn - 判断有效性函数
|
|
277
|
+
* @param repeatOpt - 可选参数
|
|
278
|
+
* @param opt.count - 重试次数 默认3
|
|
279
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
280
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
281
|
+
* @returns 结果 undefined 为未能成功接收
|
|
198
282
|
*/
|
|
199
|
-
export function
|
|
200
|
-
Promise<RepeatPromiseResult<
|
|
201
|
-
return
|
|
283
|
+
export function httpRepeatGet(json:JObject,comReqOpt:Omit<ComRequestOption,'protocol'|'method'>,verifyFn?:ReqVerifyFn<JObject|undefined>,repeatOpt?:RepeatPromiseOpt):
|
|
284
|
+
Promise<RepeatPromiseResult<HttpResp|undefined>|undefined>{
|
|
285
|
+
return repeatComReq(json,{
|
|
286
|
+
...comReqOpt,
|
|
287
|
+
method:"GET",
|
|
288
|
+
protocol:"http",
|
|
289
|
+
},verifyFn,repeatOpt);
|
|
202
290
|
}
|
|
203
291
|
|
|
204
292
|
}
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -226,7 +226,7 @@ static getNeverResolvedPromise<T>():Promise<T>{
|
|
|
226
226
|
* @param opt - 可选参数
|
|
227
227
|
* @param opt.count - 重试次数 默认3
|
|
228
228
|
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
229
|
-
* @param opt.tryDelay -
|
|
229
|
+
* @param opt.tryDelay - 重试间隔时间/秒 默认 0
|
|
230
230
|
* @returns 结果 undefined 为全部失败/超时
|
|
231
231
|
*/
|
|
232
232
|
@LogTimeAsync("repeatPromise ",true)
|