@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 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 ComPostOption = {
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
- /**方式 post为 POST */
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 RepeatPostOpt = Partial<{
21
- /**httppost 超时中断时间限制 */
22
- postTimeLimit?: number;
23
- }> & RepeatPromiseOpt;
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 options - 参数对象
30
- * @param timeLimit - 超时时间/秒 最小为10秒
31
- * @returns 结果 null 为未能成功接收
51
+ * @param json - 数据对象
52
+ * @param comReqOpt - 请求参数
53
+ * @returns 结果 undefined 为未能成功接收
32
54
  */
33
- function httpsPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | undefined>;
34
- /**发送一个 http POST请求并接受数据
55
+ function httpsGet(json: JObject, comReqOpt: Omit<ComRequestOption, 'protocol' | 'method'>): Promise<HttpResp | undefined>;
56
+ /**发送一个 http GET 请求并接受数据
35
57
  * @async
36
- * @param json - 数据对象
37
- * @param options - 参数对象
38
- * @param timeLimit - 超时时间/秒 最小为10秒
39
- * @returns 结果 null 为未能成功接收
58
+ * @param json - 数据对象
59
+ * @param comReqOpt - 请求参数
60
+ * @returns 结果 undefined 为未能成功接收
40
61
  */
41
- function httpPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | undefined>;
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 options - 参数对象
46
- * @param verifyFn - 判断有效性函数
47
- * @param opt - 可选参数
48
- * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
49
- * @param opt.count - 重试次数 默认3
50
- * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
51
- * @param opt.try_delay - 重试间隔 秒 默认0
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, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | undefined>, opt?: RepeatPostOpt): Promise<RepeatPromiseResult<JObject | undefined> | undefined>;
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 options - 参数对象
60
- * @param verifyFn - 判断有效性函数
61
- * @param opt - 可选参数
62
- * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
63
- * @param opt.count - 重试次数 默认3
64
- * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
65
- * @param opt.try_delay - 重试间隔 秒 默认0
66
- * @returns 结果 null 为未能成功接收
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 httpRepeatPost(json: JObject, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | undefined>, opt?: RepeatPostOpt): Promise<RepeatPromiseResult<JObject | undefined> | undefined>;
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
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 https = __importStar(require("https"));
28
- const http = __importStar(require("http"));
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 options - 参数对象
17
+ * @param comReqOpt - 请求参数
38
18
  * @param timeLimit - 超时时间/秒 最小为10秒
39
19
  * @returns 结果 undefined 为未能成功接收
40
20
  */
41
- function post(posttype, json, options, timeLimit) {
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 jsonStr = UtilFunctions_1.UtilFunc.stringifyJToken(json);
48
- const funcName = `s${posttype}Psot`;
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 + " 接收反馈错误: resdata 为空");
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 + " 接受信息:", UtilFunctions_1.UtilFunc.stringifyJToken(obj));
78
- resolve(obj);
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
- let req = null;
96
- if (posttype === "https")
97
- req = https.request(options, resFunc);
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
- req.write(jsonStr);
98
+ if (isHttps)
99
+ req.write(jsonStr);
112
100
  req.end();
113
101
  });
114
102
  }
115
- /**发送一个 https POST请求并接受数据
103
+ /**通用重复post处理
116
104
  * @async
117
105
  * @param json - 数据对象
118
- * @param options - 参数对象
119
- * @param timeLimit - 超时时间/秒 最小为10秒
120
- * @returns 结果 null 为未能成功接收
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, options, timeLimit) {
123
- return post("https", json, options, timeLimit);
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 options - 参数对象
130
- * @param timeLimit - 超时时间/秒 最小为10秒
131
- * @returns 结果 null 为未能成功接收
135
+ * @param json - 数据对象
136
+ * @param comReqOpt - 请求参数
137
+ * @returns 结果 undefined 为未能成功接收
132
138
  */
