@whitesev/utils 2.4.2 → 2.4.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/LICENSE +674 -0
- package/dist/index.amd.js +66 -58
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +66 -58
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +66 -58
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +66 -58
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +66 -58
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +66 -58
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Httpx.d.ts +3 -3
- package/package.json +1 -1
- package/src/Httpx.ts +91 -83
- package/src/Utils.ts +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1537,7 +1537,7 @@ class Httpx {
|
|
|
1537
1537
|
* @param details 当前的请求配置
|
|
1538
1538
|
* @private
|
|
1539
1539
|
*/
|
|
1540
|
-
beforeRequestCallBack(details) {
|
|
1540
|
+
async beforeRequestCallBack(details) {
|
|
1541
1541
|
if (typeof details.allowInterceptConfig === "boolean") {
|
|
1542
1542
|
if (!details.allowInterceptConfig) {
|
|
1543
1543
|
// 不允许拦截
|
|
@@ -1558,7 +1558,7 @@ class Httpx {
|
|
|
1558
1558
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1559
1559
|
let item = this.$config.configList[index];
|
|
1560
1560
|
if (typeof item.fn === "function") {
|
|
1561
|
-
let result = item.fn(details);
|
|
1561
|
+
let result = await item.fn(details);
|
|
1562
1562
|
if (result == null) {
|
|
1563
1563
|
return;
|
|
1564
1564
|
}
|
|
@@ -1615,7 +1615,7 @@ class Httpx {
|
|
|
1615
1615
|
* @param response 响应
|
|
1616
1616
|
* @param details 请求的配置
|
|
1617
1617
|
*/
|
|
1618
|
-
successResponseCallBack(response, details) {
|
|
1618
|
+
async successResponseCallBack(response, details) {
|
|
1619
1619
|
if (typeof details.allowInterceptConfig === "boolean") {
|
|
1620
1620
|
if (!details.allowInterceptConfig) {
|
|
1621
1621
|
// 不允许拦截
|
|
@@ -1637,7 +1637,8 @@ class Httpx {
|
|
|
1637
1637
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1638
1638
|
let item = this.$config.configList[index];
|
|
1639
1639
|
if (typeof item.successFn === "function") {
|
|
1640
|
-
|
|
1640
|
+
let result = await item.successFn(response, details);
|
|
1641
|
+
if (result == null) {
|
|
1641
1642
|
return;
|
|
1642
1643
|
}
|
|
1643
1644
|
}
|
|
@@ -1650,7 +1651,7 @@ class Httpx {
|
|
|
1650
1651
|
* @returns
|
|
1651
1652
|
* 返回null|undefined就是拦截掉了
|
|
1652
1653
|
*/
|
|
1653
|
-
errorResponseCallBack(data) {
|
|
1654
|
+
async errorResponseCallBack(data) {
|
|
1654
1655
|
if (typeof data.details.allowInterceptConfig === "boolean") {
|
|
1655
1656
|
if (!data.details.allowInterceptConfig) {
|
|
1656
1657
|
// 不允许拦截
|
|
@@ -1672,7 +1673,8 @@ class Httpx {
|
|
|
1672
1673
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1673
1674
|
let item = this.$config.configList[index];
|
|
1674
1675
|
if (typeof item.errorFn === "function") {
|
|
1675
|
-
|
|
1676
|
+
let result = await item.errorFn(data);
|
|
1677
|
+
if (result == null) {
|
|
1676
1678
|
return;
|
|
1677
1679
|
}
|
|
1678
1680
|
}
|
|
@@ -1712,7 +1714,7 @@ class Httpx {
|
|
|
1712
1714
|
this.$config.configList = [];
|
|
1713
1715
|
},
|
|
1714
1716
|
};
|
|
1715
|
-
|
|
1717
|
+
HttpxRequestOption = {
|
|
1716
1718
|
context: this,
|
|
1717
1719
|
/**
|
|
1718
1720
|
* 根据传入的参数处理获取details配置
|
|
@@ -2060,7 +2062,7 @@ class Httpx {
|
|
|
2060
2062
|
* @param reject 抛出错误
|
|
2061
2063
|
* @param argsResult 返回的参数列表
|
|
2062
2064
|
*/
|
|
2063
|
-
onAbort(details, resolve, reject, argsResult) {
|
|
2065
|
+
async onAbort(details, resolve, reject, argsResult) {
|
|
2064
2066
|
// console.log(argsResult);
|
|
2065
2067
|
if ("onabort" in details) {
|
|
2066
2068
|
details.onabort.apply(this, argsResult);
|
|
@@ -2072,12 +2074,12 @@ class Httpx {
|
|
|
2072
2074
|
if (response.length) {
|
|
2073
2075
|
response = response[0];
|
|
2074
2076
|
}
|
|
2075
|
-
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2077
|
+
if ((await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2076
2078
|
type: "onabort",
|
|
2077
2079
|
error: new TypeError("request canceled"),
|
|
2078
2080
|
response: null,
|
|
2079
2081
|
details: details,
|
|
2080
|
-
}) == null) {
|
|
2082
|
+
})) == null) {
|
|
2081
2083
|
// reject(new TypeError("response is intercept with onabort"));
|
|
2082
2084
|
return;
|
|
2083
2085
|
}
|
|
@@ -2097,7 +2099,7 @@ class Httpx {
|
|
|
2097
2099
|
* @param reject 抛出错误
|
|
2098
2100
|
* @param argsResult 返回的参数列表
|
|
2099
2101
|
*/
|
|
2100
|
-
onError(details, resolve, reject, argsResult) {
|
|
2102
|
+
async onError(details, resolve, reject, argsResult) {
|
|
2101
2103
|
// console.log(argsResult);
|
|
2102
2104
|
if ("onerror" in details) {
|
|
2103
2105
|
details.onerror.apply(this, argsResult);
|
|
@@ -2109,12 +2111,12 @@ class Httpx {
|
|
|
2109
2111
|
if (response.length) {
|
|
2110
2112
|
response = response[0];
|
|
2111
2113
|
}
|
|
2112
|
-
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2114
|
+
if ((await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2113
2115
|
type: "onerror",
|
|
2114
2116
|
error: new TypeError("request error"),
|
|
2115
2117
|
response: response,
|
|
2116
2118
|
details: details,
|
|
2117
|
-
}) == null) {
|
|
2119
|
+
})) == null) {
|
|
2118
2120
|
// reject(new TypeError("response is intercept with onerror"));
|
|
2119
2121
|
return;
|
|
2120
2122
|
}
|
|
@@ -2134,7 +2136,7 @@ class Httpx {
|
|
|
2134
2136
|
* @param reject 抛出错误
|
|
2135
2137
|
* @param argsResult 返回的参数列表
|
|
2136
2138
|
*/
|
|
2137
|
-
onTimeout(details, resolve, reject, argsResult) {
|
|
2139
|
+
async onTimeout(details, resolve, reject, argsResult) {
|
|
2138
2140
|
// console.log(argsResult);
|
|
2139
2141
|
if ("ontimeout" in details) {
|
|
2140
2142
|
details.ontimeout.apply(this, argsResult);
|
|
@@ -2146,12 +2148,12 @@ class Httpx {
|
|
|
2146
2148
|
if (response.length) {
|
|
2147
2149
|
response = response[0];
|
|
2148
2150
|
}
|
|
2149
|
-
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2151
|
+
if ((await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2150
2152
|
type: "ontimeout",
|
|
2151
2153
|
error: new TypeError("request timeout"),
|
|
2152
2154
|
response: (argsResult || [null])[0],
|
|
2153
2155
|
details: details,
|
|
2154
|
-
}) == null) {
|
|
2156
|
+
})) == null) {
|
|
2155
2157
|
// reject(new TypeError("response is intercept with ontimeout"));
|
|
2156
2158
|
return;
|
|
2157
2159
|
}
|
|
@@ -2185,7 +2187,7 @@ class Httpx {
|
|
|
2185
2187
|
* @param reject 抛出错误
|
|
2186
2188
|
* @param argsResult 返回的参数列表
|
|
2187
2189
|
*/
|
|
2188
|
-
onLoad(details, resolve, reject, argsResult) {
|
|
2190
|
+
async onLoad(details, resolve, reject, argsResult) {
|
|
2189
2191
|
// console.log(argsResult);
|
|
2190
2192
|
/* X浏览器会因为设置了responseType导致不返回responseText */
|
|
2191
2193
|
let originResponse = argsResult[0];
|
|
@@ -2256,7 +2258,7 @@ class Httpx {
|
|
|
2256
2258
|
}
|
|
2257
2259
|
/* 状态码2xx都是成功的 */
|
|
2258
2260
|
if (Math.floor(originResponse.status / 100) === 2) {
|
|
2259
|
-
if (this.context.HttpxResponseHook.successResponseCallBack(originResponse, details) == null) {
|
|
2261
|
+
if ((await this.context.HttpxResponseHook.successResponseCallBack(originResponse, details)) == null) {
|
|
2260
2262
|
// reject(new TypeError("response is intercept with onloada"));
|
|
2261
2263
|
return;
|
|
2262
2264
|
}
|
|
@@ -2308,20 +2310,20 @@ class Httpx {
|
|
|
2308
2310
|
* 发送请求
|
|
2309
2311
|
* @param details
|
|
2310
2312
|
*/
|
|
2311
|
-
request(details) {
|
|
2313
|
+
async request(details) {
|
|
2312
2314
|
if (this.context.#LOG_DETAILS) {
|
|
2313
2315
|
console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
|
|
2314
2316
|
}
|
|
2315
2317
|
if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
|
|
2316
2318
|
"function") {
|
|
2317
|
-
let hookResult = this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
2319
|
+
let hookResult = await this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
2318
2320
|
if (hookResult == null) {
|
|
2319
2321
|
return;
|
|
2320
2322
|
}
|
|
2321
2323
|
}
|
|
2322
2324
|
if (details.fetch) {
|
|
2323
2325
|
// 使用fetch请求
|
|
2324
|
-
const { fetchOption: fetchOption, fetchRequestOption: fetchRequestOption, abortController, } = this.context.
|
|
2326
|
+
const { fetchOption: fetchOption, fetchRequestOption: fetchRequestOption, abortController, } = this.context.HttpxRequestOption.handleFetchOption(details);
|
|
2325
2327
|
return this.fetch(fetchOption, fetchRequestOption, abortController);
|
|
2326
2328
|
}
|
|
2327
2329
|
else {
|
|
@@ -2609,15 +2611,15 @@ class Httpx {
|
|
|
2609
2611
|
* @param url 网址
|
|
2610
2612
|
* @param details 配置
|
|
2611
2613
|
*/
|
|
2612
|
-
|
|
2614
|
+
get(...args // @ts-ignore
|
|
2613
2615
|
) {
|
|
2614
|
-
let userRequestOption = this.
|
|
2616
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2615
2617
|
let abortFn = null;
|
|
2616
|
-
let promise = new globalThis.Promise((resolve, reject) => {
|
|
2617
|
-
let requestOption = this.
|
|
2618
|
+
let promise = new globalThis.Promise(async (resolve, reject) => {
|
|
2619
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("GET", userRequestOption, resolve, reject);
|
|
2618
2620
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2619
|
-
this.
|
|
2620
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2621
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2622
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2621
2623
|
if (requestResult != null &&
|
|
2622
2624
|
typeof requestResult.abort === "function") {
|
|
2623
2625
|
abortFn = requestResult.abort;
|
|
@@ -2629,21 +2631,22 @@ class Httpx {
|
|
|
2629
2631
|
abortFn();
|
|
2630
2632
|
}
|
|
2631
2633
|
};
|
|
2634
|
+
// @ts-ignore
|
|
2632
2635
|
return promise;
|
|
2633
2636
|
}
|
|
2634
2637
|
/**
|
|
2635
2638
|
* POST 请求
|
|
2636
2639
|
*/
|
|
2637
|
-
|
|
2640
|
+
post(...args // @ts-ignore
|
|
2638
2641
|
) {
|
|
2639
|
-
let userRequestOption = this.
|
|
2642
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2640
2643
|
let abortFn = null;
|
|
2641
|
-
let promise = new Promise((resolve, reject) => {
|
|
2642
|
-
let requestOption = this.
|
|
2644
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2645
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("POST", userRequestOption, resolve, reject);
|
|
2643
2646
|
// @ts-ignore
|
|
2644
2647
|
requestOption =
|
|
2645
|
-
this.
|
|
2646
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2648
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2649
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2647
2650
|
if (requestResult != null &&
|
|
2648
2651
|
typeof requestResult.abort === "function") {
|
|
2649
2652
|
abortFn = requestResult.abort;
|
|
@@ -2655,22 +2658,23 @@ class Httpx {
|
|
|
2655
2658
|
abortFn();
|
|
2656
2659
|
}
|
|
2657
2660
|
};
|
|
2661
|
+
// @ts-ignore
|
|
2658
2662
|
return promise;
|
|
2659
2663
|
}
|
|
2660
2664
|
/**
|
|
2661
2665
|
* HEAD 请求
|
|
2662
2666
|
*/
|
|
2663
|
-
|
|
2667
|
+
head(...args // @ts-ignore
|
|
2664
2668
|
) {
|
|
2665
|
-
let userRequestOption = this.
|
|
2669
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2666
2670
|
let abortFn = null;
|
|
2667
|
-
let promise = new Promise((resolve, reject) => {
|
|
2668
|
-
let requestOption = this.
|
|
2671
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2672
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("HEAD", userRequestOption, resolve, reject);
|
|
2669
2673
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2670
2674
|
// @ts-ignore
|
|
2671
2675
|
requestOption =
|
|
2672
|
-
this.
|
|
2673
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2676
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2677
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2674
2678
|
if (requestResult != null &&
|
|
2675
2679
|
typeof requestResult.abort === "function") {
|
|
2676
2680
|
abortFn = requestResult.abort;
|
|
@@ -2682,22 +2686,23 @@ class Httpx {
|
|
|
2682
2686
|
abortFn();
|
|
2683
2687
|
}
|
|
2684
2688
|
};
|
|
2689
|
+
// @ts-ignore
|
|
2685
2690
|
return promise;
|
|
2686
2691
|
}
|
|
2687
2692
|
/**
|
|
2688
2693
|
* OPTIONS 请求
|
|
2689
2694
|
*/
|
|
2690
|
-
|
|
2695
|
+
options(...args // @ts-ignore
|
|
2691
2696
|
) {
|
|
2692
|
-
let userRequestOption = this.
|
|
2697
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2693
2698
|
let abortFn = null;
|
|
2694
|
-
let promise = new Promise((resolve, reject) => {
|
|
2695
|
-
let requestOption = this.
|
|
2699
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2700
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("OPTIONS", userRequestOption, resolve, reject);
|
|
2696
2701
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2697
2702
|
// @ts-ignore
|
|
2698
2703
|
requestOption =
|
|
2699
|
-
this.
|
|
2700
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2704
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2705
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2701
2706
|
if (requestResult != null &&
|
|
2702
2707
|
typeof requestResult.abort === "function") {
|
|
2703
2708
|
abortFn = requestResult.abort;
|
|
@@ -2709,22 +2714,23 @@ class Httpx {
|
|
|
2709
2714
|
abortFn();
|
|
2710
2715
|
}
|
|
2711
2716
|
};
|
|
2717
|
+
// @ts-ignore
|
|
2712
2718
|
return promise;
|
|
2713
2719
|
}
|
|
2714
2720
|
/**
|
|
2715
2721
|
* DELETE 请求
|
|
2716
2722
|
*/
|
|
2717
|
-
|
|
2723
|
+
delete(...args // @ts-ignore
|
|
2718
2724
|
) {
|
|
2719
|
-
let userRequestOption = this.
|
|
2725
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2720
2726
|
let abortFn = null;
|
|
2721
|
-
let promise = new Promise((resolve, reject) => {
|
|
2722
|
-
let requestOption = this.
|
|
2727
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2728
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("DELETE", userRequestOption, resolve, reject);
|
|
2723
2729
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2724
2730
|
// @ts-ignore
|
|
2725
2731
|
requestOption =
|
|
2726
|
-
this.
|
|
2727
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2732
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2733
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2728
2734
|
if (requestResult != null &&
|
|
2729
2735
|
typeof requestResult.abort === "function") {
|
|
2730
2736
|
abortFn = requestResult.abort;
|
|
@@ -2736,21 +2742,22 @@ class Httpx {
|
|
|
2736
2742
|
abortFn();
|
|
2737
2743
|
}
|
|
2738
2744
|
};
|
|
2745
|
+
// @ts-ignore
|
|
2739
2746
|
return promise;
|
|
2740
2747
|
}
|
|
2741
2748
|
/**
|
|
2742
2749
|
* PUT 请求
|
|
2743
2750
|
*/
|
|
2744
|
-
|
|
2751
|
+
put(...args // @ts-ignore
|
|
2745
2752
|
) {
|
|
2746
|
-
let userRequestOption = this.
|
|
2753
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2747
2754
|
let abortFn = null;
|
|
2748
|
-
let promise = new Promise((resolve, reject) => {
|
|
2749
|
-
let requestOption = this.
|
|
2755
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2756
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("PUT", userRequestOption, resolve, reject);
|
|
2750
2757
|
// @ts-ignore
|
|
2751
2758
|
requestOption =
|
|
2752
|
-
this.
|
|
2753
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2759
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2760
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2754
2761
|
if (requestResult != null &&
|
|
2755
2762
|
typeof requestResult.abort === "function") {
|
|
2756
2763
|
abortFn = requestResult.abort;
|
|
@@ -2762,6 +2769,7 @@ class Httpx {
|
|
|
2762
2769
|
abortFn();
|
|
2763
2770
|
}
|
|
2764
2771
|
};
|
|
2772
|
+
// @ts-ignore
|
|
2765
2773
|
return promise;
|
|
2766
2774
|
}
|
|
2767
2775
|
}
|
|
@@ -4066,7 +4074,7 @@ class Utils {
|
|
|
4066
4074
|
this.windowApi = new WindowApi(option);
|
|
4067
4075
|
}
|
|
4068
4076
|
/** 版本号 */
|
|
4069
|
-
version = "2024.10.
|
|
4077
|
+
version = "2024.10.31";
|
|
4070
4078
|
addStyle(cssText) {
|
|
4071
4079
|
if (typeof cssText !== "string") {
|
|
4072
4080
|
throw new Error("Utils.addStyle 参数cssText 必须为String类型");
|