@zwa73/utils 1.0.233 → 1.0.234
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/UtilHttp.d.ts +42 -96
- package/dist/UtilHttp.js +67 -62
- package/package.json +1 -1
package/dist/UtilHttp.d.ts
CHANGED
|
@@ -45,30 +45,24 @@ export type RequestParseFn<D, R> = (result: RequestResult<D> | undefined) => MPr
|
|
|
45
45
|
export type RequestReduceFn<T> = (acc: T, data: string) => MPromise<T>;
|
|
46
46
|
/**数据初始化函数 */
|
|
47
47
|
export type RequestInitFn<T> = (res: http.IncomingMessage) => MPromise<T>;
|
|
48
|
-
declare const AcceptTypeList: readonly ["json", "string"];
|
|
49
|
-
/**可用的接受类型 */
|
|
50
|
-
type AcceptType = typeof AcceptTypeList[number];
|
|
51
|
-
declare const SendTypeList: readonly ["json", "query", "formData", "none"];
|
|
52
|
-
/**可用的发送类型 */
|
|
53
|
-
type SendType = typeof SendTypeList[number];
|
|
54
48
|
/**accept处理数据
|
|
55
49
|
* @template D - 接受时转换的数据类型
|
|
56
50
|
* @template R - 完成处理的数据类型
|
|
57
51
|
*/
|
|
58
|
-
type AcceptProcCtor<D, R, T> = (arg: T) => MPromise<
|
|
59
|
-
type
|
|
52
|
+
type AcceptProcCtor<D, R, T extends {}> = AcceptProcData<D, R> | ((arg: T) => MPromise<AcceptProcData<D, R>>);
|
|
53
|
+
type AcceptProcData<D, R> = {
|
|
60
54
|
init: RequestInitFn<D>;
|
|
61
|
-
reduce
|
|
55
|
+
reduce?: RequestReduceFn<D>;
|
|
62
56
|
parse: RequestParseFn<D, R>;
|
|
63
57
|
};
|
|
64
58
|
/**send处理数据 */
|
|
65
|
-
type SendProcCtor<T extends {}> = (opt: RequestOption, arg: T) => MPromise<
|
|
66
|
-
type
|
|
59
|
+
type SendProcCtor<T extends {}> = SendProcData | ((opt: RequestOption, arg: T) => MPromise<SendProcData>);
|
|
60
|
+
type SendProcData = {
|
|
67
61
|
proc: RequestProcFn;
|
|
68
62
|
};
|
|
69
63
|
declare const SendNoneProc: SendProcCtor<{}>;
|
|
70
64
|
declare const AcceptStringProc: AcceptProcCtor<string, RequestResult<string> | undefined, {}>;
|
|
71
|
-
declare const AcceptNoneProc: AcceptProcCtor<undefined, undefined, {}>;
|
|
65
|
+
declare const AcceptNoneProc: AcceptProcCtor<undefined, RequestResult<undefined> | undefined, {}>;
|
|
72
66
|
/**send处理的参数 */
|
|
73
67
|
type SendParams<S extends SendProcCtor<any>> = S extends SendProcCtor<infer P> ? P : never;
|
|
74
68
|
/**send处理的参数 */
|
|
@@ -127,10 +121,10 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
127
121
|
headers: {
|
|
128
122
|
"Content-Type": "application/json";
|
|
129
123
|
};
|
|
130
|
-
},
|
|
124
|
+
}, (opt: RequestOption, arg: {
|
|
131
125
|
/**发送的json */
|
|
132
126
|
json: JToken;
|
|
133
|
-
}>,
|
|
127
|
+
}) => MPromise<SendProcData>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
134
128
|
/**收发皆为json的post预设 */
|
|
135
129
|
postJson(): UtilHttp<D & {
|
|
136
130
|
method: "POST";
|
|
@@ -138,21 +132,21 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
138
132
|
headers: {
|
|
139
133
|
'Content-Type': "application/json";
|
|
140
134
|
};
|
|
141
|
-
},
|
|
135
|
+
}, (opt: RequestOption, arg: {
|
|
142
136
|
/**发送的json */
|
|
143
137
|
json: JToken;
|
|
144
|
-
}>,
|
|
138
|
+
}) => MPromise<SendProcData>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
145
139
|
/**无查询参数获取json的get预设 */
|
|
146
140
|
getJson(): UtilHttp<D & {
|
|
147
141
|
method: "GET";
|
|
148
|
-
}, SendProcCtor<{}>,
|
|
142
|
+
}, SendProcCtor<{}>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
149
143
|
/**有查询参数获取json的get预设 */
|
|
150
144
|
queryJson(): UtilHttp<D & {
|
|
151
145
|
method: "GET";
|
|
152
|
-
},
|
|
146
|
+
}, (opt: RequestOption, arg: {
|
|
153
147
|
/**附加的查询表单数据 */
|
|
154
148
|
query: QueryRequestData;
|
|
155
|
-
}>,
|
|
149
|
+
}) => MPromise<SendProcData>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
156
150
|
/**收发皆为json的https-post预设 */
|
|
157
151
|
static httpsPostJson(): UtilHttp<{
|
|
158
152
|
readonly protocol: "https:";
|
|
@@ -162,10 +156,10 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
162
156
|
headers: {
|
|
163
157
|
'Content-Type': "application/json";
|
|
164
158
|
};
|
|
165
|
-
},
|
|
159
|
+
}, (opt: RequestOption, arg: {
|
|
166
160
|
/**发送的json */
|
|
167
161
|
json: JToken;
|
|
168
|
-
}>,
|
|
162
|
+
}) => MPromise<SendProcData>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
169
163
|
/**收发皆为json的http-post预设 */
|
|
170
164
|
static httpPostJson(): UtilHttp<{
|
|
171
165
|
readonly protocol: "http:";
|
|
@@ -175,50 +169,46 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
175
169
|
headers: {
|
|
176
170
|
'Content-Type': "application/json";
|
|
177
171
|
};
|
|
178
|
-
},
|
|
172
|
+
}, (opt: RequestOption, arg: {
|
|
179
173
|
/**发送的json */
|
|
180
174
|
json: JToken;
|
|
181
|
-
}>,
|
|
175
|
+
}) => MPromise<SendProcData>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
182
176
|
/**无查询参数获取json的https-get预设 */
|
|
183
177
|
static httpsGetJson(): UtilHttp<{
|
|
184
178
|
readonly protocol: "https:";
|
|
185
179
|
} & {
|
|
186
180
|
method: "GET";
|
|
187
|
-
}, SendProcCtor<{}>,
|
|
181
|
+
}, SendProcCtor<{}>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
188
182
|
/**有查询参数获取json的https-get预设 */
|
|
189
183
|
static httpsQueryJson(): UtilHttp<{
|
|
190
184
|
readonly protocol: "http:";
|
|
191
185
|
} & {
|
|
192
186
|
method: "GET";
|
|
193
|
-
},
|
|
187
|
+
}, (opt: RequestOption, arg: {
|
|
194
188
|
/**附加的查询表单数据 */
|
|
195
189
|
query: QueryRequestData;
|
|
196
|
-
}>,
|
|
190
|
+
}) => MPromise<SendProcData>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
197
191
|
/**无查询参数获取json的http-get预设 */
|
|
198
192
|
static httpGetJson(): UtilHttp<{
|
|
199
193
|
readonly protocol: "http:";
|
|
200
194
|
} & {
|
|
201
195
|
method: "GET";
|
|
202
|
-
}, SendProcCtor<{}>,
|
|
196
|
+
}, SendProcCtor<{}>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
203
197
|
/**有查询参数获取json的http-get预设 */
|
|
204
198
|
static httpQueryJson(): UtilHttp<{
|
|
205
199
|
readonly protocol: "http:";
|
|
206
200
|
} & {
|
|
207
201
|
method: "GET";
|
|
208
|
-
},
|
|
202
|
+
}, (opt: RequestOption, arg: {
|
|
209
203
|
/**附加的查询表单数据 */
|
|
210
204
|
query: QueryRequestData;
|
|
211
|
-
}>,
|
|
212
|
-
|
|
213
|
-
accept<T extends AcceptType>(t: T): {
|
|
214
|
-
readonly json: UtilHttp<D, S, AcceptProcCtor<string, RequestResult<JToken> | undefined, {}>>;
|
|
215
|
-
readonly string: UtilHttp<D, S, AcceptProcCtor<string, RequestResult<string> | undefined, {}>>;
|
|
216
|
-
readonly none: UtilHttp<D, S, AcceptProcCtor<undefined, undefined, {}>>;
|
|
217
|
-
}[T];
|
|
218
|
-
acceptJson(): UtilHttp<D, S, AcceptProcCtor<string, RequestResult<JToken> | undefined, {}>>;
|
|
205
|
+
}) => MPromise<SendProcData>, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
206
|
+
acceptJson(): UtilHttp<D, S, AcceptProcData<string, RequestResult<JToken> | undefined>>;
|
|
219
207
|
acceptString(): UtilHttp<D, S, typeof AcceptStringProc>;
|
|
220
208
|
acceptNone(): UtilHttp<D, S, typeof AcceptNoneProc>;
|
|
221
|
-
acceptFile(): UtilHttp<D, S,
|
|
209
|
+
acceptFile(): UtilHttp<D, S, (arg: {
|
|
210
|
+
acceptFilepath: string;
|
|
211
|
+
}) => MPromise<AcceptProcData<{
|
|
222
212
|
promise: Promise<boolean>;
|
|
223
213
|
}, {
|
|
224
214
|
/**响应头 */
|
|
@@ -227,53 +217,9 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
227
217
|
statusCode?: number;
|
|
228
218
|
/**保存文件路径 */
|
|
229
219
|
filepath: string;
|
|
230
|
-
} | undefined
|
|
231
|
-
acceptFilepath: string;
|
|
232
|
-
}>>;
|
|
220
|
+
} | undefined>>>;
|
|
233
221
|
/**自定的接收数据类型*/
|
|
234
|
-
acceptRaw<AD, AR, AT>(proc: AcceptProcCtor<AD, AR, AT>): UtilHttp<D, S, typeof proc>;
|
|
235
|
-
/**预设的发送数据类型 */
|
|
236
|
-
send<T extends SendType>(t: T): {
|
|
237
|
-
readonly json: UtilHttp<D & {
|
|
238
|
-
headers: {
|
|
239
|
-
"Content-Type": "application/json";
|
|
240
|
-
};
|
|
241
|
-
}, SendProcCtor<{
|
|
242
|
-
/**发送的json */
|
|
243
|
-
json: JToken;
|
|
244
|
-
}>, A>;
|
|
245
|
-
readonly query: UtilHttp<D, SendProcCtor<{
|
|
246
|
-
/**附加的查询表单数据 */
|
|
247
|
-
query: QueryRequestData;
|
|
248
|
-
}>, A>;
|
|
249
|
-
readonly formData: UtilHttp<D & {
|
|
250
|
-
headers: {
|
|
251
|
-
"Content-Type": "multipart/form-data";
|
|
252
|
-
};
|
|
253
|
-
}, SendProcCtor<{
|
|
254
|
-
/**form-data包提供的FormData表单数据 */
|
|
255
|
-
formdata: FormData;
|
|
256
|
-
}>, A>;
|
|
257
|
-
readonly form: UtilHttp<D & {
|
|
258
|
-
headers: {
|
|
259
|
-
"Content-Type": "application/x-www-form-urlencoded";
|
|
260
|
-
};
|
|
261
|
-
}, SendProcCtor<{
|
|
262
|
-
/**表单 */
|
|
263
|
-
form: QueryRequestData;
|
|
264
|
-
}>, A>;
|
|
265
|
-
readonly file: UtilHttp<D & {
|
|
266
|
-
headers: {
|
|
267
|
-
"Content-Type": "multipart/form-data";
|
|
268
|
-
};
|
|
269
|
-
}, SendProcCtor<{
|
|
270
|
-
/**文件路径 */
|
|
271
|
-
filepath: string;
|
|
272
|
-
/**文件名 */
|
|
273
|
-
filename?: string;
|
|
274
|
-
}>, A>;
|
|
275
|
-
readonly none: UtilHttp<D, SendProcCtor<{}>, A>;
|
|
276
|
-
}[T];
|
|
222
|
+
acceptRaw<AD, AR, AT extends {}>(proc: AcceptProcCtor<AD, AR, AT>): UtilHttp<D, S, typeof proc>;
|
|
277
223
|
/**发送json
|
|
278
224
|
* 请求参数为 (token:JToken)
|
|
279
225
|
*/
|
|
@@ -281,44 +227,44 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
281
227
|
headers: {
|
|
282
228
|
"Content-Type": "application/json";
|
|
283
229
|
};
|
|
284
|
-
},
|
|
230
|
+
}, (opt: RequestOption, arg: {
|
|
285
231
|
/**发送的json */
|
|
286
232
|
json: JToken;
|
|
287
|
-
}>, A>;
|
|
288
|
-
/**利用 appendQuery
|
|
289
|
-
sendQuery(): UtilHttp<D,
|
|
233
|
+
}) => MPromise<SendProcData>, A>;
|
|
234
|
+
/**利用 appendQuery 直接将表单附加在path上发送请求 */
|
|
235
|
+
sendQuery(): UtilHttp<D, (opt: RequestOption, arg: {
|
|
290
236
|
/**附加的查询表单数据 */
|
|
291
237
|
query: QueryRequestData;
|
|
292
|
-
}>, A>;
|
|
293
|
-
|
|
238
|
+
}) => MPromise<SendProcData>, A>;
|
|
239
|
+
/**发送form-data包提供的表单数据 */
|
|
294
240
|
sendFormData(): UtilHttp<D & {
|
|
295
241
|
headers: {
|
|
296
242
|
"Content-Type": "multipart/form-data";
|
|
297
243
|
};
|
|
298
|
-
},
|
|
244
|
+
}, (opt: RequestOption, arg: {
|
|
299
245
|
/**form-data包提供的FormData表单数据 */
|
|
300
246
|
formdata: FormData;
|
|
301
|
-
}>, A>;
|
|
247
|
+
}) => MPromise<SendProcData>, A>;
|
|
302
248
|
/**发送表单 */
|
|
303
249
|
sendForm(): UtilHttp<D & {
|
|
304
250
|
headers: {
|
|
305
251
|
"Content-Type": "application/x-www-form-urlencoded";
|
|
306
252
|
};
|
|
307
|
-
},
|
|
253
|
+
}, (opt: RequestOption, arg: {
|
|
308
254
|
/**表单 */
|
|
309
255
|
form: QueryRequestData;
|
|
310
|
-
}>, A>;
|
|
256
|
+
}) => MPromise<SendProcData>, A>;
|
|
311
257
|
/**发送文件 */
|
|
312
258
|
sendFile(): UtilHttp<D & {
|
|
313
259
|
headers: {
|
|
314
260
|
"Content-Type": "multipart/form-data";
|
|
315
261
|
};
|
|
316
|
-
},
|
|
262
|
+
}, (opt: RequestOption, arg: {
|
|
317
263
|
/**文件路径 */
|
|
318
264
|
filepath: string;
|
|
319
265
|
/**文件名 */
|
|
320
266
|
filename?: string;
|
|
321
|
-
}>, A>;
|
|
267
|
+
}) => MPromise<SendProcData>, A>;
|
|
322
268
|
sendNone(): UtilHttp<D, typeof SendNoneProc, A>;
|
|
323
269
|
/**自定的发送数据类型 */
|
|
324
270
|
sendRaw<T extends any[]>(proc: SendProcCtor<T>): UtilHttp<D, typeof proc, A>;
|
|
@@ -348,7 +294,7 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
348
294
|
* @param reqProc - 请求处理函数 需调用req.end()
|
|
349
295
|
* @param acceptProc - 响应处理数据
|
|
350
296
|
*/
|
|
351
|
-
static request<T>(option: RequestOption, sendProc:
|
|
297
|
+
static request<T>(option: RequestOption, sendProc: SendProcData, acceptProc: Omit<AcceptProcData<T, unknown>, 'parse'>): Promise<RequestResult<T> | undefined>;
|
|
352
298
|
/**构建query */
|
|
353
299
|
static buildQuery(base: string, data: QueryRequestData): string;
|
|
354
300
|
}
|
package/dist/UtilHttp.js
CHANGED
|
@@ -16,21 +16,30 @@ const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
|
|
|
16
16
|
const js_utils_1 = require("@zwa73/js-utils");
|
|
17
17
|
const fs_1 = __importDefault(require("fs"));
|
|
18
18
|
const pathe_1 = __importDefault(require("pathe"));
|
|
19
|
-
const
|
|
20
|
-
const SendTypeList = ["json", "query", "formData", "none"];
|
|
21
|
-
const SendNoneProc = () => ({
|
|
19
|
+
const SendNoneProc = {
|
|
22
20
|
proc: req => void req.end()
|
|
23
|
-
}
|
|
24
|
-
const AcceptStringProc =
|
|
21
|
+
};
|
|
22
|
+
const AcceptStringProc = {
|
|
25
23
|
init: () => '',
|
|
26
24
|
reduce: (acc, dat) => acc + dat,
|
|
27
|
-
parse:
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
parse: result => {
|
|
26
|
+
if (result == undefined) {
|
|
27
|
+
UtilLogger_1.SLogger.warn(`accept json 接收反馈错误: 响应结果无效`);
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const AcceptNoneProc = {
|
|
30
34
|
init: () => undefined,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
parse: result => {
|
|
36
|
+
if (result == undefined) {
|
|
37
|
+
UtilLogger_1.SLogger.warn(`accept none 接收反馈错误: 响应结果无效`);
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
34
43
|
/**http请求工具 */
|
|
35
44
|
class UtilHttp {
|
|
36
45
|
_data;
|
|
@@ -149,40 +158,31 @@ class UtilHttp {
|
|
|
149
158
|
}
|
|
150
159
|
//#endregion
|
|
151
160
|
//#region 接收数据类型
|
|
152
|
-
/**预设的接收数据类型*/
|
|
153
|
-
accept(t) {
|
|
154
|
-
const map = {
|
|
155
|
-
'json': this.acceptJson(),
|
|
156
|
-
'string': this.acceptString(),
|
|
157
|
-
'none': this.acceptNone(),
|
|
158
|
-
};
|
|
159
|
-
return map[t];
|
|
160
|
-
}
|
|
161
161
|
acceptJson() {
|
|
162
|
-
const acceptProc =
|
|
162
|
+
const acceptProc = {
|
|
163
163
|
init: () => '',
|
|
164
164
|
reduce: (acc, curr) => acc + curr,
|
|
165
165
|
parse: result => {
|
|
166
166
|
if (result == undefined) {
|
|
167
|
-
UtilLogger_1.SLogger.warn(`json
|
|
167
|
+
UtilLogger_1.SLogger.warn(`accept json 接收反馈错误: 响应结果无效`);
|
|
168
168
|
return undefined;
|
|
169
169
|
}
|
|
170
170
|
const { data, ...rest } = result;
|
|
171
171
|
if (data.trim() == "") {
|
|
172
|
-
UtilLogger_1.SLogger.warn(`json
|
|
172
|
+
UtilLogger_1.SLogger.warn(`accept json 接收反馈错误: 原始字符串为空`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
173
173
|
return { ...result, raw: "", data: null };
|
|
174
174
|
}
|
|
175
175
|
try {
|
|
176
176
|
const obj = JSON.parse(data.trim());
|
|
177
|
-
UtilLogger_1.SLogger.http(`json
|
|
177
|
+
UtilLogger_1.SLogger.http(`accept json 接受信息 data:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }), `result:`, UtilFunctions_1.UtilFunc.stringifyJToken(rest, { compress: true, space: 2 }));
|
|
178
178
|
return { ...rest, data: obj };
|
|
179
179
|
}
|
|
180
180
|
catch (err) {
|
|
181
|
-
UtilLogger_1.SLogger.warn(`json
|
|
181
|
+
UtilLogger_1.SLogger.warn(`accept json 接收反馈错误:`, err, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
182
182
|
return { ...result, raw: data, data: null };
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
|
-
}
|
|
185
|
+
};
|
|
186
186
|
this._accept = acceptProc;
|
|
187
187
|
return this;
|
|
188
188
|
}
|
|
@@ -200,7 +200,7 @@ class UtilHttp {
|
|
|
200
200
|
const fileStream = fs_1.default.createWriteStream(arg.acceptFilepath);
|
|
201
201
|
const fileWritePromise = new Promise((resolve) => {
|
|
202
202
|
fileStream.on('error', err => {
|
|
203
|
-
UtilLogger_1.SLogger.warn(`file
|
|
203
|
+
UtilLogger_1.SLogger.warn(`accept file 文件流写入错误:`, err);
|
|
204
204
|
resolve(false); // 标记写入已结束, 但失败
|
|
205
205
|
});
|
|
206
206
|
fileStream.on('finish', () => {
|
|
@@ -210,10 +210,11 @@ class UtilHttp {
|
|
|
210
210
|
res.pipe(fileStream);
|
|
211
211
|
return { promise: fileWritePromise };
|
|
212
212
|
},
|
|
213
|
-
reduce: acc => acc,
|
|
214
213
|
parse: async (result) => {
|
|
215
|
-
if (result == undefined)
|
|
214
|
+
if (result == undefined) {
|
|
215
|
+
UtilLogger_1.SLogger.warn(`accept file 接收反馈错误: 响应结果无效`);
|
|
216
216
|
return undefined;
|
|
217
|
+
}
|
|
217
218
|
const success = await result.data.promise;
|
|
218
219
|
if (!success)
|
|
219
220
|
return undefined;
|
|
@@ -230,18 +231,6 @@ class UtilHttp {
|
|
|
230
231
|
}
|
|
231
232
|
//#endregion
|
|
232
233
|
//#region 发送数据类型
|
|
233
|
-
/**预设的发送数据类型 */
|
|
234
|
-
send(t) {
|
|
235
|
-
const map = {
|
|
236
|
-
'json': this.sendJson(),
|
|
237
|
-
'query': this.sendQuery(),
|
|
238
|
-
'formData': this.sendFormData(),
|
|
239
|
-
'form': this.sendForm(),
|
|
240
|
-
'file': this.sendFile(),
|
|
241
|
-
'none': this.sendNone(),
|
|
242
|
-
};
|
|
243
|
-
return map[t];
|
|
244
|
-
}
|
|
245
234
|
/**发送json
|
|
246
235
|
* 请求参数为 (token:JToken)
|
|
247
236
|
*/
|
|
@@ -253,7 +242,7 @@ class UtilHttp {
|
|
|
253
242
|
this._data.headers ??= {};
|
|
254
243
|
this._data.headers['Content-Length'] = Buffer.byteLength(data);
|
|
255
244
|
return {
|
|
256
|
-
proc:
|
|
245
|
+
proc: req => {
|
|
257
246
|
if (isPost)
|
|
258
247
|
req.write(data);
|
|
259
248
|
req.end();
|
|
@@ -265,24 +254,29 @@ class UtilHttp {
|
|
|
265
254
|
this._data.headers['Content-Type'] = 'application/json';
|
|
266
255
|
return this;
|
|
267
256
|
}
|
|
268
|
-
/**利用 appendQuery
|
|
257
|
+
/**利用 appendQuery 直接将表单附加在path上发送请求 */
|
|
269
258
|
sendQuery() {
|
|
270
259
|
const sendProc = (opt, arg) => {
|
|
271
260
|
opt.path = UtilHttp.buildQuery(opt.path ?? '', arg.query);
|
|
272
261
|
return {
|
|
273
|
-
proc:
|
|
262
|
+
proc: req => void req.end()
|
|
274
263
|
};
|
|
275
264
|
};
|
|
276
265
|
this._send = sendProc;
|
|
277
266
|
return this;
|
|
278
267
|
}
|
|
279
|
-
|
|
268
|
+
/**发送form-data包提供的表单数据 */
|
|
280
269
|
sendFormData() {
|
|
281
270
|
const sendProc = (opt, arg) => {
|
|
271
|
+
const { method } = opt;
|
|
272
|
+
const isPost = (method == "POST");
|
|
282
273
|
opt.headers = arg.formdata.getHeaders();
|
|
283
274
|
return {
|
|
284
275
|
proc: (req) => {
|
|
285
|
-
|
|
276
|
+
if (isPost)
|
|
277
|
+
arg.formdata.pipe(req);
|
|
278
|
+
else
|
|
279
|
+
req.end();
|
|
286
280
|
}
|
|
287
281
|
};
|
|
288
282
|
};
|
|
@@ -300,7 +294,7 @@ class UtilHttp {
|
|
|
300
294
|
this._data.headers ??= {};
|
|
301
295
|
this._data.headers['Content-Length'] = Buffer.byteLength(data);
|
|
302
296
|
return {
|
|
303
|
-
proc:
|
|
297
|
+
proc: req => {
|
|
304
298
|
if (isPost)
|
|
305
299
|
req.write(data);
|
|
306
300
|
req.end();
|
|
@@ -315,14 +309,19 @@ class UtilHttp {
|
|
|
315
309
|
/**发送文件 */
|
|
316
310
|
sendFile() {
|
|
317
311
|
const proc = (opt, arg) => {
|
|
312
|
+
const { method } = opt;
|
|
313
|
+
const isPost = (method == "POST");
|
|
318
314
|
let { filepath, filename } = arg;
|
|
319
315
|
const formData = new form_data_1.default();
|
|
320
316
|
filename = filename ?? pathe_1.default.basename(filepath);
|
|
321
317
|
formData.append(filename, fs_1.default.createReadStream(filepath));
|
|
322
318
|
opt.headers = formData.getHeaders();
|
|
323
319
|
return {
|
|
324
|
-
proc:
|
|
325
|
-
|
|
320
|
+
proc: req => {
|
|
321
|
+
if (isPost)
|
|
322
|
+
formData.pipe(req);
|
|
323
|
+
else
|
|
324
|
+
req.end();
|
|
326
325
|
}
|
|
327
326
|
};
|
|
328
327
|
};
|
|
@@ -346,8 +345,12 @@ class UtilHttp {
|
|
|
346
345
|
*/
|
|
347
346
|
async once(arg) {
|
|
348
347
|
const fullopt = { ...this._data, ...arg.option ?? {} };
|
|
349
|
-
const sendProc =
|
|
350
|
-
|
|
348
|
+
const sendProc = typeof this._send === 'function'
|
|
349
|
+
? await this._send(fullopt, arg)
|
|
350
|
+
: this._send;
|
|
351
|
+
const { parse, ...acceptProc } = typeof this._accept === 'function'
|
|
352
|
+
? await this._accept(arg)
|
|
353
|
+
: this._accept;
|
|
351
354
|
const res = await UtilHttp.request(fullopt, sendProc, acceptProc);
|
|
352
355
|
return parse(res);
|
|
353
356
|
}
|
|
@@ -407,18 +410,20 @@ class UtilHttp {
|
|
|
407
410
|
return;
|
|
408
411
|
}
|
|
409
412
|
res.setEncoding(option.responseEncode ?? 'utf8');
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
413
|
+
if (reqReduce) {
|
|
414
|
+
res.on('data', chunk => {
|
|
415
|
+
dataPromise = reduceQueue
|
|
416
|
+
.enqueue(async () => {
|
|
417
|
+
try {
|
|
418
|
+
mergedata = await reqReduce(mergedata, chunk);
|
|
419
|
+
}
|
|
420
|
+
catch (err) {
|
|
421
|
+
UtilLogger_1.SLogger.error(`${flagName} reduce函数错误:`, err, `chunk:${chunk}\nmergedata:`, mergedata);
|
|
422
|
+
finallyResolve(undefined);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
420
425
|
});
|
|
421
|
-
}
|
|
426
|
+
}
|
|
422
427
|
res.on('error', err => {
|
|
423
428
|
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈错误:`, err);
|
|
424
429
|
finallyResolve(undefined);
|