133
- function httpPost(json, options, timeLimit) {
134
- return post("http", json, options, timeLimit);
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
- /**通用重复post处理
147
+ /**发送一个 https GET 请求并接受数据
138
148
  * @async
139
- * @param posttype - post类型
140
- * @param json - 数据对象
141
- * @param options - 参数对象
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
- async function repeatPost(posttype, json, options, verifyFn, opt = {}) {
151
- opt.count = opt.count ?? 3;
152
- opt.tryInterval = opt.tryInterval ?? 180;
153
- opt.tryDelay = opt.tryDelay ?? 1;
154
- const procFn = () => post(posttype, json, options, opt.postTimeLimit);
155
- return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, opt);
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 options - 参数对象
161
- * @param verifyFn - 判断有效性函数
162
- * @param opt - 可选参数
163
- * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
164
- * @param opt.count - 重试次数 默认3
165
- * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
166
- * @param opt.try_delay - 重试间隔 秒 默认0
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, options, verifyFn, opt) {
170
- return repeatPost("https", json, options, verifyFn, opt);
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 options - 参数对象
178
- * @param verifyFn - 判断有效性函数
179
- * @param opt - 可选参数
180
- * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
181
- * @param opt.count - 重试次数 默认3
182
- * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
183
- * @param opt.try_delay - 重试间隔 秒 默认0
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, options, verifyFn, opt) {
187
- return repeatPost("http", json, options, verifyFn, opt);
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 = {}));
@@ -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 - 重试间隔时间 默认0
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>;
@@ -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 - 重试间隔时间 默认0
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.170",
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 * as https from 'https';
3
- import * as http from 'http';
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 ComPostOption = {
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
- /**方式 post为 POST */
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 RepeatPostOpt = Partial<{
26
- /**httppost 超时中断时间限制 */
27
- postTimeLimit? : number;
28
- }> & RepeatPromiseOpt;
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 options - 参数对象
44
+ * @param comReqOpt - 请求参数
37
45
  * @param timeLimit - 超时时间/秒 最小为10秒
38
46
  * @returns 结果 undefined 为未能成功接收
39
47
  */
40
- function post(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|undefined>{
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 jsonStr = UtilFunc.stringifyJToken(json);
47
- const funcName = `s${posttype}Psot`;
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+" 接收反馈错误: resdata 为空");
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+" 接受信息:",UtilFunc.stringifyJToken(obj));
81
- resolve(obj);
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
- let req:http.ClientRequest=null as any as http.ClientRequest;
98
- if(posttype === "https")
99
- req = https.request(options, resFunc);
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
- /**发送一个 https POST请求并接受数据
137
+ /**通用重复post处理
122
138
  * @async
123
139
  * @param json - 数据对象
124
- * @param options - 参数对象
125
- * @param timeLimit - 超时时间/秒 最小为10秒
126
- * @returns 结果 null 为未能成功接收
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
- export function httpsPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|undefined>{
129
- return post("https",json,options,timeLimit);
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
- /**发送一个 http POST请求并接受数据
156
+ /**发送一个 https POST 请求并接受数据
133
157
  * @async
134
- * @param json - 数据对象
135
- * @param options - 参数对象
136
- * @param timeLimit - 超时时间/秒 最小为10秒
137
- * @returns 结果 null 为未能成功接收
158
+ * @param json - 数据对象
159
+ * @param comReqOpt - 请求参数
160
+ * @returns 结果 undefined 为未能成功接收
138
161
  */
139
- export function httpPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|undefined>{
140
- return post("http",json,options,timeLimit);
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 posttype - post类型
148
- * @param json - 数据对象
149
- * @param options - 参数对象
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
- async function repeatPost(posttype:"http"|"https",json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|undefined>,opt:RepeatPostOpt={}):
159
- Promise<RepeatPromiseResult<JObject|undefined>|undefined>{
160
- opt.count = opt.count??3;
161
- opt.tryInterval = opt.tryInterval??180;
162
- opt.tryDelay = opt.tryDelay??1;
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 options - 参数对象
173
- * @param verifyFn - 判断有效性函数
174
- * @param opt - 可选参数
175
- * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
176
- * @param opt.count - 重试次数 默认3
177
- * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
178
- * @param opt.try_delay - 重试间隔 秒 默认0
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,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|undefined>,opt?:RepeatPostOpt):
182
- Promise<RepeatPromiseResult<JObject|undefined>|undefined>{
183
- return repeatPost("https",json,options,verifyFn,opt);
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 options - 参数对象
191
- * @param verifyFn - 判断有效性函数
192
- * @param opt - 可选参数
193
- * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
194
- * @param opt.count - 重试次数 默认3
195
- * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
196
- * @param opt.try_delay - 重试间隔 秒 默认0
197
- * @returns 结果 null 为未能成功接收
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 httpRepeatPost(json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|undefined>,opt?:RepeatPostOpt):
200
- Promise<RepeatPromiseResult<JObject|undefined>|undefined>{
201
- return repeatPost("http",json,options,verifyFn,opt);
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
  }
@@ -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 - 重试间隔时间 默认0
229
+ * @param opt.tryDelay - 重试间隔时间/秒 默认 0
230
230
  * @returns 结果 undefined 为全部失败/超时
231
231
  */
232
232
  @LogTimeAsync("repeatPromise ",true)