@zwa73/utils 1.0.197 → 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 +147 -54
- package/dist/UtilCom.js +174 -80
- 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 +215 -116
- 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,67 +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;
|
|
99
98
|
/**收发皆为json的预设 */
|
|
100
|
-
json(): UtilCom<D
|
|
99
|
+
json(): UtilCom<D & {
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json";
|
|
102
|
+
};
|
|
103
|
+
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
104
|
+
/**收发皆为json的post预设 */
|
|
105
|
+
postJson(): UtilCom<D & {
|
|
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>>;
|
|
101
164
|
/**接收数据类型*/
|
|
102
165
|
accept<T extends AcceptType>(t: T): {
|
|
103
166
|
readonly json: UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
104
|
-
readonly
|
|
167
|
+
readonly raw: UtilCom<D, S, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
105
168
|
}[T];
|
|
106
169
|
acceptJson(): UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
107
|
-
|
|
170
|
+
acceptRaw(): UtilCom<D, S, typeof AcceptRawProc>;
|
|
108
171
|
/**发送数据类型*/
|
|
109
172
|
send<T extends SendType>(t: T): {
|
|
110
|
-
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>;
|
|
111
184
|
readonly none: UtilCom<D, SendProc<[]>, A>;
|
|
185
|
+
readonly raw: UtilCom<D, SendProc<[(opt: RequestOption) => RequestProcFn]>, A>;
|
|
112
186
|
}[T];
|
|
113
|
-
|
|
114
|
-
|
|
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>;
|
|
115
202
|
/**发送请求
|
|
116
|
-
* @param option
|
|
117
|
-
* @param datas
|
|
203
|
+
* @param option - 网络请求选项
|
|
204
|
+
* @param datas - 数据对象
|
|
118
205
|
*/
|
|
119
|
-
once(option: PartialOption<
|
|
206
|
+
once(option: PartialOption<RequestOption, D>, ...datas: SendParams<S>): Promise<any>;
|
|
120
207
|
/**重复发送网络请求
|
|
121
|
-
* @param option
|
|
122
|
-
* @param
|
|
123
|
-
* @param
|
|
124
|
-
* @param datas
|
|
208
|
+
* @param option - 网络请求选项
|
|
209
|
+
* @param verify - 有效性验证函数
|
|
210
|
+
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
211
|
+
* @param datas - 数据对象
|
|
125
212
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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 - 初始数据
|
|
132
223
|
*/
|
|
133
|
-
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;
|
|
134
227
|
}
|
|
135
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,26 +32,52 @@ 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;
|
|
49
60
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
/**补充header */
|
|
62
|
+
header(headers) {
|
|
63
|
+
this._data.headers = {
|
|
64
|
+
...this._data.headers,
|
|
65
|
+
...headers,
|
|
66
|
+
};
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
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 快速预设
|
|
@@ -57,13 +85,49 @@ class UtilCom {
|
|
|
57
85
|
json() {
|
|
58
86
|
return this.sendJson().acceptJson();
|
|
59
87
|
}
|
|
88
|
+
/**收发皆为json的post预设 */
|
|
89
|
+
postJson() {
|
|
90
|
+
return this.post().json();
|
|
91
|
+
}
|
|
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();
|
|
123
|
+
}
|
|
60
124
|
//#endregion
|
|
61
125
|
//#region 接收数据类型
|
|
62
126
|
/**接收数据类型*/
|
|
63
127
|
accept(t) {
|
|
64
128
|
const map = {
|
|
65
129
|
'json': this.acceptJson(),
|
|
66
|
-
'
|
|
130
|
+
'raw': this.acceptRaw(),
|
|
67
131
|
};
|
|
68
132
|
return map[t];
|
|
69
133
|
}
|
|
@@ -76,7 +140,7 @@ class UtilCom {
|
|
|
76
140
|
return undefined;
|
|
77
141
|
const { data, ...rest } = result;
|
|
78
142
|
if (data.trim() == "") {
|
|
79
|
-
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:
|
|
143
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误: 原始字符串为空`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
80
144
|
return { ...result, raw: "", data: null };
|
|
81
145
|
}
|
|
82
146
|
try {
|
|
@@ -85,26 +149,17 @@ class UtilCom {
|
|
|
85
149
|
return { ...rest, data: obj };
|
|
86
150
|
}
|
|
87
151
|
catch (e) {
|
|
88
|
-
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:${e}
|
|
152
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:${e}`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
89
153
|
return { ...result, raw: data, data: null };
|
|
90
154
|
}
|
|
91
155
|
}
|
|
92
156
|
};
|
|
93
|
-
|
|
157
|
+
this._accept = proc;
|
|
158
|
+
return this;
|
|
94
159
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
reduce: (acc, curr) => acc + curr,
|
|
99
|
-
parse: (result) => {
|
|
100
|
-
UtilLogger_1.SLogger.http(`log accept 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
101
|
-
if (result == undefined)
|
|
102
|
-
return '';
|
|
103
|
-
const { data, ...rest } = result;
|
|
104
|
-
return data;
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
return new UtilCom(this._data, this._send, proc);
|
|
160
|
+
acceptRaw() {
|
|
161
|
+
this._accept = AcceptRawProc;
|
|
162
|
+
return this;
|
|
108
163
|
}
|
|
109
164
|
//#endregion
|
|
110
165
|
//#region 发送数据类型
|
|
@@ -112,29 +167,21 @@ class UtilCom {
|
|
|
112
167
|
send(t) {
|
|
113
168
|
const map = {
|
|
114
169
|
'json': this.sendJson(),
|
|
115
|
-
|
|
170
|
+
'query': this.sendQuery(),
|
|
171
|
+
'formData': this.sendFormData(),
|
|
172
|
+
'none': this.sendNone(),
|
|
173
|
+
'raw': this.sendRaw(),
|
|
116
174
|
};
|
|
117
175
|
return map[t];
|
|
118
176
|
}
|
|
177
|
+
/**利用 req.write 发送一段json */
|
|
119
178
|
sendJson() {
|
|
120
179
|
const proc = {
|
|
121
180
|
proc: (opt, reqData) => {
|
|
122
181
|
const { method } = opt;
|
|
123
182
|
const isPost = (method == "POST");
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (queryString) {
|
|
127
|
-
// 检查当前路径是否已经包含问号
|
|
128
|
-
const separator = opt?.path?.includes('?') ? '&' : '?';
|
|
129
|
-
opt.path += separator + queryString;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
if (isPost) {
|
|
133
|
-
this._data.headers ??= {
|
|
134
|
-
'Content-Type': 'application/json',
|
|
135
|
-
'Content-Length': Buffer.from(JSON.stringify(reqData)).length,
|
|
136
|
-
};
|
|
137
|
-
}
|
|
183
|
+
this._data.headers ??= {};
|
|
184
|
+
this._data.headers['Content-Length'] = Buffer.byteLength(JSON.stringify(reqData));
|
|
138
185
|
const procReq = (req) => {
|
|
139
186
|
if (isPost)
|
|
140
187
|
req.write(JSON.stringify(reqData));
|
|
@@ -143,52 +190,85 @@ class UtilCom {
|
|
|
143
190
|
return procReq;
|
|
144
191
|
}
|
|
145
192
|
};
|
|
146
|
-
|
|
193
|
+
this._send = proc;
|
|
194
|
+
this._data.headers ??= {};
|
|
195
|
+
this._data.headers['Content-Type'] = 'application/json';
|
|
196
|
+
return this;
|
|
147
197
|
}
|
|
148
|
-
|
|
198
|
+
/**利用 appendQuery 直接将数据附加在path上发送请求 */
|
|
199
|
+
sendQuery() {
|
|
149
200
|
const proc = {
|
|
150
|
-
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();
|
|
151
214
|
const procReq = (req) => {
|
|
215
|
+
formData.pipe(req);
|
|
152
216
|
req.end();
|
|
153
217
|
};
|
|
154
218
|
return procReq;
|
|
155
|
-
}
|
|
219
|
+
},
|
|
220
|
+
};
|
|
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),
|
|
156
229
|
};
|
|
157
|
-
|
|
230
|
+
this._send = proc;
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
sendNone() {
|
|
234
|
+
this._send = SendNoneProc;
|
|
235
|
+
return this;
|
|
158
236
|
}
|
|
159
237
|
//#endregion
|
|
160
238
|
/**发送请求
|
|
161
|
-
* @param option
|
|
162
|
-
* @param datas
|
|
239
|
+
* @param option - 网络请求选项
|
|
240
|
+
* @param datas - 数据对象
|
|
163
241
|
*/
|
|
164
242
|
async once(option, ...datas) {
|
|
165
243
|
const fullopt = Object.assign({}, this._data, option);
|
|
166
|
-
const
|
|
244
|
+
const proc = await this._send.proc(fullopt, ...datas);
|
|
167
245
|
const { reduce, init, parse } = this._accept;
|
|
168
|
-
const res = await UtilCom.
|
|
246
|
+
const res = await UtilCom.request(fullopt, proc, reduce, init);
|
|
169
247
|
return parse(res);
|
|
170
248
|
}
|
|
171
249
|
/**重复发送网络请求
|
|
172
|
-
* @param option
|
|
173
|
-
* @param
|
|
174
|
-
* @param
|
|
175
|
-
* @param datas
|
|
250
|
+
* @param option - 网络请求选项
|
|
251
|
+
* @param verify - 有效性验证函数
|
|
252
|
+
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
253
|
+
* @param datas - 数据对象
|
|
176
254
|
*/
|
|
177
|
-
async
|
|
178
|
-
|
|
255
|
+
async retry(opt, ...datas) {
|
|
256
|
+
let { option, retries, verify } = opt;
|
|
257
|
+
retries ??= {};
|
|
258
|
+
retries.tryDelay = retries.tryDelay ?? 1000;
|
|
179
259
|
const procFn = () => this.once(option, ...datas);
|
|
180
|
-
return UtilFunctions_1.UtilFunc.
|
|
260
|
+
return UtilFunctions_1.UtilFunc.retryPromise(procFn, verify, retries);
|
|
181
261
|
}
|
|
182
|
-
|
|
183
|
-
* @param
|
|
184
|
-
* @param
|
|
185
|
-
* @param
|
|
186
|
-
* @param
|
|
262
|
+
/**发送网络请求
|
|
263
|
+
* @param option - 网络请求选项
|
|
264
|
+
* @param proc - 请求处理函数 需调用req.end()
|
|
265
|
+
* @param reduce - 数据处理函数
|
|
266
|
+
* @param init - 初始数据
|
|
187
267
|
*/
|
|
188
|
-
static async
|
|
189
|
-
const { protocol, timeout, ...baseReqOpt } =
|
|
268
|
+
static async request(option, proc, reduce, init) {
|
|
269
|
+
const { protocol, timeout, ...baseReqOpt } = option;
|
|
190
270
|
const hasTimeLimit = (timeout ? timeout >= 10_000 : false);
|
|
191
|
-
const flagName = `UtilCom.
|
|
271
|
+
const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
|
|
192
272
|
let dataPromise = null;
|
|
193
273
|
return new Promise(async (resolve, rejecte) => {
|
|
194
274
|
const resFunc = (res) => {
|
|
@@ -200,10 +280,10 @@ class UtilCom {
|
|
|
200
280
|
resolve(undefined);
|
|
201
281
|
});
|
|
202
282
|
}
|
|
203
|
-
let mergedata =
|
|
283
|
+
let mergedata = init;
|
|
204
284
|
res.setEncoding('utf8');
|
|
205
285
|
res.on('data', chunk => {
|
|
206
|
-
dataPromise = UtilFunctions_1.UtilFunc.queueProc(flagName, async () => mergedata = await
|
|
286
|
+
dataPromise = UtilFunctions_1.UtilFunc.queueProc(flagName, async () => mergedata = await reduce(mergedata, chunk));
|
|
207
287
|
});
|
|
208
288
|
res.on('error', (e) => {
|
|
209
289
|
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈错误:${e}`);
|
|
@@ -239,18 +319,32 @@ class UtilCom {
|
|
|
239
319
|
UtilLogger_1.SLogger.warn(`${flagName} 发送请求错误:${e}`);
|
|
240
320
|
resolve(undefined);
|
|
241
321
|
});
|
|
242
|
-
await
|
|
322
|
+
await proc(req);
|
|
243
323
|
});
|
|
244
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
|
+
}
|
|
245
335
|
}
|
|
246
336
|
exports.UtilCom = UtilCom;
|
|
247
337
|
if (false)
|
|
248
338
|
((async () => {
|
|
249
|
-
const t = await UtilCom.https().
|
|
250
|
-
.
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
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 });
|
|
255
349
|
console.log(t);
|
|
256
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
|