@zwa73/utils 1.0.199 → 1.0.201
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 +24 -22
- package/dist/UtilCom.js +21 -19
- package/package.json +1 -1
- package/src/UtilCom.ts +32 -24
package/dist/UtilCom.d.ts
CHANGED
|
@@ -39,10 +39,10 @@ export type QueryRequestData = NodeJS.Dict<string | number | boolean | readonly
|
|
|
39
39
|
export type RequestProcFn = ((req: http.ClientRequest) => MPromise<void>);
|
|
40
40
|
/**数据处理函数 */
|
|
41
41
|
export type RequestReduceFn<T> = (acc: T, data: string) => MPromise<T>;
|
|
42
|
-
declare const AcceptTypeList: readonly ["json", "
|
|
42
|
+
declare const AcceptTypeList: readonly ["json", "string"];
|
|
43
43
|
/**可用的接受类型 */
|
|
44
44
|
type AcceptType = typeof AcceptTypeList[number];
|
|
45
|
-
declare const SendTypeList: readonly ["json", "query", "formData", "none"
|
|
45
|
+
declare const SendTypeList: readonly ["json", "query", "formData", "none"];
|
|
46
46
|
/**可用的发送类型 */
|
|
47
47
|
type SendType = typeof SendTypeList[number];
|
|
48
48
|
/**accept处理数据 */
|
|
@@ -56,7 +56,7 @@ type SendProc<T extends any[]> = {
|
|
|
56
56
|
proc: (opt: RequestOption, ...args: T) => MPromise<RequestProcFn>;
|
|
57
57
|
};
|
|
58
58
|
declare const SendNoneProc: SendProc<[]>;
|
|
59
|
-
declare const
|
|
59
|
+
declare const AcceptStringProc: AcceptProc<string, RequestResult<string> | undefined>;
|
|
60
60
|
/**send处理的参数 */
|
|
61
61
|
type SendParams<T extends SendProc<any>> = T extends SendProc<infer T> ? T : never;
|
|
62
62
|
/**accept处理的结果 */
|
|
@@ -100,7 +100,7 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
|
|
|
100
100
|
headers: {
|
|
101
101
|
"Content-Type": "application/json";
|
|
102
102
|
};
|
|
103
|
-
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
103
|
+
}, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
104
104
|
/**收发皆为json的post预设 */
|
|
105
105
|
postJson(): UtilCom<D & {
|
|
106
106
|
method: "POST";
|
|
@@ -108,15 +108,15 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
|
|
|
108
108
|
headers: {
|
|
109
109
|
'Content-Type': "application/json";
|
|
110
110
|
};
|
|
111
|
-
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
111
|
+
}, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
112
112
|
/**无查询参数获取json的get预设 */
|
|
113
113
|
getJson(): UtilCom<D & {
|
|
114
114
|
method: "GET";
|
|
115
|
-
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
115
|
+
}, SendProc<[]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
116
116
|
/**有查询参数获取json的get预设 */
|
|
117
117
|
queryJson(): UtilCom<D & {
|
|
118
118
|
method: "GET";
|
|
119
|
-
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
119
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
120
120
|
/**收发皆为json的https-post预设 */
|
|
121
121
|
static httpsPostJson(): UtilCom<{
|
|
122
122
|
readonly protocol: "https:";
|
|
@@ -126,7 +126,7 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
|
|
|
126
126
|
headers: {
|
|
127
127
|
'Content-Type': "application/json";
|
|
128
128
|
};
|
|
129
|
-
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
129
|
+
}, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
130
130
|
/**收发皆为json的http-post预设 */
|
|
131
131
|
static httpPostJson(): UtilCom<{
|
|
132
132
|
readonly protocol: "http:";
|
|
@@ -136,39 +136,41 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
|
|
|
136
136
|
headers: {
|
|
137
137
|
'Content-Type': "application/json";
|
|
138
138
|
};
|
|
139
|
-
}, SendProc<[JToken]>, AcceptProc<string, JToken>>;
|
|
139
|
+
}, SendProc<[JToken]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
140
140
|
/**无查询参数获取json的https-get预设 */
|
|
141
141
|
static httpsGetJson(): UtilCom<{
|
|
142
142
|
readonly protocol: "https:";
|
|
143
143
|
} & {
|
|
144
144
|
method: "GET";
|
|
145
|
-
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
145
|
+
}, SendProc<[]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
146
146
|
/**有查询参数获取json的https-get预设 */
|
|
147
147
|
static httpsQueryJson(): UtilCom<{
|
|
148
148
|
readonly protocol: "http:";
|
|
149
149
|
} & {
|
|
150
150
|
method: "GET";
|
|
151
|
-
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
151
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
152
152
|
/**无查询参数获取json的http-get预设 */
|
|
153
153
|
static httpGetJson(): UtilCom<{
|
|
154
154
|
readonly protocol: "http:";
|
|
155
155
|
} & {
|
|
156
156
|
method: "GET";
|
|
157
|
-
}, SendProc<[]>, AcceptProc<string, JToken>>;
|
|
157
|
+
}, SendProc<[]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
158
158
|
/**有查询参数获取json的http-get预设 */
|
|
159
159
|
static httpQueryJson(): UtilCom<{
|
|
160
160
|
readonly protocol: "http:";
|
|
161
161
|
} & {
|
|
162
162
|
method: "GET";
|
|
163
|
-
}, SendProc<[QueryRequestData]>, AcceptProc<string, JToken>>;
|
|
164
|
-
|
|
163
|
+
}, SendProc<[QueryRequestData]>, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
164
|
+
/**预设的接收数据类型*/
|
|
165
165
|
accept<T extends AcceptType>(t: T): {
|
|
166
|
-
readonly json: UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
167
|
-
readonly
|
|
166
|
+
readonly json: UtilCom<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
167
|
+
readonly string: UtilCom<D, S, AcceptProc<string, RequestResult<string> | undefined>>;
|
|
168
168
|
}[T];
|
|
169
|
-
acceptJson(): UtilCom<D, S, AcceptProc<string, JToken>>;
|
|
170
|
-
|
|
171
|
-
|
|
169
|
+
acceptJson(): UtilCom<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
|
|
170
|
+
acceptString(): UtilCom<D, S, typeof AcceptStringProc>;
|
|
171
|
+
/**自定的接收数据类型*/
|
|
172
|
+
acceptRaw<AD, AT>(proc: AcceptProc<AD, AT>): UtilCom<D, S, typeof proc>;
|
|
173
|
+
/**预设的发送数据类型*/
|
|
172
174
|
send<T extends SendType>(t: T): {
|
|
173
175
|
readonly json: UtilCom<D & {
|
|
174
176
|
headers: {
|
|
@@ -182,7 +184,6 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
|
|
|
182
184
|
};
|
|
183
185
|
}, SendProc<[FormData]>, A>;
|
|
184
186
|
readonly none: UtilCom<D, SendProc<[]>, A>;
|
|
185
|
-
readonly raw: UtilCom<D, SendProc<[(opt: RequestOption) => RequestProcFn]>, A>;
|
|
186
187
|
}[T];
|
|
187
188
|
/**利用 req.write 发送一段json */
|
|
188
189
|
sendJson(): UtilCom<D & {
|
|
@@ -197,13 +198,14 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
|
|
|
197
198
|
"Content-Type": "multipart/form-data";
|
|
198
199
|
};
|
|
199
200
|
}, SendProc<[FormData]>, A>;
|
|
200
|
-
sendRaw(): UtilCom<D, SendProc<[(opt: RequestOption) => RequestProcFn]>, A>;
|
|
201
201
|
sendNone(): UtilCom<D, typeof SendNoneProc, A>;
|
|
202
|
+
/**自定的发送数据类型*/
|
|
203
|
+
sendRaw<T extends any[]>(proc: SendProc<T>): UtilCom<D, typeof proc, A>;
|
|
202
204
|
/**发送请求
|
|
203
205
|
* @param option - 网络请求选项
|
|
204
206
|
* @param datas - 数据对象
|
|
205
207
|
*/
|
|
206
|
-
once(option: PartialOption<RequestOption, D>, ...datas: SendParams<S>): Promise<
|
|
208
|
+
once(option: PartialOption<RequestOption, D>, ...datas: SendParams<S>): Promise<ParseResult<A>>;
|
|
207
209
|
/**重复发送网络请求
|
|
208
210
|
* @param option - 网络请求选项
|
|
209
211
|
* @param verify - 有效性验证函数
|
package/dist/UtilCom.js
CHANGED
|
@@ -12,12 +12,12 @@ const querystring_1 = __importDefault(require("querystring"));
|
|
|
12
12
|
const UtilSymbol_1 = require("./UtilSymbol");
|
|
13
13
|
const http_proxy_agent_1 = __importDefault(require("http-proxy-agent"));
|
|
14
14
|
const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
|
|
15
|
-
const AcceptTypeList = ["json", "
|
|
16
|
-
const SendTypeList = ["json", "query", "formData", "none"
|
|
15
|
+
const AcceptTypeList = ["json", "string"];
|
|
16
|
+
const SendTypeList = ["json", "query", "formData", "none"];
|
|
17
17
|
const SendNoneProc = {
|
|
18
18
|
proc: (opt) => (req) => void req.end()
|
|
19
19
|
};
|
|
20
|
-
const
|
|
20
|
+
const AcceptStringProc = {
|
|
21
21
|
init: '',
|
|
22
22
|
reduce: (acc, dat) => acc + dat,
|
|
23
23
|
parse: (result) => result
|
|
@@ -35,11 +35,11 @@ class UtilCom {
|
|
|
35
35
|
//#region 流式创建
|
|
36
36
|
/**设为https请求 */
|
|
37
37
|
static https() {
|
|
38
|
-
return new UtilCom({ protocol: 'https:' }, SendNoneProc,
|
|
38
|
+
return new UtilCom({ protocol: 'https:' }, SendNoneProc, AcceptStringProc);
|
|
39
39
|
}
|
|
40
40
|
/**设为http请求 */
|
|
41
41
|
static http() {
|
|
42
|
-
return new UtilCom({ protocol: 'http:' }, SendNoneProc,
|
|
42
|
+
return new UtilCom({ protocol: 'http:' }, SendNoneProc, AcceptStringProc);
|
|
43
43
|
}
|
|
44
44
|
/**设为get方式的请求 */
|
|
45
45
|
get() {
|
|
@@ -123,11 +123,11 @@ class UtilCom {
|
|
|
123
123
|
}
|
|
124
124
|
//#endregion
|
|
125
125
|
//#region 接收数据类型
|
|
126
|
-
|
|
126
|
+
/**预设的接收数据类型*/
|
|
127
127
|
accept(t) {
|
|
128
128
|
const map = {
|
|
129
129
|
'json': this.acceptJson(),
|
|
130
|
-
'
|
|
130
|
+
'string': this.acceptString(),
|
|
131
131
|
};
|
|
132
132
|
return map[t];
|
|
133
133
|
}
|
|
@@ -145,7 +145,7 @@ class UtilCom {
|
|
|
145
145
|
}
|
|
146
146
|
try {
|
|
147
147
|
const obj = JSON.parse(data.trim());
|
|
148
|
-
UtilLogger_1.SLogger.http(`json accept
|
|
148
|
+
UtilLogger_1.SLogger.http(`json accept 接受信息 data:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }), `result:`, UtilFunctions_1.UtilFunc.stringifyJToken(rest, { compress: true, space: 2 }));
|
|
149
149
|
return { ...rest, data: obj };
|
|
150
150
|
}
|
|
151
151
|
catch (e) {
|
|
@@ -157,20 +157,24 @@ class UtilCom {
|
|
|
157
157
|
this._accept = proc;
|
|
158
158
|
return this;
|
|
159
159
|
}
|
|
160
|
-
|
|
161
|
-
this._accept =
|
|
160
|
+
acceptString() {
|
|
161
|
+
this._accept = AcceptStringProc;
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
/**自定的接收数据类型*/
|
|
165
|
+
acceptRaw(proc) {
|
|
166
|
+
this._accept = proc;
|
|
162
167
|
return this;
|
|
163
168
|
}
|
|
164
169
|
//#endregion
|
|
165
170
|
//#region 发送数据类型
|
|
166
|
-
|
|
171
|
+
/**预设的发送数据类型*/
|
|
167
172
|
send(t) {
|
|
168
173
|
const map = {
|
|
169
174
|
'json': this.sendJson(),
|
|
170
175
|
'query': this.sendQuery(),
|
|
171
176
|
'formData': this.sendFormData(),
|
|
172
177
|
'none': this.sendNone(),
|
|
173
|
-
'raw': this.sendRaw(),
|
|
174
178
|
};
|
|
175
179
|
return map[t];
|
|
176
180
|
}
|
|
@@ -223,17 +227,15 @@ class UtilCom {
|
|
|
223
227
|
this._data.headers['Content-Type'] = 'multipart/form-data';
|
|
224
228
|
return this;
|
|
225
229
|
}
|
|
226
|
-
sendRaw() {
|
|
227
|
-
const proc = {
|
|
228
|
-
proc: (opt, fn) => fn(opt),
|
|
229
|
-
};
|
|
230
|
-
this._send = proc;
|
|
231
|
-
return this;
|
|
232
|
-
}
|
|
233
230
|
sendNone() {
|
|
234
231
|
this._send = SendNoneProc;
|
|
235
232
|
return this;
|
|
236
233
|
}
|
|
234
|
+
/**自定的发送数据类型*/
|
|
235
|
+
sendRaw(proc) {
|
|
236
|
+
this._send = proc;
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
237
239
|
//#endregion
|
|
238
240
|
/**发送请求
|
|
239
241
|
* @param option - 网络请求选项
|
package/package.json
CHANGED
package/src/UtilCom.ts
CHANGED
|
@@ -59,10 +59,10 @@ export type RequestProcFn = ((req:http.ClientRequest)=>MPromise<void>);
|
|
|
59
59
|
export type RequestReduceFn<T> = (acc:T,data:string)=>MPromise<T>;
|
|
60
60
|
|
|
61
61
|
|
|
62
|
-
const AcceptTypeList = ["json","
|
|
62
|
+
const AcceptTypeList = ["json","string"]as const;
|
|
63
63
|
/**可用的接受类型 */
|
|
64
64
|
type AcceptType = typeof AcceptTypeList[number];
|
|
65
|
-
const SendTypeList = ["json","query","formData","none"
|
|
65
|
+
const SendTypeList = ["json","query","formData","none"] as const;
|
|
66
66
|
/**可用的发送类型 */
|
|
67
67
|
type SendType = typeof SendTypeList[number];
|
|
68
68
|
|
|
@@ -79,11 +79,12 @@ type SendProc<T extends any[]> = {
|
|
|
79
79
|
const SendNoneProc:SendProc<[]> = {
|
|
80
80
|
proc:(opt)=>(req)=>void req.end()
|
|
81
81
|
}
|
|
82
|
-
const
|
|
82
|
+
const AcceptStringProc:AcceptProc<string,RequestResult<string>|undefined> = {
|
|
83
83
|
init :'',
|
|
84
84
|
reduce:(acc:string,dat:string)=>acc+dat,
|
|
85
85
|
parse :(result:RequestResult<string>|undefined)=>result
|
|
86
86
|
}
|
|
87
|
+
|
|
87
88
|
/**send处理的参数 */
|
|
88
89
|
type SendParams<T extends SendProc<any>> = T extends SendProc<infer T>
|
|
89
90
|
? T : never;
|
|
@@ -102,11 +103,11 @@ A extends AcceptProc<any,any>,
|
|
|
102
103
|
//#region 流式创建
|
|
103
104
|
/**设为https请求 */
|
|
104
105
|
static https(){
|
|
105
|
-
return new UtilCom({protocol:'https:'} as const,SendNoneProc,
|
|
106
|
+
return new UtilCom({protocol:'https:'} as const,SendNoneProc,AcceptStringProc);
|
|
106
107
|
}
|
|
107
108
|
/**设为http请求 */
|
|
108
109
|
static http(){
|
|
109
|
-
return new UtilCom({protocol:'http:'} as const,SendNoneProc,
|
|
110
|
+
return new UtilCom({protocol:'http:'} as const,SendNoneProc,AcceptStringProc);
|
|
110
111
|
}
|
|
111
112
|
/**设为get方式的请求 */
|
|
112
113
|
get(){
|
|
@@ -192,16 +193,16 @@ A extends AcceptProc<any,any>,
|
|
|
192
193
|
//#endregion
|
|
193
194
|
|
|
194
195
|
//#region 接收数据类型
|
|
195
|
-
|
|
196
|
+
/**预设的接收数据类型*/
|
|
196
197
|
accept<T extends AcceptType>(t:T){
|
|
197
198
|
const map = {
|
|
198
|
-
'json':this.acceptJson(),
|
|
199
|
-
'
|
|
199
|
+
'json' :this.acceptJson(),
|
|
200
|
+
'string':this.acceptString(),
|
|
200
201
|
} as const;
|
|
201
202
|
return map[t];
|
|
202
203
|
}
|
|
203
204
|
acceptJson(){
|
|
204
|
-
const proc:AcceptProc<string,JToken>={
|
|
205
|
+
const proc:AcceptProc<string,RequestResult<JToken>|undefined>={
|
|
205
206
|
init:'',
|
|
206
207
|
reduce:(acc,curr)=>acc+curr,
|
|
207
208
|
parse:(result)=>{
|
|
@@ -214,7 +215,12 @@ A extends AcceptProc<any,any>,
|
|
|
214
215
|
}
|
|
215
216
|
try{
|
|
216
217
|
const obj = JSON.parse(data.trim()) as JToken;
|
|
217
|
-
SLogger.http(
|
|
218
|
+
SLogger.http(
|
|
219
|
+
`json accept 接受信息 data:`,
|
|
220
|
+
UtilFunc.stringifyJToken(obj,{compress:true,space:2}),
|
|
221
|
+
`result:`,
|
|
222
|
+
UtilFunc.stringifyJToken(rest,{compress:true,space:2})
|
|
223
|
+
);
|
|
218
224
|
return{...rest,data:obj};
|
|
219
225
|
}
|
|
220
226
|
catch(e){
|
|
@@ -224,23 +230,27 @@ A extends AcceptProc<any,any>,
|
|
|
224
230
|
}
|
|
225
231
|
}
|
|
226
232
|
this._accept = proc as any;
|
|
227
|
-
return this as any as UtilCom<D,S,typeof proc>;
|
|
233
|
+
return this as any as UtilCom<D,S,typeof proc>;
|
|
234
|
+
}
|
|
235
|
+
acceptString(){
|
|
236
|
+
this._accept = AcceptStringProc as any;
|
|
237
|
+
return this as any as UtilCom<D,S,typeof AcceptStringProc>;
|
|
228
238
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
239
|
+
/**自定的接收数据类型*/
|
|
240
|
+
acceptRaw<AD,AT>(proc:AcceptProc<AD,AT>){
|
|
241
|
+
this._accept = proc as any;
|
|
242
|
+
return this as any as UtilCom<D,S,typeof proc>;
|
|
232
243
|
}
|
|
233
244
|
//#endregion
|
|
234
245
|
|
|
235
246
|
//#region 发送数据类型
|
|
236
|
-
|
|
247
|
+
/**预设的发送数据类型*/
|
|
237
248
|
send<T extends SendType>(t:T){
|
|
238
249
|
const map = {
|
|
239
250
|
'json' :this.sendJson(),
|
|
240
251
|
'query' :this.sendQuery(),
|
|
241
252
|
'formData' :this.sendFormData(),
|
|
242
253
|
'none' :this.sendNone(),
|
|
243
|
-
'raw' :this.sendRaw(),
|
|
244
254
|
} as const;
|
|
245
255
|
return map[t];
|
|
246
256
|
}
|
|
@@ -298,17 +308,15 @@ A extends AcceptProc<any,any>,
|
|
|
298
308
|
headers:{ 'Content-Type': 'multipart/form-data' }
|
|
299
309
|
}, typeof proc, A>;
|
|
300
310
|
}
|
|
301
|
-
sendRaw(){
|
|
302
|
-
const proc: SendProc<[(opt:RequestOption)=>RequestProcFn]> = {
|
|
303
|
-
proc: (opt: RequestOption, fn: ((opt:RequestOption)=>RequestProcFn)) => fn(opt),
|
|
304
|
-
};
|
|
305
|
-
this._send = proc as any;
|
|
306
|
-
return this as any as UtilCom<D, typeof proc, A>;
|
|
307
|
-
}
|
|
308
311
|
sendNone(){
|
|
309
312
|
this._send = SendNoneProc as any;
|
|
310
313
|
return this as any as UtilCom<D,typeof SendNoneProc,A>;
|
|
311
314
|
}
|
|
315
|
+
/**自定的发送数据类型*/
|
|
316
|
+
sendRaw<T extends any[]>(proc:SendProc<T>){
|
|
317
|
+
this._send = proc as any;
|
|
318
|
+
return this as any as UtilCom<D, typeof proc, A>;
|
|
319
|
+
}
|
|
312
320
|
//#endregion
|
|
313
321
|
|
|
314
322
|
/**发送请求
|
|
@@ -320,7 +328,7 @@ A extends AcceptProc<any,any>,
|
|
|
320
328
|
const proc = await this._send.proc(fullopt,...datas as any[]);
|
|
321
329
|
const {reduce,init,parse} = this._accept;
|
|
322
330
|
const res = await UtilCom.request(fullopt,proc,reduce,init);
|
|
323
|
-
return parse(res)
|
|
331
|
+
return parse(res) as ParseResult<A>;
|
|
324
332
|
}
|
|
325
333
|
/**重复发送网络请求
|
|
326
334
|
* @param option - 网络请求选项
|