@whitesev/utils 2.7.1 → 2.7.3
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/index.amd.js +260 -409
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +260 -409
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +260 -409
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +260 -409
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +260 -409
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +260 -409
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/ColorConversion.d.ts +3 -8
- package/dist/types/src/Dictionary.d.ts +21 -14
- package/dist/types/src/GBKEncoder.d.ts +1 -2
- package/dist/types/src/Hooks.d.ts +1 -2
- package/dist/types/src/Httpx.d.ts +45 -46
- package/dist/types/src/LockFunction.d.ts +1 -2
- package/dist/types/src/Log.d.ts +1 -2
- package/dist/types/src/Progress.d.ts +1 -2
- package/dist/types/src/Utils.d.ts +30 -2
- package/dist/types/src/UtilsGMMenu.d.ts +1 -2
- package/dist/types/src/WindowApi.d.ts +1 -2
- package/dist/types/src/indexedDB.d.ts +1 -2
- package/dist/types/src/types/Event.d.ts +1 -2
- package/dist/types/src/types/Httpx.d.ts +75 -86
- package/dist/types/src/types/ajaxHooker.d.ts +1 -5
- package/dist/types/src/types/env.d.ts +2 -0
- package/dist/types/src/types/global.d.ts +3 -0
- package/package.json +1 -1
- package/src/ColorConversion.ts +13 -37
- package/src/CommonUtil.ts +8 -31
- package/src/DOMUtils.ts +9 -24
- package/src/Dictionary.ts +37 -38
- package/src/GBKEncoder.ts +9 -18
- package/src/Hooks.ts +2 -7
- package/src/Httpx.ts +257 -412
- package/src/LockFunction.ts +1 -3
- package/src/Log.ts +8 -26
- package/src/Progress.ts +3 -13
- package/src/TryCatch.ts +4 -12
- package/src/Utils.ts +233 -595
- package/src/UtilsCommon.ts +5 -9
- package/src/UtilsGMCookie.ts +1 -4
- package/src/UtilsGMMenu.ts +29 -51
- package/src/Vue.ts +6 -18
- package/src/WindowApi.ts +2 -5
- package/src/indexedDB.ts +11 -20
- package/src/types/Event.d.ts +1 -2
- package/src/types/Httpx.d.ts +75 -86
- package/src/types/ajaxHooker.d.ts +1 -5
- package/src/types/env.d.ts +2 -0
- package/src/types/global.d.ts +3 -0
package/src/Httpx.ts
CHANGED
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
} from "./types/Httpx";
|
|
13
13
|
import { GenerateUUID } from "./UtilsCommon";
|
|
14
14
|
|
|
15
|
-
class Httpx {
|
|
15
|
+
export class Httpx {
|
|
16
16
|
private GM_Api = {
|
|
17
17
|
xmlHttpRequest: null as any,
|
|
18
18
|
};
|
|
@@ -79,9 +79,7 @@ class Httpx {
|
|
|
79
79
|
});
|
|
80
80
|
return uuid;
|
|
81
81
|
} else {
|
|
82
|
-
console.warn(
|
|
83
|
-
"[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function"
|
|
84
|
-
);
|
|
82
|
+
console.warn("[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function");
|
|
85
83
|
}
|
|
86
84
|
},
|
|
87
85
|
/**
|
|
@@ -90,9 +88,7 @@ class Httpx {
|
|
|
90
88
|
*/
|
|
91
89
|
delete(id: string) {
|
|
92
90
|
if (typeof id === "string") {
|
|
93
|
-
let findIndex = this.$config.configList.findIndex(
|
|
94
|
-
(item) => item.id === id
|
|
95
|
-
);
|
|
91
|
+
let findIndex = this.$config.configList.findIndex((item) => item.id === id);
|
|
96
92
|
if (findIndex !== -1) {
|
|
97
93
|
this.$config.configList.splice(findIndex, 1);
|
|
98
94
|
return true;
|
|
@@ -139,8 +135,7 @@ class Httpx {
|
|
|
139
135
|
// 配置存在
|
|
140
136
|
// 细分处理是否拦截
|
|
141
137
|
if (
|
|
142
|
-
typeof details.allowInterceptConfig.afterResponseSuccess ===
|
|
143
|
-
"boolean" &&
|
|
138
|
+
typeof details.allowInterceptConfig.afterResponseSuccess === "boolean" &&
|
|
144
139
|
!details.allowInterceptConfig.afterResponseSuccess
|
|
145
140
|
) {
|
|
146
141
|
// 设置了禁止拦截
|
|
@@ -179,8 +174,7 @@ class Httpx {
|
|
|
179
174
|
// 配置存在
|
|
180
175
|
// 细分处理是否拦截
|
|
181
176
|
if (
|
|
182
|
-
typeof data.details.allowInterceptConfig.afterResponseError ===
|
|
183
|
-
"boolean" &&
|
|
177
|
+
typeof data.details.allowInterceptConfig.afterResponseError === "boolean" &&
|
|
184
178
|
!data.details.allowInterceptConfig.afterResponseError
|
|
185
179
|
) {
|
|
186
180
|
// 设置了禁止拦截
|
|
@@ -220,9 +214,7 @@ class Httpx {
|
|
|
220
214
|
*/
|
|
221
215
|
delete(id: string) {
|
|
222
216
|
if (typeof id === "string") {
|
|
223
|
-
let findIndex = this.$config.configList.findIndex(
|
|
224
|
-
(item) => item.id === id
|
|
225
|
-
);
|
|
217
|
+
let findIndex = this.$config.configList.findIndex((item) => item.id === id);
|
|
226
218
|
if (findIndex !== -1) {
|
|
227
219
|
this.$config.configList.splice(findIndex, 1);
|
|
228
220
|
return true;
|
|
@@ -243,7 +235,9 @@ class Httpx {
|
|
|
243
235
|
* 对请求的参数进行合并处理
|
|
244
236
|
*/
|
|
245
237
|
handleBeforeRequestOptionArgs(...args: (HttpxRequestOption | string)[]) {
|
|
246
|
-
let option: HttpxRequestOption = {
|
|
238
|
+
let option: HttpxRequestOption = {
|
|
239
|
+
url: void 0 as any as string,
|
|
240
|
+
};
|
|
247
241
|
if (typeof args[0] === "string") {
|
|
248
242
|
/* 传入的是url,转为配置 */
|
|
249
243
|
let url = args[0];
|
|
@@ -266,7 +260,7 @@ class Httpx {
|
|
|
266
260
|
* @param method 当前请求方法,默认get
|
|
267
261
|
* @param userRequestOption 用户的请求配置
|
|
268
262
|
* @param resolve promise回调
|
|
269
|
-
* @param reject 抛出错误回调
|
|
263
|
+
* @param reject promise抛出错误回调
|
|
270
264
|
*/
|
|
271
265
|
getRequestOption(
|
|
272
266
|
method: HttpxMethod,
|
|
@@ -291,70 +285,43 @@ class Httpx {
|
|
|
291
285
|
let requestOption = <Required<HttpxRequestOption>>{
|
|
292
286
|
url: url,
|
|
293
287
|
method: (method || "GET").toString().toUpperCase().trim(),
|
|
294
|
-
timeout:
|
|
295
|
-
|
|
296
|
-
this.context.#defaultRequestOption.timeout,
|
|
297
|
-
responseType:
|
|
298
|
-
userRequestOption.responseType ||
|
|
299
|
-
this.context.#defaultRequestOption.responseType,
|
|
288
|
+
timeout: userRequestOption.timeout || this.context.#defaultRequestOption.timeout,
|
|
289
|
+
responseType: userRequestOption.responseType || this.context.#defaultRequestOption.responseType,
|
|
300
290
|
/* 对象使用深拷贝 */
|
|
301
|
-
headers: CommonUtil.deepClone(
|
|
302
|
-
this.context.#defaultRequestOption.headers
|
|
303
|
-
),
|
|
291
|
+
headers: CommonUtil.deepClone(this.context.#defaultRequestOption.headers),
|
|
304
292
|
data: userRequestOption.data || this.context.#defaultRequestOption.data,
|
|
305
|
-
redirect:
|
|
306
|
-
|
|
307
|
-
this.context.#defaultRequestOption.redirect,
|
|
308
|
-
cookie:
|
|
309
|
-
userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
|
|
293
|
+
redirect: userRequestOption.redirect || this.context.#defaultRequestOption.redirect,
|
|
294
|
+
cookie: userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
|
|
310
295
|
cookiePartition:
|
|
311
|
-
userRequestOption.cookiePartition ||
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
nocache:
|
|
316
|
-
userRequestOption.nocache ||
|
|
317
|
-
this.context.#defaultRequestOption.nocache,
|
|
318
|
-
revalidate:
|
|
319
|
-
userRequestOption.revalidate ||
|
|
320
|
-
this.context.#defaultRequestOption.revalidate,
|
|
296
|
+
userRequestOption.cookiePartition || this.context.#defaultRequestOption.cookiePartition,
|
|
297
|
+
binary: userRequestOption.binary || this.context.#defaultRequestOption.binary,
|
|
298
|
+
nocache: userRequestOption.nocache || this.context.#defaultRequestOption.nocache,
|
|
299
|
+
revalidate: userRequestOption.revalidate || this.context.#defaultRequestOption.revalidate,
|
|
321
300
|
/* 对象使用深拷贝 */
|
|
322
301
|
context: CommonUtil.deepClone(
|
|
323
|
-
userRequestOption.context ||
|
|
324
|
-
this.context.#defaultRequestOption.context
|
|
302
|
+
userRequestOption.context || this.context.#defaultRequestOption.context
|
|
325
303
|
),
|
|
326
304
|
overrideMimeType:
|
|
327
|
-
userRequestOption.overrideMimeType ||
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
userRequestOption.anonymous ||
|
|
331
|
-
this.context.#defaultRequestOption.anonymous,
|
|
332
|
-
fetch:
|
|
333
|
-
userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
|
|
305
|
+
userRequestOption.overrideMimeType || this.context.#defaultRequestOption.overrideMimeType,
|
|
306
|
+
anonymous: userRequestOption.anonymous || this.context.#defaultRequestOption.anonymous,
|
|
307
|
+
fetch: userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
|
|
334
308
|
/* 对象使用深拷贝 */
|
|
335
|
-
fetchInit: CommonUtil.deepClone(
|
|
336
|
-
this.context.#defaultRequestOption.fetchInit
|
|
337
|
-
),
|
|
309
|
+
fetchInit: CommonUtil.deepClone(this.context.#defaultRequestOption.fetchInit),
|
|
338
310
|
allowInterceptConfig: {
|
|
339
311
|
beforeRequest: (
|
|
340
|
-
this.context.#defaultRequestOption
|
|
341
|
-
.allowInterceptConfig as HttpxAllowInterceptConfig
|
|
312
|
+
this.context.#defaultRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig
|
|
342
313
|
).beforeRequest,
|
|
343
314
|
afterResponseSuccess: (
|
|
344
|
-
this.context.#defaultRequestOption
|
|
345
|
-
.allowInterceptConfig as HttpxAllowInterceptConfig
|
|
315
|
+
this.context.#defaultRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig
|
|
346
316
|
).afterResponseSuccess,
|
|
347
317
|
afterResponseError: (
|
|
348
|
-
this.context.#defaultRequestOption
|
|
349
|
-
.allowInterceptConfig as HttpxAllowInterceptConfig
|
|
318
|
+
this.context.#defaultRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig
|
|
350
319
|
).afterResponseError,
|
|
351
320
|
},
|
|
352
321
|
user: userRequestOption.user || this.context.#defaultRequestOption.user,
|
|
353
|
-
password:
|
|
354
|
-
userRequestOption.password ||
|
|
355
|
-
this.context.#defaultRequestOption.password,
|
|
322
|
+
password: userRequestOption.password || this.context.#defaultRequestOption.password,
|
|
356
323
|
onabort(...args) {
|
|
357
|
-
that.context.
|
|
324
|
+
that.context.HttpxResponseCallBack.onAbort(
|
|
358
325
|
userRequestOption as Required<HttpxRequestOption>,
|
|
359
326
|
resolve,
|
|
360
327
|
reject,
|
|
@@ -362,7 +329,7 @@ class Httpx {
|
|
|
362
329
|
);
|
|
363
330
|
},
|
|
364
331
|
onerror(...args) {
|
|
365
|
-
that.context.
|
|
332
|
+
that.context.HttpxResponseCallBack.onError(
|
|
366
333
|
userRequestOption as Required<HttpxRequestOption>,
|
|
367
334
|
resolve,
|
|
368
335
|
reject,
|
|
@@ -370,25 +337,25 @@ class Httpx {
|
|
|
370
337
|
);
|
|
371
338
|
},
|
|
372
339
|
onloadstart(...args) {
|
|
373
|
-
that.context.
|
|
340
|
+
that.context.HttpxResponseCallBack.onLoadStart(
|
|
374
341
|
userRequestOption as Required<HttpxRequestOption>,
|
|
375
342
|
args
|
|
376
343
|
);
|
|
377
344
|
},
|
|
378
345
|
onprogress(...args) {
|
|
379
|
-
that.context.
|
|
346
|
+
that.context.HttpxResponseCallBack.onProgress(
|
|
380
347
|
userRequestOption as Required<HttpxRequestOption>,
|
|
381
348
|
args
|
|
382
349
|
);
|
|
383
350
|
},
|
|
384
351
|
onreadystatechange(...args) {
|
|
385
|
-
that.context.
|
|
352
|
+
that.context.HttpxResponseCallBack.onReadyStateChange(
|
|
386
353
|
userRequestOption as Required<HttpxRequestOption>,
|
|
387
354
|
args
|
|
388
355
|
);
|
|
389
356
|
},
|
|
390
357
|
ontimeout(...args) {
|
|
391
|
-
that.context.
|
|
358
|
+
that.context.HttpxResponseCallBack.onTimeout(
|
|
392
359
|
userRequestOption as Required<HttpxRequestOption>,
|
|
393
360
|
resolve,
|
|
394
361
|
reject,
|
|
@@ -396,7 +363,7 @@ class Httpx {
|
|
|
396
363
|
);
|
|
397
364
|
},
|
|
398
365
|
onload(...args) {
|
|
399
|
-
that.context.
|
|
366
|
+
that.context.HttpxResponseCallBack.onLoad(
|
|
400
367
|
userRequestOption as Required<HttpxRequestOption>,
|
|
401
368
|
resolve,
|
|
402
369
|
reject,
|
|
@@ -406,9 +373,7 @@ class Httpx {
|
|
|
406
373
|
};
|
|
407
374
|
// 补全allowInterceptConfig参数
|
|
408
375
|
if (typeof userRequestOption.allowInterceptConfig === "boolean") {
|
|
409
|
-
Object.keys(
|
|
410
|
-
requestOption.allowInterceptConfig as HttpxAllowInterceptConfig
|
|
411
|
-
).forEach((keyName) => {
|
|
376
|
+
Object.keys(requestOption.allowInterceptConfig as HttpxAllowInterceptConfig).forEach((keyName) => {
|
|
412
377
|
Reflect.set(
|
|
413
378
|
requestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
|
|
414
379
|
keyName,
|
|
@@ -420,27 +385,18 @@ class Httpx {
|
|
|
420
385
|
typeof userRequestOption.allowInterceptConfig === "object" &&
|
|
421
386
|
userRequestOption.allowInterceptConfig != null
|
|
422
387
|
) {
|
|
423
|
-
Object.keys(userRequestOption.allowInterceptConfig).forEach(
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
keyName
|
|
434
|
-
)
|
|
435
|
-
) {
|
|
436
|
-
Reflect.set(
|
|
437
|
-
requestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
|
|
438
|
-
keyName,
|
|
439
|
-
value
|
|
440
|
-
);
|
|
441
|
-
}
|
|
388
|
+
Object.keys(userRequestOption.allowInterceptConfig).forEach((keyName) => {
|
|
389
|
+
let value = Reflect.get(
|
|
390
|
+
userRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
|
|
391
|
+
keyName
|
|
392
|
+
) as Boolean;
|
|
393
|
+
if (
|
|
394
|
+
typeof value === "boolean" &&
|
|
395
|
+
Reflect.has(requestOption.allowInterceptConfig as HttpxAllowInterceptConfig, keyName)
|
|
396
|
+
) {
|
|
397
|
+
Reflect.set(requestOption.allowInterceptConfig as HttpxAllowInterceptConfig, keyName, value);
|
|
442
398
|
}
|
|
443
|
-
);
|
|
399
|
+
});
|
|
444
400
|
}
|
|
445
401
|
}
|
|
446
402
|
if (typeof this.context.GM_Api.xmlHttpRequest !== "function") {
|
|
@@ -450,15 +406,11 @@ class Httpx {
|
|
|
450
406
|
if (typeof requestOption.headers === "object") {
|
|
451
407
|
if (typeof userRequestOption.headers === "object") {
|
|
452
408
|
Object.keys(userRequestOption.headers).forEach((keyName, index) => {
|
|
453
|
-
if (
|
|
454
|
-
keyName in requestOption.headers &&
|
|
455
|
-
userRequestOption!.headers?.[keyName] == null
|
|
456
|
-
) {
|
|
409
|
+
if (keyName in requestOption.headers && userRequestOption!.headers?.[keyName] == null) {
|
|
457
410
|
/* 在默认的header中存在,且设置它新的值为空,那么就是默认的值 */
|
|
458
411
|
Reflect.deleteProperty(requestOption.headers, keyName);
|
|
459
412
|
} else {
|
|
460
|
-
requestOption.headers[keyName] =
|
|
461
|
-
userRequestOption?.headers?.[keyName];
|
|
413
|
+
requestOption.headers[keyName] = userRequestOption?.headers?.[keyName];
|
|
462
414
|
}
|
|
463
415
|
});
|
|
464
416
|
} else {
|
|
@@ -473,10 +425,7 @@ class Httpx {
|
|
|
473
425
|
/* 使用assign替换且添加 */
|
|
474
426
|
if (typeof userRequestOption.fetchInit === "object") {
|
|
475
427
|
Object.keys(userRequestOption.fetchInit).forEach((keyName, index) => {
|
|
476
|
-
if (
|
|
477
|
-
keyName in requestOption.fetchInit &&
|
|
478
|
-
(userRequestOption as any).fetchInit[keyName] == null
|
|
479
|
-
) {
|
|
428
|
+
if (keyName in requestOption.fetchInit && (userRequestOption as any).fetchInit[keyName] == null) {
|
|
480
429
|
/* 在默认的fetchInit中存在,且设置它新的值为空,那么就是默认的值 */
|
|
481
430
|
Reflect.deleteProperty(requestOption.fetchInit, keyName);
|
|
482
431
|
} else {
|
|
@@ -493,10 +442,7 @@ class Httpx {
|
|
|
493
442
|
}
|
|
494
443
|
|
|
495
444
|
// 处理新的cookiePartition
|
|
496
|
-
if (
|
|
497
|
-
typeof requestOption.cookiePartition === "object" &&
|
|
498
|
-
requestOption.cookiePartition != null
|
|
499
|
-
) {
|
|
445
|
+
if (typeof requestOption.cookiePartition === "object" && requestOption.cookiePartition != null) {
|
|
500
446
|
if (
|
|
501
447
|
Reflect.has(requestOption.cookiePartition, "topLevelSite") &&
|
|
502
448
|
typeof requestOption.cookiePartition.topLevelSite !== "string"
|
|
@@ -518,8 +464,7 @@ class Httpx {
|
|
|
518
464
|
requestOption.url = globalThis.location.origin + requestOption.url;
|
|
519
465
|
} else {
|
|
520
466
|
// 补充origin+/
|
|
521
|
-
requestOption.url =
|
|
522
|
-
globalThis.location.origin + "/" + requestOption.url;
|
|
467
|
+
requestOption.url = globalThis.location.origin + "/" + requestOption.url;
|
|
523
468
|
}
|
|
524
469
|
}
|
|
525
470
|
|
|
@@ -545,8 +490,7 @@ class Httpx {
|
|
|
545
490
|
} else if (typeof requestOption.data === "object") {
|
|
546
491
|
isHandler = true;
|
|
547
492
|
// URLSearchParams参数可以转普通的string:string,包括FormData
|
|
548
|
-
|
|
549
|
-
let searchParams = new URLSearchParams(requestOption.data);
|
|
493
|
+
let searchParams = new URLSearchParams(requestOption.data as Record<string, string>);
|
|
550
494
|
urlSearch = searchParams.toString();
|
|
551
495
|
}
|
|
552
496
|
if (isHandler) {
|
|
@@ -594,14 +538,11 @@ class Httpx {
|
|
|
594
538
|
} else if (typeof requestOption.data === "object") {
|
|
595
539
|
requestOption.data = JSON.stringify(requestOption.data);
|
|
596
540
|
}
|
|
597
|
-
} else if (
|
|
598
|
-
ContentType.includes("application/x-www-form-urlencoded")
|
|
599
|
-
) {
|
|
541
|
+
} else if (ContentType.includes("application/x-www-form-urlencoded")) {
|
|
600
542
|
// application/x-www-form-urlencoded
|
|
601
543
|
if (typeof requestOption.data === "object") {
|
|
602
544
|
requestOption.data = new URLSearchParams(
|
|
603
|
-
|
|
604
|
-
requestOption.data
|
|
545
|
+
requestOption.data as Record<string, string>
|
|
605
546
|
).toString();
|
|
606
547
|
}
|
|
607
548
|
} else if (ContentType.includes("multipart/form-data")) {
|
|
@@ -621,11 +562,9 @@ class Httpx {
|
|
|
621
562
|
},
|
|
622
563
|
/**
|
|
623
564
|
* 处理发送请求的配置,去除值为undefined、空function的值
|
|
624
|
-
* @param option
|
|
565
|
+
* @param option 请求配置
|
|
625
566
|
*/
|
|
626
|
-
removeRequestNullOption(
|
|
627
|
-
option: Required<HttpxRequestOption>
|
|
628
|
-
): HttpxRequestOption {
|
|
567
|
+
removeRequestNullOption(option: Required<HttpxRequestOption>): HttpxRequestOption {
|
|
629
568
|
Object.keys(option).forEach((keyName) => {
|
|
630
569
|
if (
|
|
631
570
|
option[keyName as keyof HttpxRequestOption] == null ||
|
|
@@ -637,23 +576,20 @@ class Httpx {
|
|
|
637
576
|
}
|
|
638
577
|
});
|
|
639
578
|
if (CommonUtil.isNull(option.url)) {
|
|
640
|
-
throw new TypeError(`Utils.Httpx 参数
|
|
579
|
+
throw new TypeError(`Utils.Httpx 参数url不能为空:${option.url}`);
|
|
641
580
|
}
|
|
642
581
|
return option;
|
|
643
582
|
},
|
|
644
583
|
/**
|
|
645
584
|
* 处理fetch的配置
|
|
646
|
-
* @param option
|
|
585
|
+
* @param option 请求配置
|
|
647
586
|
*/
|
|
648
587
|
handleFetchOption(option: Required<HttpxRequestOption>) {
|
|
649
588
|
/**
|
|
650
589
|
* fetch的请求配置
|
|
651
590
|
**/
|
|
652
591
|
let fetchRequestOption = <RequestInit>{};
|
|
653
|
-
if (
|
|
654
|
-
(option.method === "GET" || option.method === "HEAD") &&
|
|
655
|
-
option.data != null
|
|
656
|
-
) {
|
|
592
|
+
if ((option.method === "GET" || option.method === "HEAD") && option.data != null) {
|
|
657
593
|
/* GET 或 HEAD 方法的请求不能包含 body 信息 */
|
|
658
594
|
Reflect.deleteProperty(option, "data");
|
|
659
595
|
}
|
|
@@ -698,13 +634,13 @@ class Httpx {
|
|
|
698
634
|
};
|
|
699
635
|
},
|
|
700
636
|
};
|
|
701
|
-
private
|
|
637
|
+
private HttpxResponseCallBack = {
|
|
702
638
|
context: this,
|
|
703
639
|
/**
|
|
704
640
|
* onabort请求被取消-触发
|
|
705
641
|
* @param details 配置
|
|
706
|
-
* @param resolve 回调
|
|
707
|
-
* @param reject
|
|
642
|
+
* @param resolve promise回调
|
|
643
|
+
* @param reject promise抛出错误回调
|
|
708
644
|
* @param argsResult 返回的参数列表
|
|
709
645
|
*/
|
|
710
646
|
async onAbort(
|
|
@@ -714,10 +650,10 @@ class Httpx {
|
|
|
714
650
|
argsResult: any
|
|
715
651
|
) {
|
|
716
652
|
// console.log(argsResult);
|
|
717
|
-
if (
|
|
653
|
+
if (typeof details?.onabort === "function") {
|
|
718
654
|
details.onabort.apply(this, argsResult);
|
|
719
|
-
} else if (
|
|
720
|
-
this.context.#defaultRequestOption
|
|
655
|
+
} else if (typeof this.context.#defaultRequestOption?.onabort === "function") {
|
|
656
|
+
this.context.#defaultRequestOption.onabort.apply(this, argsResult);
|
|
721
657
|
}
|
|
722
658
|
let response = argsResult;
|
|
723
659
|
if (response.length) {
|
|
@@ -726,12 +662,12 @@ class Httpx {
|
|
|
726
662
|
if (
|
|
727
663
|
(await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
728
664
|
type: "onabort",
|
|
729
|
-
error: new
|
|
665
|
+
error: new Error("request canceled"),
|
|
730
666
|
response: null,
|
|
731
667
|
details: details,
|
|
732
668
|
})) == null
|
|
733
669
|
) {
|
|
734
|
-
// reject(new
|
|
670
|
+
// reject(new Error("response is intercept with onabort"));
|
|
735
671
|
return;
|
|
736
672
|
}
|
|
737
673
|
resolve({
|
|
@@ -744,66 +680,70 @@ class Httpx {
|
|
|
744
680
|
});
|
|
745
681
|
},
|
|
746
682
|
/**
|
|
747
|
-
*
|
|
683
|
+
* ontimeout请求超时-触发
|
|
748
684
|
* @param details 配置
|
|
749
685
|
* @param resolve 回调
|
|
750
686
|
* @param reject 抛出错误
|
|
751
687
|
* @param argsResult 返回的参数列表
|
|
752
688
|
*/
|
|
753
|
-
async
|
|
689
|
+
async onTimeout(
|
|
754
690
|
details: Required<HttpxRequestOption>,
|
|
755
691
|
resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
|
|
756
692
|
reject: (...args: any[]) => void,
|
|
757
693
|
argsResult: any
|
|
758
694
|
) {
|
|
759
695
|
// console.log(argsResult);
|
|
760
|
-
if ("
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
696
|
+
if (typeof details?.ontimeout === "function") {
|
|
697
|
+
// 执行配置中的ontime回调
|
|
698
|
+
details.ontimeout.apply(this, argsResult);
|
|
699
|
+
} else if (typeof this.context.#defaultRequestOption?.ontimeout === "function") {
|
|
700
|
+
// 执行默认配置的ontime回调
|
|
701
|
+
this.context.#defaultRequestOption.ontimeout.apply(this, argsResult);
|
|
764
702
|
}
|
|
703
|
+
// 获取响应结果
|
|
765
704
|
let response = argsResult;
|
|
766
705
|
if (response.length) {
|
|
767
706
|
response = response[0];
|
|
768
707
|
}
|
|
708
|
+
// 执行错误回调的钩子
|
|
769
709
|
if (
|
|
770
710
|
(await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
771
|
-
type: "
|
|
772
|
-
error: new
|
|
711
|
+
type: "ontimeout",
|
|
712
|
+
error: new Error("request timeout"),
|
|
773
713
|
response: response,
|
|
774
714
|
details: details,
|
|
775
715
|
})) == null
|
|
776
716
|
) {
|
|
777
|
-
// reject(new
|
|
717
|
+
// reject(new Error("response is intercept with ontimeout"));
|
|
778
718
|
return;
|
|
779
719
|
}
|
|
780
720
|
resolve({
|
|
781
721
|
data: response,
|
|
782
722
|
details: details,
|
|
783
|
-
msg: "
|
|
723
|
+
msg: "请求超时",
|
|
784
724
|
status: false,
|
|
785
|
-
statusCode:
|
|
786
|
-
type: "
|
|
725
|
+
statusCode: 0,
|
|
726
|
+
type: "ontimeout",
|
|
787
727
|
});
|
|
788
728
|
},
|
|
789
729
|
/**
|
|
790
|
-
*
|
|
730
|
+
* onerror请求异常-触发
|
|
791
731
|
* @param details 配置
|
|
792
732
|
* @param resolve 回调
|
|
793
733
|
* @param reject 抛出错误
|
|
794
734
|
* @param argsResult 返回的参数列表
|
|
795
735
|
*/
|
|
796
|
-
async
|
|
736
|
+
async onError(
|
|
797
737
|
details: Required<HttpxRequestOption>,
|
|
798
738
|
resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
|
|
799
739
|
reject: (...args: any[]) => void,
|
|
800
740
|
argsResult: any
|
|
801
741
|
) {
|
|
802
742
|
// console.log(argsResult);
|
|
803
|
-
if ("
|
|
804
|
-
details.
|
|
805
|
-
} else if (
|
|
806
|
-
this.context.#defaultRequestOption
|
|
743
|
+
if (typeof details?.onerror === "function") {
|
|
744
|
+
details.onerror.apply(this, argsResult);
|
|
745
|
+
} else if (typeof this.context.#defaultRequestOption?.onerror === "function") {
|
|
746
|
+
this.context.#defaultRequestOption.onerror.apply(this, argsResult);
|
|
807
747
|
}
|
|
808
748
|
let response = argsResult;
|
|
809
749
|
if (response.length) {
|
|
@@ -811,41 +751,24 @@ class Httpx {
|
|
|
811
751
|
}
|
|
812
752
|
if (
|
|
813
753
|
(await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
814
|
-
type: "
|
|
815
|
-
error: new
|
|
816
|
-
response:
|
|
754
|
+
type: "onerror",
|
|
755
|
+
error: new Error("request error"),
|
|
756
|
+
response: response,
|
|
817
757
|
details: details,
|
|
818
758
|
})) == null
|
|
819
759
|
) {
|
|
820
|
-
// reject(new
|
|
760
|
+
// reject(new Error("response is intercept with onerror"));
|
|
821
761
|
return;
|
|
822
762
|
}
|
|
823
763
|
resolve({
|
|
824
764
|
data: response,
|
|
825
765
|
details: details,
|
|
826
|
-
msg: "
|
|
766
|
+
msg: "请求异常",
|
|
827
767
|
status: false,
|
|
828
|
-
statusCode:
|
|
829
|
-
type: "
|
|
768
|
+
statusCode: response["status"],
|
|
769
|
+
type: "onerror",
|
|
830
770
|
});
|
|
831
771
|
},
|
|
832
|
-
|
|
833
|
-
/**
|
|
834
|
-
* onloadstart请求开始-触发
|
|
835
|
-
* @param details 配置
|
|
836
|
-
* @param argsResult 返回的参数列表
|
|
837
|
-
*/
|
|
838
|
-
onLoadStart(details: Required<HttpxRequestOption>, argsResult: any[]) {
|
|
839
|
-
// console.log(argsResult);
|
|
840
|
-
if ("onloadstart" in details) {
|
|
841
|
-
details.onloadstart.apply(this, argsResult);
|
|
842
|
-
} else if ("onloadstart" in this.context.#defaultRequestOption) {
|
|
843
|
-
this.context.#defaultRequestOption!.onloadstart!.apply(
|
|
844
|
-
this,
|
|
845
|
-
argsResult
|
|
846
|
-
);
|
|
847
|
-
}
|
|
848
|
-
},
|
|
849
772
|
/**
|
|
850
773
|
* onload加载完毕-触发
|
|
851
774
|
* @param details 请求的配置
|
|
@@ -869,9 +792,7 @@ class Httpx {
|
|
|
869
792
|
) {
|
|
870
793
|
if (typeof originResponse["response"] === "object") {
|
|
871
794
|
TryCatch().run(() => {
|
|
872
|
-
originResponse["responseText"] = JSON.stringify(
|
|
873
|
-
originResponse["response"]
|
|
874
|
-
);
|
|
795
|
+
originResponse["responseText"] = JSON.stringify(originResponse["response"]);
|
|
875
796
|
});
|
|
876
797
|
} else {
|
|
877
798
|
originResponse["responseText"] = originResponse["response"] as string;
|
|
@@ -892,10 +813,7 @@ class Httpx {
|
|
|
892
813
|
httpxResponse = CommonUtil.toJSON(httpxResponseText);
|
|
893
814
|
} else if (details.responseType === "document") {
|
|
894
815
|
let parser = new DOMParser();
|
|
895
|
-
httpxResponse = parser.parseFromString(
|
|
896
|
-
httpxResponseText,
|
|
897
|
-
"text/html"
|
|
898
|
-
);
|
|
816
|
+
httpxResponse = parser.parseFromString(httpxResponseText, "text/html");
|
|
899
817
|
} else if (details.responseType === "arraybuffer") {
|
|
900
818
|
let encoder = new TextEncoder();
|
|
901
819
|
let arrayBuffer = encoder.encode(httpxResponseText);
|
|
@@ -907,33 +825,21 @@ class Httpx {
|
|
|
907
825
|
}
|
|
908
826
|
// 尝试覆盖原response
|
|
909
827
|
try {
|
|
910
|
-
let setStatus = Reflect.set(
|
|
911
|
-
originResponse,
|
|
912
|
-
"response",
|
|
913
|
-
httpxResponse
|
|
914
|
-
);
|
|
828
|
+
let setStatus = Reflect.set(originResponse, "response", httpxResponse);
|
|
915
829
|
if (!setStatus) {
|
|
916
|
-
console.warn(
|
|
917
|
-
"[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse"
|
|
918
|
-
);
|
|
830
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse");
|
|
919
831
|
try {
|
|
920
832
|
Reflect.set(originResponse, "httpxResponse", httpxResponse);
|
|
921
833
|
} catch (error) {
|
|
922
|
-
console.warn(
|
|
923
|
-
"[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖"
|
|
924
|
-
);
|
|
834
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
|
|
925
835
|
}
|
|
926
836
|
}
|
|
927
837
|
} catch (error) {
|
|
928
|
-
console.warn(
|
|
929
|
-
"[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse"
|
|
930
|
-
);
|
|
838
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse");
|
|
931
839
|
try {
|
|
932
840
|
Reflect.set(originResponse, "httpxResponse", httpxResponse);
|
|
933
841
|
} catch (error) {
|
|
934
|
-
console.warn(
|
|
935
|
-
"[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖"
|
|
936
|
-
);
|
|
842
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
|
|
937
843
|
}
|
|
938
844
|
}
|
|
939
845
|
}
|
|
@@ -945,13 +851,8 @@ class Httpx {
|
|
|
945
851
|
|
|
946
852
|
/* 状态码2xx都是成功的 */
|
|
947
853
|
if (Math.floor(originResponse.status / 100) === 2) {
|
|
948
|
-
if (
|
|
949
|
-
(
|
|
950
|
-
originResponse,
|
|
951
|
-
details
|
|
952
|
-
)) == null
|
|
953
|
-
) {
|
|
954
|
-
// reject(new TypeError("response is intercept with onloada"));
|
|
854
|
+
if ((await this.context.HttpxResponseHook.successResponseCallBack(originResponse, details)) == null) {
|
|
855
|
+
// reject(new Error("response is intercept with onloada"));
|
|
955
856
|
return;
|
|
956
857
|
}
|
|
957
858
|
resolve({
|
|
@@ -963,25 +864,20 @@ class Httpx {
|
|
|
963
864
|
type: "onload",
|
|
964
865
|
});
|
|
965
866
|
} else {
|
|
966
|
-
this.context.
|
|
967
|
-
details,
|
|
968
|
-
resolve,
|
|
969
|
-
reject,
|
|
970
|
-
argsResult
|
|
971
|
-
);
|
|
867
|
+
this.context.HttpxResponseCallBack.onError(details, resolve, reject, argsResult);
|
|
972
868
|
}
|
|
973
869
|
},
|
|
974
870
|
/**
|
|
975
|
-
*
|
|
871
|
+
* onloadstart请求开始-触发
|
|
976
872
|
* @param details 配置
|
|
977
873
|
* @param argsResult 返回的参数列表
|
|
978
874
|
*/
|
|
979
|
-
|
|
875
|
+
onLoadStart(details: Required<HttpxRequestOption>, argsResult: any[]) {
|
|
980
876
|
// console.log(argsResult);
|
|
981
|
-
if ("
|
|
982
|
-
details.
|
|
983
|
-
} else if (
|
|
984
|
-
this.context.#defaultRequestOption
|
|
877
|
+
if (typeof details?.onloadstart === "function") {
|
|
878
|
+
details.onloadstart.apply(this, argsResult);
|
|
879
|
+
} else if (typeof this.context.#defaultRequestOption?.onloadstart === "function") {
|
|
880
|
+
this.context.#defaultRequestOption.onloadstart.apply(this, argsResult);
|
|
985
881
|
}
|
|
986
882
|
},
|
|
987
883
|
/**
|
|
@@ -989,18 +885,25 @@ class Httpx {
|
|
|
989
885
|
* @param details 配置
|
|
990
886
|
* @param argsResult 返回的参数列表
|
|
991
887
|
*/
|
|
992
|
-
onReadyStateChange(
|
|
993
|
-
details: Required<HttpxRequestOption>,
|
|
994
|
-
argsResult: any[]
|
|
995
|
-
) {
|
|
888
|
+
onReadyStateChange(details: Required<HttpxRequestOption>, argsResult: any[]) {
|
|
996
889
|
// console.log(argsResult);
|
|
997
|
-
if (
|
|
890
|
+
if (typeof details?.onreadystatechange === "function") {
|
|
998
891
|
details.onreadystatechange.apply(this, argsResult);
|
|
999
|
-
} else if (
|
|
1000
|
-
this.context.#defaultRequestOption
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
892
|
+
} else if (typeof this.context.#defaultRequestOption?.onreadystatechange === "function") {
|
|
893
|
+
this.context.#defaultRequestOption.onreadystatechange.apply(this, argsResult);
|
|
894
|
+
}
|
|
895
|
+
},
|
|
896
|
+
/**
|
|
897
|
+
* onprogress上传进度-触发
|
|
898
|
+
* @param details 配置
|
|
899
|
+
* @param argsResult 返回的参数列表
|
|
900
|
+
*/
|
|
901
|
+
onProgress(details: Required<HttpxRequestOption>, argsResult: any[]) {
|
|
902
|
+
// console.log(argsResult);
|
|
903
|
+
if (typeof details?.onprogress === "function") {
|
|
904
|
+
details.onprogress.apply(this, argsResult);
|
|
905
|
+
} else if (typeof this.context.#defaultRequestOption?.onprogress === "function") {
|
|
906
|
+
this.context.#defaultRequestOption.onprogress.apply(this, argsResult);
|
|
1004
907
|
}
|
|
1005
908
|
},
|
|
1006
909
|
};
|
|
@@ -1014,12 +917,8 @@ class Httpx {
|
|
|
1014
917
|
if (this.context.#defaultInitOption.logDetails) {
|
|
1015
918
|
console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
|
|
1016
919
|
}
|
|
1017
|
-
if (
|
|
1018
|
-
|
|
1019
|
-
"function"
|
|
1020
|
-
) {
|
|
1021
|
-
let hookResult =
|
|
1022
|
-
await this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
920
|
+
if (typeof this.context.HttpxRequestHook.beforeRequestCallBack === "function") {
|
|
921
|
+
let hookResult = await this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
1023
922
|
if (hookResult == null) {
|
|
1024
923
|
return;
|
|
1025
924
|
}
|
|
@@ -1064,15 +963,12 @@ class Httpx {
|
|
|
1064
963
|
isFetch: true,
|
|
1065
964
|
finalUrl: fetchResponse.url,
|
|
1066
965
|
readyState: 4,
|
|
1067
|
-
// @ts-ignore
|
|
1068
966
|
status: fetchResponse.status,
|
|
1069
967
|
statusText: fetchResponse.statusText,
|
|
1070
|
-
|
|
1071
|
-
response: void 0,
|
|
968
|
+
response: "",
|
|
1072
969
|
responseFetchHeaders: fetchResponse.headers,
|
|
1073
970
|
responseHeaders: "",
|
|
1074
|
-
|
|
1075
|
-
responseText: void 0,
|
|
971
|
+
responseText: "",
|
|
1076
972
|
responseType: option.responseType,
|
|
1077
973
|
responseXML: void 0,
|
|
1078
974
|
};
|
|
@@ -1090,9 +986,7 @@ class Httpx {
|
|
|
1090
986
|
if (
|
|
1091
987
|
option.responseType === "stream" ||
|
|
1092
988
|
(fetchResponse.headers.has("Content-Type") &&
|
|
1093
|
-
fetchResponse.headers
|
|
1094
|
-
.get("Content-Type")!
|
|
1095
|
-
.includes("text/event-stream"))
|
|
989
|
+
fetchResponse.headers.get("Content-Type")!.includes("text/event-stream"))
|
|
1096
990
|
) {
|
|
1097
991
|
Reflect.set(httpxResponse, "isStream", true);
|
|
1098
992
|
Reflect.set(httpxResponse, "response", fetchResponse.body);
|
|
@@ -1105,7 +999,7 @@ class Httpx {
|
|
|
1105
999
|
/** 响应 */
|
|
1106
1000
|
let response: any = "";
|
|
1107
1001
|
/** 响应字符串 */
|
|
1108
|
-
let responseText = "";
|
|
1002
|
+
let responseText: string = "";
|
|
1109
1003
|
/** 响应xml文档 */
|
|
1110
1004
|
let responseXML: XMLDocument | string = "";
|
|
1111
1005
|
/** 先获取二进制数据 */
|
|
@@ -1114,9 +1008,7 @@ class Httpx {
|
|
|
1114
1008
|
/** 数据编码 */
|
|
1115
1009
|
let encoding = "utf-8";
|
|
1116
1010
|
if (fetchResponse.headers.has("Content-Type")) {
|
|
1117
|
-
let charsetMatched = fetchResponse.headers
|
|
1118
|
-
.get("Content-Type")
|
|
1119
|
-
?.match(/charset=(.+)/);
|
|
1011
|
+
let charsetMatched = fetchResponse.headers.get("Content-Type")?.match(/charset=(.+)/);
|
|
1120
1012
|
if (charsetMatched) {
|
|
1121
1013
|
encoding = charsetMatched[1];
|
|
1122
1014
|
encoding = encoding.toLowerCase();
|
|
@@ -1138,15 +1030,11 @@ class Httpx {
|
|
|
1138
1030
|
response = new Blob([arrayBuffer]);
|
|
1139
1031
|
} else if (
|
|
1140
1032
|
option.responseType === "json" ||
|
|
1141
|
-
(typeof fetchResponseType === "string" &&
|
|
1142
|
-
fetchResponseType.includes("application/json"))
|
|
1033
|
+
(typeof fetchResponseType === "string" && fetchResponseType.includes("application/json"))
|
|
1143
1034
|
) {
|
|
1144
1035
|
// response返回格式是JSON格式
|
|
1145
1036
|
response = CommonUtil.toJSON(responseText);
|
|
1146
|
-
} else if (
|
|
1147
|
-
option.responseType === "document" ||
|
|
1148
|
-
option.responseType == null
|
|
1149
|
-
) {
|
|
1037
|
+
} else if (option.responseType === "document" || option.responseType == null) {
|
|
1150
1038
|
// response返回格式是文档格式
|
|
1151
1039
|
let parser = new DOMParser();
|
|
1152
1040
|
response = parser.parseFromString(responseText, "text/html");
|
|
@@ -1155,9 +1043,9 @@ class Httpx {
|
|
|
1155
1043
|
let parser = new DOMParser();
|
|
1156
1044
|
responseXML = parser.parseFromString(responseText, "text/xml");
|
|
1157
1045
|
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1046
|
+
httpxResponse.response = response;
|
|
1047
|
+
httpxResponse.responseText = responseText;
|
|
1048
|
+
httpxResponse.responseXML = responseXML;
|
|
1161
1049
|
|
|
1162
1050
|
// 执行回调
|
|
1163
1051
|
option.onload(httpxResponse);
|
|
@@ -1197,7 +1085,7 @@ class Httpx {
|
|
|
1197
1085
|
* 默认配置
|
|
1198
1086
|
*/
|
|
1199
1087
|
#defaultRequestOption = <HttpxRequestOption>{
|
|
1200
|
-
url: void 0,
|
|
1088
|
+
url: void 0 as undefined | string,
|
|
1201
1089
|
timeout: 5000,
|
|
1202
1090
|
async: false,
|
|
1203
1091
|
responseType: void 0,
|
|
@@ -1261,14 +1149,8 @@ class Httpx {
|
|
|
1261
1149
|
if (typeof option.xmlHttpRequest === "function") {
|
|
1262
1150
|
this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
|
|
1263
1151
|
}
|
|
1264
|
-
this.#defaultRequestOption = CommonUtil.assign(
|
|
1265
|
-
|
|
1266
|
-
option
|
|
1267
|
-
);
|
|
1268
|
-
this.#defaultInitOption = CommonUtil.assign(
|
|
1269
|
-
this.#defaultInitOption,
|
|
1270
|
-
option
|
|
1271
|
-
);
|
|
1152
|
+
this.#defaultRequestOption = CommonUtil.assign(this.#defaultRequestOption, option);
|
|
1153
|
+
this.#defaultInitOption = CommonUtil.assign(this.#defaultInitOption, option);
|
|
1272
1154
|
}
|
|
1273
1155
|
/**
|
|
1274
1156
|
* 拦截器
|
|
@@ -1283,11 +1165,7 @@ class Httpx {
|
|
|
1283
1165
|
* 添加拦截器
|
|
1284
1166
|
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
1285
1167
|
*/
|
|
1286
|
-
use(
|
|
1287
|
-
fn: <T extends Required<HttpxRequestOption>>(
|
|
1288
|
-
details: T
|
|
1289
|
-
) => void | T | Promise<void | T>
|
|
1290
|
-
) {
|
|
1168
|
+
use(fn: <T extends Required<HttpxRequestOption>>(details: T) => void | T | Promise<void | T>) {
|
|
1291
1169
|
if (typeof fn !== "function") {
|
|
1292
1170
|
console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
|
|
1293
1171
|
return;
|
|
@@ -1325,9 +1203,7 @@ class Httpx {
|
|
|
1325
1203
|
response: T,
|
|
1326
1204
|
details: HttpxRequestOption
|
|
1327
1205
|
) => void | T,
|
|
1328
|
-
errorFn?: <T extends HttpxHookErrorData>(
|
|
1329
|
-
data: T
|
|
1330
|
-
) => void | T | Promise<void | T>
|
|
1206
|
+
errorFn?: <T extends HttpxHookErrorData>(data: T) => void | T | Promise<void | T>
|
|
1331
1207
|
) {
|
|
1332
1208
|
if (typeof successFn !== "function" && typeof errorFn !== "function") {
|
|
1333
1209
|
console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
|
|
@@ -1357,13 +1233,6 @@ class Httpx {
|
|
|
1357
1233
|
setXMLHttpRequest(httpRequest: Function) {
|
|
1358
1234
|
this.GM_Api.xmlHttpRequest = httpRequest;
|
|
1359
1235
|
}
|
|
1360
|
-
/**
|
|
1361
|
-
* GET 请求
|
|
1362
|
-
* @param url 网址
|
|
1363
|
-
*/
|
|
1364
|
-
get<T extends HttpxRequestOption>(
|
|
1365
|
-
url: string
|
|
1366
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1367
1236
|
/**
|
|
1368
1237
|
* GET 请求
|
|
1369
1238
|
* @param details 配置
|
|
@@ -1371,23 +1240,29 @@ class Httpx {
|
|
|
1371
1240
|
get<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
|
|
1372
1241
|
/**
|
|
1373
1242
|
* GET 请求
|
|
1374
|
-
* @param url
|
|
1243
|
+
* @param url 请求的url
|
|
1375
1244
|
* @param details 配置
|
|
1376
1245
|
*/
|
|
1377
|
-
get<T extends HttpxRequestOption
|
|
1246
|
+
get<T extends Omit<HttpxRequestOption, "url">>(
|
|
1378
1247
|
url: string,
|
|
1379
|
-
details
|
|
1380
|
-
): HttpxPromise<
|
|
1248
|
+
details?: T
|
|
1249
|
+
): HttpxPromise<
|
|
1250
|
+
HttpxResponse<
|
|
1251
|
+
T & {
|
|
1252
|
+
/**
|
|
1253
|
+
* 请求的url
|
|
1254
|
+
*/
|
|
1255
|
+
url: string;
|
|
1256
|
+
}
|
|
1257
|
+
>
|
|
1258
|
+
>;
|
|
1381
1259
|
/**
|
|
1382
1260
|
* GET 请求
|
|
1383
|
-
* @param url
|
|
1261
|
+
* @param url 请求的url
|
|
1384
1262
|
* @param details 配置
|
|
1385
1263
|
*/
|
|
1386
|
-
get(
|
|
1387
|
-
|
|
1388
|
-
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1389
|
-
let useRequestOption =
|
|
1390
|
-
this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1264
|
+
get(...args: (string | HttpxRequestOption)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1265
|
+
let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1391
1266
|
useRequestOption.method = "GET";
|
|
1392
1267
|
return this.request(useRequestOption, (option) => {
|
|
1393
1268
|
Reflect.deleteProperty(option, "onprogress");
|
|
@@ -1397,33 +1272,30 @@ class Httpx {
|
|
|
1397
1272
|
* POST 请求
|
|
1398
1273
|
* @param details 配置
|
|
1399
1274
|
*/
|
|
1400
|
-
post<T extends HttpxRequestOption>(
|
|
1401
|
-
details: T
|
|
1402
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1403
|
-
/**
|
|
1404
|
-
* POST 请求
|
|
1405
|
-
* @param url 网址
|
|
1406
|
-
*/
|
|
1407
|
-
post<T extends HttpxRequestOption>(
|
|
1408
|
-
url: string
|
|
1409
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1275
|
+
post<T extends HttpxRequestOption>(details?: T): HttpxPromise<HttpxResponse<T>>;
|
|
1410
1276
|
/**
|
|
1411
1277
|
* POST 请求
|
|
1412
|
-
* @param url
|
|
1278
|
+
* @param url 请求的url
|
|
1413
1279
|
* @param details 配置
|
|
1414
1280
|
*/
|
|
1415
|
-
post<T extends HttpxRequestOption
|
|
1281
|
+
post<T extends Omit<HttpxRequestOption, "url">>(
|
|
1416
1282
|
url: string,
|
|
1417
|
-
details
|
|
1418
|
-
): HttpxPromise<
|
|
1283
|
+
details?: T
|
|
1284
|
+
): HttpxPromise<
|
|
1285
|
+
HttpxResponse<
|
|
1286
|
+
T & {
|
|
1287
|
+
/**
|
|
1288
|
+
* 请求的url
|
|
1289
|
+
*/
|
|
1290
|
+
url: string;
|
|
1291
|
+
}
|
|
1292
|
+
>
|
|
1293
|
+
>;
|
|
1419
1294
|
/**
|
|
1420
1295
|
* POST 请求
|
|
1421
1296
|
*/
|
|
1422
|
-
post(
|
|
1423
|
-
|
|
1424
|
-
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1425
|
-
let useRequestOption =
|
|
1426
|
-
this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1297
|
+
post(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1298
|
+
let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1427
1299
|
useRequestOption.method = "POST";
|
|
1428
1300
|
return this.request(useRequestOption);
|
|
1429
1301
|
}
|
|
@@ -1431,33 +1303,30 @@ class Httpx {
|
|
|
1431
1303
|
* HEAD 请求
|
|
1432
1304
|
* @param details 配置
|
|
1433
1305
|
*/
|
|
1434
|
-
head<T extends HttpxRequestOption>(
|
|
1435
|
-
details: T
|
|
1436
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1437
|
-
/**
|
|
1438
|
-
* HEAD 请求
|
|
1439
|
-
* @param url 网址
|
|
1440
|
-
*/
|
|
1441
|
-
head<T extends HttpxRequestOption>(
|
|
1442
|
-
url: string
|
|
1443
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1306
|
+
head<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
|
|
1444
1307
|
/**
|
|
1445
1308
|
* HEAD 请求
|
|
1446
|
-
* @param url
|
|
1309
|
+
* @param url 请求的url
|
|
1447
1310
|
* @param details 配置
|
|
1448
1311
|
*/
|
|
1449
|
-
head<T extends HttpxRequestOption
|
|
1312
|
+
head<T extends Omit<HttpxRequestOption, "url">>(
|
|
1450
1313
|
url: string,
|
|
1451
|
-
details
|
|
1452
|
-
): HttpxPromise<
|
|
1314
|
+
details?: T
|
|
1315
|
+
): HttpxPromise<
|
|
1316
|
+
HttpxResponse<
|
|
1317
|
+
T & {
|
|
1318
|
+
/**
|
|
1319
|
+
* 请求的url
|
|
1320
|
+
*/
|
|
1321
|
+
url: string;
|
|
1322
|
+
}
|
|
1323
|
+
>
|
|
1324
|
+
>;
|
|
1453
1325
|
/**
|
|
1454
1326
|
* HEAD 请求
|
|
1455
1327
|
*/
|
|
1456
|
-
head(
|
|
1457
|
-
|
|
1458
|
-
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1459
|
-
let useRequestOption =
|
|
1460
|
-
this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1328
|
+
head(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1329
|
+
let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1461
1330
|
useRequestOption.method = "HEAD";
|
|
1462
1331
|
return this.request(useRequestOption, (option) => {
|
|
1463
1332
|
Reflect.deleteProperty(option, "onprogress");
|
|
@@ -1467,76 +1336,68 @@ class Httpx {
|
|
|
1467
1336
|
* OPTIONS 请求
|
|
1468
1337
|
* @param details 配置
|
|
1469
1338
|
*/
|
|
1470
|
-
options<T extends HttpxRequestOption>(
|
|
1471
|
-
details: T
|
|
1472
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1473
|
-
/**
|
|
1474
|
-
* OPTIONS 请求
|
|
1475
|
-
* @param url 网址
|
|
1476
|
-
*/
|
|
1477
|
-
options<T extends HttpxRequestOption>(
|
|
1478
|
-
url: string
|
|
1479
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1339
|
+
options<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
|
|
1480
1340
|
/**
|
|
1481
1341
|
* OPTIONS 请求
|
|
1482
|
-
* @param url
|
|
1342
|
+
* @param url 请求的url
|
|
1483
1343
|
* @param details 配置
|
|
1484
1344
|
*/
|
|
1485
|
-
options<T extends HttpxRequestOption
|
|
1345
|
+
options<T extends Omit<HttpxRequestOption, "url">>(
|
|
1486
1346
|
url: string,
|
|
1487
|
-
details
|
|
1488
|
-
): HttpxPromise<
|
|
1347
|
+
details?: T
|
|
1348
|
+
): HttpxPromise<
|
|
1349
|
+
HttpxResponse<
|
|
1350
|
+
T & {
|
|
1351
|
+
/**
|
|
1352
|
+
* 请求的url
|
|
1353
|
+
*/
|
|
1354
|
+
url: string;
|
|
1355
|
+
}
|
|
1356
|
+
>
|
|
1357
|
+
>;
|
|
1489
1358
|
/**
|
|
1490
1359
|
* OPTIONS 请求
|
|
1491
1360
|
*/
|
|
1492
|
-
options(
|
|
1493
|
-
|
|
1494
|
-
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1495
|
-
let useRequestOption =
|
|
1496
|
-
this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1361
|
+
options(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1362
|
+
let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1497
1363
|
useRequestOption.method = "OPTIONS";
|
|
1498
1364
|
return this.request(useRequestOption, (option) => {
|
|
1499
1365
|
Reflect.deleteProperty(option, "onprogress");
|
|
1500
1366
|
});
|
|
1501
1367
|
}
|
|
1502
|
-
|
|
1503
1368
|
/**
|
|
1504
1369
|
* DELETE 请求
|
|
1505
1370
|
* @param details 配置
|
|
1506
1371
|
*/
|
|
1507
|
-
delete<T extends HttpxRequestOption>(
|
|
1508
|
-
details: T
|
|
1509
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1372
|
+
delete<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
|
|
1510
1373
|
/**
|
|
1511
1374
|
* DELETE 请求
|
|
1512
|
-
* @param url
|
|
1513
|
-
*/
|
|
1514
|
-
delete<T extends HttpxRequestOption>(
|
|
1515
|
-
url: string
|
|
1516
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1517
|
-
/**
|
|
1518
|
-
* DELETE 请求
|
|
1519
|
-
* @param url 网址
|
|
1375
|
+
* @param url 请求的url
|
|
1520
1376
|
* @param details 配置
|
|
1521
1377
|
*/
|
|
1522
|
-
delete<T extends HttpxRequestOption
|
|
1378
|
+
delete<T extends Omit<HttpxRequestOption, "url">>(
|
|
1523
1379
|
url: string,
|
|
1524
|
-
details
|
|
1525
|
-
): HttpxPromise<
|
|
1380
|
+
details?: T
|
|
1381
|
+
): HttpxPromise<
|
|
1382
|
+
HttpxResponse<
|
|
1383
|
+
T & {
|
|
1384
|
+
/**
|
|
1385
|
+
* 请求的url
|
|
1386
|
+
*/
|
|
1387
|
+
url: string;
|
|
1388
|
+
}
|
|
1389
|
+
>
|
|
1390
|
+
>;
|
|
1526
1391
|
/**
|
|
1527
1392
|
* DELETE 请求
|
|
1528
1393
|
*/
|
|
1529
|
-
delete(
|
|
1530
|
-
|
|
1531
|
-
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1532
|
-
let useRequestOption =
|
|
1533
|
-
this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1394
|
+
delete(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1395
|
+
let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1534
1396
|
useRequestOption.method = "DELETE";
|
|
1535
1397
|
return this.request(useRequestOption, (option) => {
|
|
1536
1398
|
Reflect.deleteProperty(option, "onprogress");
|
|
1537
1399
|
});
|
|
1538
1400
|
}
|
|
1539
|
-
|
|
1540
1401
|
/**
|
|
1541
1402
|
* PUT 请求
|
|
1542
1403
|
* @param details 配置
|
|
@@ -1544,32 +1405,30 @@ class Httpx {
|
|
|
1544
1405
|
put<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
|
|
1545
1406
|
/**
|
|
1546
1407
|
* PUT 请求
|
|
1547
|
-
* @param url
|
|
1548
|
-
*/
|
|
1549
|
-
put<T extends HttpxRequestOption>(
|
|
1550
|
-
url: string
|
|
1551
|
-
): HttpxPromise<HttpxResponse<T>>;
|
|
1552
|
-
/**
|
|
1553
|
-
* PUT 请求
|
|
1554
|
-
* @param url 网址
|
|
1408
|
+
* @param url 请求的url
|
|
1555
1409
|
* @param details 配置
|
|
1556
1410
|
*/
|
|
1557
|
-
put<T extends HttpxRequestOption
|
|
1411
|
+
put<T extends Omit<HttpxRequestOption, "url">>(
|
|
1558
1412
|
url: string,
|
|
1559
|
-
details
|
|
1560
|
-
): HttpxPromise<
|
|
1413
|
+
details?: T
|
|
1414
|
+
): HttpxPromise<
|
|
1415
|
+
HttpxResponse<
|
|
1416
|
+
T & {
|
|
1417
|
+
/**
|
|
1418
|
+
* 请求的url
|
|
1419
|
+
*/
|
|
1420
|
+
url: string;
|
|
1421
|
+
}
|
|
1422
|
+
>
|
|
1423
|
+
>;
|
|
1561
1424
|
/**
|
|
1562
1425
|
* PUT 请求
|
|
1563
1426
|
*/
|
|
1564
|
-
put(
|
|
1565
|
-
|
|
1566
|
-
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1567
|
-
let userRequestOption =
|
|
1568
|
-
this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1427
|
+
put(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1428
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
|
|
1569
1429
|
userRequestOption.method = "PUT";
|
|
1570
1430
|
return this.request(userRequestOption);
|
|
1571
1431
|
}
|
|
1572
|
-
|
|
1573
1432
|
/**
|
|
1574
1433
|
* 发送请求
|
|
1575
1434
|
* @param details 配置
|
|
@@ -1579,43 +1438,29 @@ class Httpx {
|
|
|
1579
1438
|
details: T,
|
|
1580
1439
|
beforeRequestOption?: (option: Required<T>) => void
|
|
1581
1440
|
): HttpxPromise<HttpxResponse<T>> {
|
|
1582
|
-
let useRequestOption =
|
|
1583
|
-
this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
|
|
1441
|
+
let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
|
|
1584
1442
|
/** 取消请求 */
|
|
1585
1443
|
let abortFn: Function | null = null;
|
|
1586
|
-
let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
requestOption =
|
|
1600
|
-
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
1601
|
-
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
1602
|
-
if (
|
|
1603
|
-
requestResult != null &&
|
|
1604
|
-
typeof requestResult.abort === "function"
|
|
1605
|
-
) {
|
|
1606
|
-
abortFn = requestResult.abort;
|
|
1607
|
-
}
|
|
1444
|
+
let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(async (resolve, reject) => {
|
|
1445
|
+
let requestOption = <Required<T>>(
|
|
1446
|
+
this.HttpxRequestOption.getRequestOption(useRequestOption.method!, useRequestOption, resolve, reject)
|
|
1447
|
+
);
|
|
1448
|
+
if (typeof beforeRequestOption === "function") {
|
|
1449
|
+
beforeRequestOption(requestOption);
|
|
1450
|
+
}
|
|
1451
|
+
requestOption = this.HttpxRequestOption.removeRequestNullOption(
|
|
1452
|
+
<Required<HttpxRequestOption>>requestOption
|
|
1453
|
+
) as Required<T>;
|
|
1454
|
+
const requestResult = await this.HttpxRequest.request(<Required<HttpxRequestOption>>requestOption);
|
|
1455
|
+
if (requestResult != null && typeof requestResult.abort === "function") {
|
|
1456
|
+
abortFn = requestResult.abort;
|
|
1608
1457
|
}
|
|
1609
|
-
)
|
|
1610
|
-
// @ts-ignore
|
|
1458
|
+
}) as HttpxPromise<HttpxResponse<T>>;
|
|
1611
1459
|
promise.abort = () => {
|
|
1612
1460
|
if (typeof abortFn === "function") {
|
|
1613
1461
|
abortFn();
|
|
1614
1462
|
}
|
|
1615
1463
|
};
|
|
1616
|
-
// @ts-ignore
|
|
1617
1464
|
return promise;
|
|
1618
1465
|
}
|
|
1619
1466
|
}
|
|
1620
|
-
|
|
1621
|
-
export { Httpx };
|