@zwa73/utils 1.0.198 → 1.0.199
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 +146 -59
- package/dist/UtilCom.js +173 -83
- 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 +214 -119
- package/src/UtilFileTools.ts +0 -1
- package/src/UtilFunctions.ts +39 -27
- package/src/UtilInterfaces.ts +9 -11
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { AnyString, JToken, MPromise, PartialOption, StatusVerifyFn } from "./UtilInterfaces";
|
|
2
2
|
import http from 'http';
|
|
3
|
-
import {
|
|
3
|
+
import { PromiseRetries } from "./UtilFunctions";
|
|
4
|
+
import FormData from "form-data";
|
|
4
5
|
/**网络请求返回值 */
|
|
5
|
-
export type
|
|
6
|
+
export type RequestResult<T> = {
|
|
6
7
|
/**响应头 */
|
|
7
8
|
headers: http.IncomingHttpHeaders;
|
|
8
9
|
/**响应状态码 */
|
|
@@ -11,7 +12,7 @@ export type ComResp<T> = {
|
|
|
11
12
|
data: T;
|
|
12
13
|
};
|
|
13
14
|
/**网络请求选项 */
|
|
14
|
-
export type
|
|
15
|
+
export type RequestOption = {
|
|
15
16
|
/**请求协议 */
|
|
16
17
|
protocol: 'http:' | 'https:';
|
|
17
18
|
/**超时时间/毫秒 最小为10_000 默认无限 */
|
|
@@ -27,41 +28,41 @@ export type ComRequestOption = {
|
|
|
27
28
|
/**请求头 */
|
|
28
29
|
headers?: {
|
|
29
30
|
/**内容类型 */
|
|
30
|
-
'Content-Type'?: 'application/json' | AnyString;
|
|
31
|
+
'Content-Type'?: 'application/json' | 'multipart/form-data' | AnyString;
|
|
31
32
|
/**内容长度 一般无需填写 应为buffer长度而非字符串长度 */
|
|
32
33
|
'Content-Length'?: number;
|
|
33
34
|
};
|
|
34
35
|
} & http.RequestOptions;
|
|
35
|
-
/**
|
|
36
|
-
export type
|
|
36
|
+
/**getquery请求所允许的数据 */
|
|
37
|
+
export type QueryRequestData = NodeJS.Dict<string | number | boolean | readonly string[] | readonly number[] | readonly boolean[] | null>;
|
|
37
38
|
/**请求处理函数 需调用req.end() */
|
|
38
|
-
export type
|
|
39
|
+
export type RequestProcFn = ((req: http.ClientRequest) => MPromise<void>);
|
|
39
40
|
/**数据处理函数 */
|
|
40
|
-
export type
|
|
41
|
-
declare const AcceptTypeList: readonly ["json"];
|
|
41
|
+
export type RequestReduceFn<T> = (acc: T, data: string) => MPromise<T>;
|
|
42
|
+
declare const AcceptTypeList: readonly ["json", "raw"];
|
|
42
43
|
/**可用的接受类型 */
|
|
43
44
|
type AcceptType = typeof AcceptTypeList[number];
|
|
44
|
-
declare const SendTypeList: readonly ["json", "none"];
|
|
45
|
+
declare const SendTypeList: readonly ["json", "query", "formData", "none", "raw"];
|
|
45
46
|
/**可用的发送类型 */
|
|
46
47
|
type SendType = typeof SendTypeList[number];
|
|
47
48
|
/**accept处理数据 */
|
|
48
49
|
type AcceptProc<D, T> = {
|
|
49
50
|
init: D;
|
|
50
|
-
reduce:
|
|
51
|
-
parse: (result:
|
|
51
|
+
reduce: RequestReduceFn<D>;
|
|
52
|
+
parse: (result: RequestResult<D> | undefined) => MPromise<T>;
|
|
52
53
|
};
|
|
53
54
|
/**send处理数据 */
|
|
54
55
|
type SendProc<T extends any[]> = {
|
|
55
|
-
proc: (opt:
|
|
56
|
+
proc: (opt: RequestOption, ...args: T) => MPromise<RequestProcFn>;
|
|
56
57
|
};
|
|
58
|
+
declare const SendNoneProc: SendProc<[]>;
|
|
59
|
+
declare const AcceptRawProc: AcceptProc<string, RequestResult<string> | undefined>;
|
|
57
60
|
/**send处理的参数 */
|
|
58
61
|
type SendParams<T extends SendProc<any>> = T extends SendProc<infer T> ? T : never;
|
|
59
62
|
/**accept处理的结果 */
|
|
60
63
|
type ParseResult<D extends AcceptProc<any, any>> = D extends AcceptProc<any, infer T> ? Awaited<T> : never;
|
|
61
|
-
/**根据方式自动推断json请求的可用数据类型 */
|
|
62
|
-
type JsonType<OPT extends Record<string, any>> = OPT['method'] extends "POST" ? JToken : GetReqData;
|
|
63
64
|
/**网络请求工具 */
|
|
64
|
-
export declare class UtilCom<D extends Partial<
|
|
65
|
+
export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<RequestOption, 'protocol'>>, S extends SendProc<any>, A extends AcceptProc<any, any>> {
|
|
65
66
|
private _data;
|
|
66
67
|
private _send;
|
|
67
68
|
private _accept;
|
|
@@ -69,73 +70,159 @@ export declare class UtilCom<D extends Partial<ComRequestOption>, S extends Send
|
|
|
69
70
|
/**设为https请求 */
|
|
70
71
|
static https(): UtilCom<{
|
|
71
72
|
readonly protocol: "https:";
|
|
72
|
-
},
|
|
73
|
-
proc: () => (req: http.ClientRequest) => undefined;
|
|
74
|
-
}, {
|
|
75
|
-
init: string;
|
|
76
|
-
reduce: (acc: string, dat: string) => string;
|
|
77
|
-
parse: (result: ComResp<string> | undefined) => undefined;
|
|
78
|
-
}>;
|
|
73
|
+
}, SendProc<[]>, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
79
74
|
/**设为http请求 */
|
|
80
75
|
static http(): UtilCom<{
|
|
81
76
|
readonly protocol: "http:";
|
|
82
|
-
},
|
|
83
|
-
proc: () => (req: http.ClientRequest) => undefined;
|
|
84
|
-
}, {
|
|
85
|
-
init: string;
|
|
86
|
-
reduce: (acc: string, dat: string) => string;
|
|
87
|
-
parse: (result: ComResp<string> | undefined) => undefined;
|
|
88
|
-
}>;
|
|
77
|
+
}, SendProc<[]>, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
89
78
|
/**设为get方式的请求 */
|
|
90
79
|
get(): UtilCom<D & {
|
|
91
|
-
|
|
80
|
+
method: "GET";
|
|
92
81
|
}, S, A>;
|
|
93
82
|
/**设为Post方式的请求 */
|
|
94
83
|
post(): UtilCom<D & {
|
|
95
|
-
|
|
84
|
+
method: "POST";
|
|
96
85
|
}, S, A>;
|
|
97
|
-
/**补充参数
|
|
98
|
-
|
|
86
|
+
/**补充参数
|
|
87
|
+
* 将会替换对应字段, 修改headers请用header函数
|
|
88
|
+
*/
|
|
89
|
+
option<OPT extends Partial<RequestOption>>(option: OPT): UtilCom<D & OPT, S, A>;
|
|
90
|
+
/**补充header */
|
|
91
|
+
header<HAD extends http.OutgoingHttpHeaders>(headers: HAD): UtilCom<D & {
|
|
92
|
+
headers: HAD;
|
|
93
|
+
}, S, A>;
|
|
94
|
+
/**设置agent */
|
|
95
|
+
proxyAgent(url: string): this;
|
|
96
|
+
/**添加一段query */
|
|
97
|
+
query(data: QueryRequestData): this;
|
|
98
|
+
/**收发皆为json的预设 */
|
|
99
|
+
json(): UtilCom<D & {
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json";
|
|
102
|
+
};
|
|
103
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
99
104
|
/**收发皆为json的post预设 */
|
|
100
105
|
postJson(): UtilCom<D & {
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
method: "POST";
|
|
107
|
+
} & {
|
|
108
|
+
headers: {
|
|
109
|
+
'Content-Type': "application/json";
|
|
110
|
+
};
|
|
111
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
112
|
+
/**无查询参数获取json的get预设 */
|
|
113
|
+
getJson(): UtilCom<D & {
|
|
114
|
+
method: "GET";
|
|
115
|
+
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
116
|
+
/**有查询参数获取json的get预设 */
|
|
117
|
+
queryJson(): UtilCom<D & {
|
|
118
|
+
method: "GET";
|
|
119
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
120
|
+
/**收发皆为json的https-post预设 */
|
|
121
|
+
static httpsPostJson(): UtilCom<{
|
|
122
|
+
readonly protocol: "https:";
|
|
123
|
+
} & {
|
|
124
|
+
method: "POST";
|
|
125
|
+
} & {
|
|
126
|
+
headers: {
|
|
127
|
+
'Content-Type': "application/json";
|
|
128
|
+
};
|
|
129
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
130
|
+
/**收发皆为json的http-post预设 */
|
|
131
|
+
static httpPostJson(): UtilCom<{
|
|
132
|
+
readonly protocol: "http:";
|
|
133
|
+
} & {
|
|
134
|
+
method: "POST";
|
|
135
|
+
} & {
|
|
136
|
+
headers: {
|
|
137
|
+
'Content-Type': "application/json";
|
|
138
|
+
};
|
|
139
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
140
|
+
/**无查询参数获取json的https-get预设 */
|
|
141
|
+
static httpsGetJson(): UtilCom<{
|
|
142
|
+
readonly protocol: "https:";
|
|
143
|
+
} & {
|
|
144
|
+
method: "GET";
|
|
145
|
+
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
146
|
+
/**有查询参数获取json的https-get预设 */
|
|
147
|
+
static httpsQueryJson(): UtilCom<{
|
|
148
|
+
readonly protocol: "http:";
|
|
149
|
+
} & {
|
|
150
|
+
method: "GET";
|
|
151
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
152
|
+
/**无查询参数获取json的http-get预设 */
|
|
153
|
+
static httpGetJson(): UtilCom<{
|
|
154
|
+
readonly protocol: "http:";
|
|
155
|
+
} & {
|
|
156
|
+
method: "GET";
|
|
157
|
+
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
158
|
+
/**有查询参数获取json的http-get预设 */
|
|
159
|
+
static httpQueryJson(): UtilCom<{
|
|
160
|
+
readonly protocol: "http:";
|
|
161
|
+
} & {
|
|
162
|
+
method: "GET";
|
|
163
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
107
164
|
/**接收数据类型*/
|
|
108
165
|
accept<T extends AcceptType>(t: T): {
|
|
109
166
|
readonly json: UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
110
|
-
readonly
|
|
167
|
+
readonly raw: UtilCom<D, S, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
111
168
|
}[T];
|
|
112
169
|
acceptJson(): UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
113
|
-
|
|
170
|
+
acceptRaw(): UtilCom<D, S, typeof AcceptRawProc>;
|
|
114
171
|
/**发送数据类型*/
|
|
115
172
|
send<T extends SendType>(t: T): {
|
|
116
|
-
readonly json: UtilCom<D
|
|
173
|
+
readonly json: UtilCom<D & {
|
|
174
|
+
headers: {
|
|
175
|
+
"Content-Type": "application/json";
|
|
176
|
+
};
|
|
177
|
+
}, SendProc<[JToken]>, A>;
|
|
178
|
+
readonly query: UtilCom<D, SendProc<[QueryRequestData]>, A>;
|
|
179
|
+
readonly formData: UtilCom<D & {
|
|
180
|
+
headers: {
|
|
181
|
+
"Content-Type": "multipart/form-data";
|
|
182
|
+
};
|
|
183
|
+
}, SendProc<[FormData]>, A>;
|
|
117
184
|
readonly none: UtilCom<D, SendProc<[]>, A>;
|
|
185
|
+
readonly raw: UtilCom<D, SendProc<[(opt: RequestOption) => RequestProcFn]>, A>;
|
|
118
186
|
}[T];
|
|
119
|
-
|
|
120
|
-
|
|
187
|
+
/**利用 req.write 发送一段json */
|
|
188
|
+
sendJson(): UtilCom<D & {
|
|
189
|
+
headers: {
|
|
190
|
+
"Content-Type": "application/json";
|
|
191
|
+
};
|
|
192
|
+
}, SendProc<[JToken]>, A>;
|
|
193
|
+
/**利用 appendQuery 直接将数据附加在path上发送请求 */
|
|
194
|
+
sendQuery(): UtilCom<D, SendProc<[QueryRequestData]>, A>;
|
|
195
|
+
sendFormData(): UtilCom<D & {
|
|
196
|
+
headers: {
|
|
197
|
+
"Content-Type": "multipart/form-data";
|
|
198
|
+
};
|
|
199
|
+
}, SendProc<[FormData]>, A>;
|
|
200
|
+
sendRaw(): UtilCom<D, SendProc<[(opt: RequestOption) => RequestProcFn]>, A>;
|
|
201
|
+
sendNone(): UtilCom<D, typeof SendNoneProc, A>;
|
|
121
202
|
/**发送请求
|
|
122
|
-
* @param option
|
|
123
|
-
* @param datas
|
|
203
|
+
* @param option - 网络请求选项
|
|
204
|
+
* @param datas - 数据对象
|
|
124
205
|
*/
|
|
125
|
-
once(option: PartialOption<
|
|
206
|
+
once(option: PartialOption<RequestOption, D>, ...datas: SendParams<S>): Promise<any>;
|
|
126
207
|
/**重复发送网络请求
|
|
127
|
-
* @param option
|
|
128
|
-
* @param
|
|
129
|
-
* @param
|
|
130
|
-
* @param datas
|
|
208
|
+
* @param option - 网络请求选项
|
|
209
|
+
* @param verify - 有效性验证函数
|
|
210
|
+
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
211
|
+
* @param datas - 数据对象
|
|
131
212
|
*/
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
213
|
+
retry(opt: {
|
|
214
|
+
option: PartialOption<RequestOption, D>;
|
|
215
|
+
verify?: StatusVerifyFn<ParseResult<A>>;
|
|
216
|
+
retries?: PromiseRetries;
|
|
217
|
+
}, ...datas: SendParams<S>): Promise<import("./UtilFunctions").PromiseRetryResult<ParseResult<A>>>;
|
|
218
|
+
/**发送网络请求
|
|
219
|
+
* @param option - 网络请求选项
|
|
220
|
+
* @param proc - 请求处理函数 需调用req.end()
|
|
221
|
+
* @param reduce - 数据处理函数
|
|
222
|
+
* @param init - 初始数据
|
|
138
223
|
*/
|
|
139
|
-
static
|
|
224
|
+
static request<T>(option: RequestOption, proc: RequestProcFn, reduce: RequestReduceFn<T>, init: T): Promise<RequestResult<T> | undefined>;
|
|
225
|
+
/**构建query */
|
|
226
|
+
static buildQuery(base: string, data: QueryRequestData): string;
|
|
140
227
|
}
|
|
141
228
|
export {};
|
package/dist/UtilCom.js
CHANGED
|
@@ -4,21 +4,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.UtilCom = void 0;
|
|
7
|
-
const https_1 = __importDefault(require("https"));
|
|
8
7
|
const http_1 = __importDefault(require("http"));
|
|
8
|
+
const https_1 = __importDefault(require("https"));
|
|
9
9
|
const UtilLogger_1 = require("./UtilLogger");
|
|
10
10
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
11
11
|
const querystring_1 = __importDefault(require("querystring"));
|
|
12
12
|
const UtilSymbol_1 = require("./UtilSymbol");
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
13
|
+
const http_proxy_agent_1 = __importDefault(require("http-proxy-agent"));
|
|
14
|
+
const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
|
|
15
|
+
const AcceptTypeList = ["json", "raw"];
|
|
16
|
+
const SendTypeList = ["json", "query", "formData", "none", "raw"];
|
|
17
|
+
const SendNoneProc = {
|
|
18
|
+
proc: (opt) => (req) => void req.end()
|
|
17
19
|
};
|
|
18
|
-
const
|
|
20
|
+
const AcceptRawProc = {
|
|
19
21
|
init: '',
|
|
20
22
|
reduce: (acc, dat) => acc + dat,
|
|
21
|
-
parse: (result) =>
|
|
23
|
+
parse: (result) => result
|
|
22
24
|
};
|
|
23
25
|
/**网络请求工具 */
|
|
24
26
|
class UtilCom {
|
|
@@ -30,36 +32,94 @@ class UtilCom {
|
|
|
30
32
|
this._send = _send;
|
|
31
33
|
this._accept = _accept;
|
|
32
34
|
}
|
|
33
|
-
//#region
|
|
35
|
+
//#region 流式创建
|
|
34
36
|
/**设为https请求 */
|
|
35
37
|
static https() {
|
|
36
|
-
return new UtilCom({ protocol: 'https:' },
|
|
38
|
+
return new UtilCom({ protocol: 'https:' }, SendNoneProc, AcceptRawProc);
|
|
37
39
|
}
|
|
38
40
|
/**设为http请求 */
|
|
39
41
|
static http() {
|
|
40
|
-
return new UtilCom({ protocol: 'http:' },
|
|
42
|
+
return new UtilCom({ protocol: 'http:' }, SendNoneProc, AcceptRawProc);
|
|
41
43
|
}
|
|
42
44
|
/**设为get方式的请求 */
|
|
43
45
|
get() {
|
|
44
|
-
|
|
46
|
+
this._data.method = 'GET';
|
|
47
|
+
return this;
|
|
45
48
|
}
|
|
46
49
|
/**设为Post方式的请求 */
|
|
47
50
|
post() {
|
|
48
|
-
|
|
51
|
+
this._data.method = 'POST';
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**补充参数
|
|
55
|
+
* 将会替换对应字段, 修改headers请用header函数
|
|
56
|
+
*/
|
|
57
|
+
option(option) {
|
|
58
|
+
this._data = { ...this._data, ...option };
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
/**补充header */
|
|
62
|
+
header(headers) {
|
|
63
|
+
this._data.headers = {
|
|
64
|
+
...this._data.headers,
|
|
65
|
+
...headers,
|
|
66
|
+
};
|
|
67
|
+
return this;
|
|
49
68
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
69
|
+
/**设置agent */
|
|
70
|
+
proxyAgent(url) {
|
|
71
|
+
this._data.agent = UtilFunctions_1.UtilFunc.matchProc(this._data['protocol'], {
|
|
72
|
+
'http:': () => (0, http_proxy_agent_1.default)(url),
|
|
73
|
+
'https:': () => (0, https_proxy_agent_1.default)(url),
|
|
74
|
+
});
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**添加一段query */
|
|
78
|
+
query(data) {
|
|
79
|
+
this._data.path = UtilCom.buildQuery(this._data.path ?? '', data);
|
|
80
|
+
return this;
|
|
53
81
|
}
|
|
54
82
|
//#endregion
|
|
55
83
|
//#region 快速预设
|
|
84
|
+
/**收发皆为json的预设 */
|
|
85
|
+
json() {
|
|
86
|
+
return this.sendJson().acceptJson();
|
|
87
|
+
}
|
|
56
88
|
/**收发皆为json的post预设 */
|
|
57
89
|
postJson() {
|
|
58
90
|
return this.post().json();
|
|
59
91
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return this.
|
|
92
|
+
/**无查询参数获取json的get预设 */
|
|
93
|
+
getJson() {
|
|
94
|
+
return this.get().sendNone().acceptJson();
|
|
95
|
+
}
|
|
96
|
+
/**有查询参数获取json的get预设 */
|
|
97
|
+
queryJson() {
|
|
98
|
+
return this.get().sendQuery().acceptJson();
|
|
99
|
+
}
|
|
100
|
+
/**收发皆为json的https-post预设 */
|
|
101
|
+
static httpsPostJson() {
|
|
102
|
+
return UtilCom.https().postJson();
|
|
103
|
+
}
|
|
104
|
+
/**收发皆为json的http-post预设 */
|
|
105
|
+
static httpPostJson() {
|
|
106
|
+
return UtilCom.http().postJson();
|
|
107
|
+
}
|
|
108
|
+
/**无查询参数获取json的https-get预设 */
|
|
109
|
+
static httpsGetJson() {
|
|
110
|
+
return UtilCom.https().getJson();
|
|
111
|
+
}
|
|
112
|
+
/**有查询参数获取json的https-get预设 */
|
|
113
|
+
static httpsQueryJson() {
|
|
114
|
+
return UtilCom.http().queryJson();
|
|
115
|
+
}
|
|
116
|
+
/**无查询参数获取json的http-get预设 */
|
|
117
|
+
static httpGetJson() {
|
|
118
|
+
return UtilCom.http().getJson();
|
|
119
|
+
}
|
|
120
|
+
/**有查询参数获取json的http-get预设 */
|
|
121
|
+
static httpQueryJson() {
|
|
122
|
+
return UtilCom.http().queryJson();
|
|
63
123
|
}
|
|
64
124
|
//#endregion
|
|
65
125
|
//#region 接收数据类型
|
|
@@ -67,7 +127,7 @@ class UtilCom {
|
|
|
67
127
|
accept(t) {
|
|
68
128
|
const map = {
|
|
69
129
|
'json': this.acceptJson(),
|
|
70
|
-
'
|
|
130
|
+
'raw': this.acceptRaw(),
|
|
71
131
|
};
|
|
72
132
|
return map[t];
|
|
73
133
|
}
|
|
@@ -80,7 +140,7 @@ class UtilCom {
|
|
|
80
140
|
return undefined;
|
|
81
141
|
const { data, ...rest } = result;
|
|
82
142
|
if (data.trim() == "") {
|
|
83
|
-
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:
|
|
143
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误: 原始字符串为空`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
84
144
|
return { ...result, raw: "", data: null };
|
|
85
145
|
}
|
|
86
146
|
try {
|
|
@@ -89,26 +149,17 @@ class UtilCom {
|
|
|
89
149
|
return { ...rest, data: obj };
|
|
90
150
|
}
|
|
91
151
|
catch (e) {
|
|
92
|
-
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:${e}
|
|
152
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:${e}`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
93
153
|
return { ...result, raw: data, data: null };
|
|
94
154
|
}
|
|
95
155
|
}
|
|
96
156
|
};
|
|
97
|
-
|
|
157
|
+
this._accept = proc;
|
|
158
|
+
return this;
|
|
98
159
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
reduce: (acc, curr) => acc + curr,
|
|
103
|
-
parse: (result) => {
|
|
104
|
-
UtilLogger_1.SLogger.http(`log accept 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
105
|
-
if (result == undefined)
|
|
106
|
-
return '';
|
|
107
|
-
const { data, ...rest } = result;
|
|
108
|
-
return data;
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
return new UtilCom(this._data, this._send, proc);
|
|
160
|
+
acceptRaw() {
|
|
161
|
+
this._accept = AcceptRawProc;
|
|
162
|
+
return this;
|
|
112
163
|
}
|
|
113
164
|
//#endregion
|
|
114
165
|
//#region 发送数据类型
|
|
@@ -116,29 +167,21 @@ class UtilCom {
|
|
|
116
167
|
send(t) {
|
|
117
168
|
const map = {
|
|
118
169
|
'json': this.sendJson(),
|
|
119
|
-
|
|
170
|
+
'query': this.sendQuery(),
|
|
171
|
+
'formData': this.sendFormData(),
|
|
172
|
+
'none': this.sendNone(),
|
|
173
|
+
'raw': this.sendRaw(),
|
|
120
174
|
};
|
|
121
175
|
return map[t];
|
|
122
176
|
}
|
|
177
|
+
/**利用 req.write 发送一段json */
|
|
123
178
|
sendJson() {
|
|
124
179
|
const proc = {
|
|
125
180
|
proc: (opt, reqData) => {
|
|
126
181
|
const { method } = opt;
|
|
127
182
|
const isPost = (method == "POST");
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (queryString) {
|
|
131
|
-
// 检查当前路径是否已经包含问号
|
|
132
|
-
const separator = opt?.path?.includes('?') ? '&' : '?';
|
|
133
|
-
opt.path += separator + queryString;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (isPost) {
|
|
137
|
-
this._data.headers ??= {
|
|
138
|
-
'Content-Type': 'application/json',
|
|
139
|
-
'Content-Length': Buffer.from(JSON.stringify(reqData)).length,
|
|
140
|
-
};
|
|
141
|
-
}
|
|
183
|
+
this._data.headers ??= {};
|
|
184
|
+
this._data.headers['Content-Length'] = Buffer.byteLength(JSON.stringify(reqData));
|
|
142
185
|
const procReq = (req) => {
|
|
143
186
|
if (isPost)
|
|
144
187
|
req.write(JSON.stringify(reqData));
|
|
@@ -147,52 +190,85 @@ class UtilCom {
|
|
|
147
190
|
return procReq;
|
|
148
191
|
}
|
|
149
192
|
};
|
|
150
|
-
|
|
193
|
+
this._send = proc;
|
|
194
|
+
this._data.headers ??= {};
|
|
195
|
+
this._data.headers['Content-Type'] = 'application/json';
|
|
196
|
+
return this;
|
|
151
197
|
}
|
|
152
|
-
|
|
198
|
+
/**利用 appendQuery 直接将数据附加在path上发送请求 */
|
|
199
|
+
sendQuery() {
|
|
153
200
|
const proc = {
|
|
154
|
-
proc: (opt) => {
|
|
201
|
+
proc: (opt, reqData) => {
|
|
202
|
+
opt.path = UtilCom.buildQuery(opt.path ?? '', reqData);
|
|
203
|
+
const procReq = (req) => void req.end();
|
|
204
|
+
return procReq;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
this._send = proc;
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
sendFormData() {
|
|
211
|
+
const proc = {
|
|
212
|
+
proc: (opt, formData) => {
|
|
213
|
+
opt.headers = formData.getHeaders();
|
|
155
214
|
const procReq = (req) => {
|
|
215
|
+
formData.pipe(req);
|
|
156
216
|
req.end();
|
|
157
217
|
};
|
|
158
218
|
return procReq;
|
|
159
|
-
}
|
|
219
|
+
},
|
|
160
220
|
};
|
|
161
|
-
|
|
221
|
+
this._send = proc;
|
|
222
|
+
this._data.headers ??= {};
|
|
223
|
+
this._data.headers['Content-Type'] = 'multipart/form-data';
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
sendRaw() {
|
|
227
|
+
const proc = {
|
|
228
|
+
proc: (opt, fn) => fn(opt),
|
|
229
|
+
};
|
|
230
|
+
this._send = proc;
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
sendNone() {
|
|
234
|
+
this._send = SendNoneProc;
|
|
235
|
+
return this;
|
|
162
236
|
}
|
|
163
237
|
//#endregion
|
|
164
238
|
/**发送请求
|
|
165
|
-
* @param option
|
|
166
|
-
* @param datas
|
|
239
|
+
* @param option - 网络请求选项
|
|
240
|
+
* @param datas - 数据对象
|
|
167
241
|
*/
|
|
168
242
|
async once(option, ...datas) {
|
|
169
243
|
const fullopt = Object.assign({}, this._data, option);
|
|
170
|
-
const
|
|
244
|
+
const proc = await this._send.proc(fullopt, ...datas);
|
|
171
245
|
const { reduce, init, parse } = this._accept;
|
|
172
|
-
const res = await UtilCom.
|
|
246
|
+
const res = await UtilCom.request(fullopt, proc, reduce, init);
|
|
173
247
|
return parse(res);
|
|
174
248
|
}
|
|
175
249
|
/**重复发送网络请求
|
|
176
|
-
* @param option
|
|
177
|
-
* @param
|
|
178
|
-
* @param
|
|
179
|
-
* @param datas
|
|
250
|
+
* @param option - 网络请求选项
|
|
251
|
+
* @param verify - 有效性验证函数
|
|
252
|
+
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
253
|
+
* @param datas - 数据对象
|
|
180
254
|
*/
|
|
181
|
-
async
|
|
182
|
-
|
|
255
|
+
async retry(opt, ...datas) {
|
|
256
|
+
let { option, retries, verify } = opt;
|
|
257
|
+
retries ??= {};
|
|
258
|
+
retries.tryDelay = retries.tryDelay ?? 1000;
|
|
183
259
|
const procFn = () => this.once(option, ...datas);
|
|
184
|
-
return UtilFunctions_1.UtilFunc.
|
|
260
|
+
return UtilFunctions_1.UtilFunc.retryPromise(procFn, verify, retries);
|
|
185
261
|
}
|
|
186
|
-
|
|
187
|
-
* @param
|
|
188
|
-
* @param
|
|
189
|
-
* @param
|
|
190
|
-
* @param
|
|
262
|
+
/**发送网络请求
|
|
263
|
+
* @param option - 网络请求选项
|
|
264
|
+
* @param proc - 请求处理函数 需调用req.end()
|
|
265
|
+
* @param reduce - 数据处理函数
|
|
266
|
+
* @param init - 初始数据
|
|
191
267
|
*/
|
|
192
|
-
static async
|
|
193
|
-
const { protocol, timeout, ...baseReqOpt } =
|
|
268
|
+
static async request(option, proc, reduce, init) {
|
|
269
|
+
const { protocol, timeout, ...baseReqOpt } = option;
|
|
194
270
|
const hasTimeLimit = (timeout ? timeout >= 10_000 : false);
|
|
195
|
-
const flagName = `UtilCom.
|
|
271
|
+
const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
|
|
196
272
|
let dataPromise = null;
|
|
197
273
|
return new Promise(async (resolve, rejecte) => {
|
|
198
274
|
const resFunc = (res) => {
|
|
@@ -204,10 +280,10 @@ class UtilCom {
|
|
|
204
280
|
resolve(undefined);
|
|
205
281
|
});
|
|
206
282
|
}
|
|
207
|
-
let mergedata =
|
|
283
|
+
let mergedata = init;
|
|
208
284
|
res.setEncoding('utf8');
|
|
209
285
|
res.on('data', chunk => {
|
|
210
|
-
dataPromise = UtilFunctions_1.UtilFunc.queueProc(flagName, async () => mergedata = await
|
|
286
|
+
dataPromise = UtilFunctions_1.UtilFunc.queueProc(flagName, async () => mergedata = await reduce(mergedata, chunk));
|
|
211
287
|
});
|
|
212
288
|
res.on('error', (e) => {
|
|
213
289
|
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈错误:${e}`);
|
|
@@ -243,18 +319,32 @@ class UtilCom {
|
|
|
243
319
|
UtilLogger_1.SLogger.warn(`${flagName} 发送请求错误:${e}`);
|
|
244
320
|
resolve(undefined);
|
|
245
321
|
});
|
|
246
|
-
await
|
|
322
|
+
await proc(req);
|
|
247
323
|
});
|
|
248
324
|
}
|
|
325
|
+
/**构建query */
|
|
326
|
+
static buildQuery(base, data) {
|
|
327
|
+
const queryString = querystring_1.default.stringify(data);
|
|
328
|
+
if (queryString) {
|
|
329
|
+
// 检查当前路径是否已经包含问号
|
|
330
|
+
const separator = base.includes('?') ? '&' : '?';
|
|
331
|
+
base += separator + queryString;
|
|
332
|
+
}
|
|
333
|
+
return base;
|
|
334
|
+
}
|
|
249
335
|
}
|
|
250
336
|
exports.UtilCom = UtilCom;
|
|
251
337
|
if (false)
|
|
252
338
|
((async () => {
|
|
253
|
-
const t = await UtilCom.https().
|
|
254
|
-
.
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
339
|
+
const t = await UtilCom.https().postJson()
|
|
340
|
+
.retry({
|
|
341
|
+
option: {
|
|
342
|
+
hostname: 'httpbin.org',
|
|
343
|
+
path: '/post',
|
|
344
|
+
timeout: 10000
|
|
345
|
+
},
|
|
346
|
+
verify: () => UtilSymbol_1.Success,
|
|
347
|
+
retries: {}
|
|
348
|
+
}, { test: 1 });
|
|
259
349
|
console.log(t);
|
|
260
350
|
})());
|
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -115,7 +115,6 @@ export declare namespace UtilFT {
|
|
|
115
115
|
*/
|
|
116
116
|
function loadJSONFile<T extends JToken>(filePath: string, opt: LoadJsonFileOpt<T>): Promise<T>;
|
|
117
117
|
/**写入JSON文件
|
|
118
|
-
* void (string,Object)
|
|
119
118
|
* @async
|
|
120
119
|
* @param filePath - 文件路径
|
|
121
120
|
* @param token - 所要写入的JToken
|