@whitesev/utils 2.4.2 → 2.4.4
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 +83 -58
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +83 -58
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +83 -58
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +83 -58
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +83 -58
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +83 -58
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Httpx.d.ts +3 -3
- package/dist/types/src/Utils.d.ts +17 -0
- package/package.json +1 -1
- package/src/Httpx.ts +91 -83
- package/src/Utils.ts +35 -1
package/dist/index.esm.js
CHANGED
|
@@ -1535,7 +1535,7 @@ class Httpx {
|
|
|
1535
1535
|
* @param details 当前的请求配置
|
|
1536
1536
|
* @private
|
|
1537
1537
|
*/
|
|
1538
|
-
beforeRequestCallBack(details) {
|
|
1538
|
+
async beforeRequestCallBack(details) {
|
|
1539
1539
|
if (typeof details.allowInterceptConfig === "boolean") {
|
|
1540
1540
|
if (!details.allowInterceptConfig) {
|
|
1541
1541
|
// 不允许拦截
|
|
@@ -1556,7 +1556,7 @@ class Httpx {
|
|
|
1556
1556
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1557
1557
|
let item = this.$config.configList[index];
|
|
1558
1558
|
if (typeof item.fn === "function") {
|
|
1559
|
-
let result = item.fn(details);
|
|
1559
|
+
let result = await item.fn(details);
|
|
1560
1560
|
if (result == null) {
|
|
1561
1561
|
return;
|
|
1562
1562
|
}
|
|
@@ -1613,7 +1613,7 @@ class Httpx {
|
|
|
1613
1613
|
* @param response 响应
|
|
1614
1614
|
* @param details 请求的配置
|
|
1615
1615
|
*/
|
|
1616
|
-
successResponseCallBack(response, details) {
|
|
1616
|
+
async successResponseCallBack(response, details) {
|
|
1617
1617
|
if (typeof details.allowInterceptConfig === "boolean") {
|
|
1618
1618
|
if (!details.allowInterceptConfig) {
|
|
1619
1619
|
// 不允许拦截
|
|
@@ -1635,7 +1635,8 @@ class Httpx {
|
|
|
1635
1635
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1636
1636
|
let item = this.$config.configList[index];
|
|
1637
1637
|
if (typeof item.successFn === "function") {
|
|
1638
|
-
|
|
1638
|
+
let result = await item.successFn(response, details);
|
|
1639
|
+
if (result == null) {
|
|
1639
1640
|
return;
|
|
1640
1641
|
}
|
|
1641
1642
|
}
|
|
@@ -1648,7 +1649,7 @@ class Httpx {
|
|
|
1648
1649
|
* @returns
|
|
1649
1650
|
* 返回null|undefined就是拦截掉了
|
|
1650
1651
|
*/
|
|
1651
|
-
errorResponseCallBack(data) {
|
|
1652
|
+
async errorResponseCallBack(data) {
|
|
1652
1653
|
if (typeof data.details.allowInterceptConfig === "boolean") {
|
|
1653
1654
|
if (!data.details.allowInterceptConfig) {
|
|
1654
1655
|
// 不允许拦截
|
|
@@ -1670,7 +1671,8 @@ class Httpx {
|
|
|
1670
1671
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1671
1672
|
let item = this.$config.configList[index];
|
|
1672
1673
|
if (typeof item.errorFn === "function") {
|
|
1673
|
-
|
|
1674
|
+
let result = await item.errorFn(data);
|
|
1675
|
+
if (result == null) {
|
|
1674
1676
|
return;
|
|
1675
1677
|
}
|
|
1676
1678
|
}
|
|
@@ -1710,7 +1712,7 @@ class Httpx {
|
|
|
1710
1712
|
this.$config.configList = [];
|
|
1711
1713
|
},
|
|
1712
1714
|
};
|
|
1713
|
-
|
|
1715
|
+
HttpxRequestOption = {
|
|
1714
1716
|
context: this,
|
|
1715
1717
|
/**
|
|
1716
1718
|
* 根据传入的参数处理获取details配置
|
|
@@ -2058,7 +2060,7 @@ class Httpx {
|
|
|
2058
2060
|
* @param reject 抛出错误
|
|
2059
2061
|
* @param argsResult 返回的参数列表
|
|
2060
2062
|
*/
|
|
2061
|
-
onAbort(details, resolve, reject, argsResult) {
|
|
2063
|
+
async onAbort(details, resolve, reject, argsResult) {
|
|
2062
2064
|
// console.log(argsResult);
|
|
2063
2065
|
if ("onabort" in details) {
|
|
2064
2066
|
details.onabort.apply(this, argsResult);
|
|
@@ -2070,12 +2072,12 @@ class Httpx {
|
|
|
2070
2072
|
if (response.length) {
|
|
2071
2073
|
response = response[0];
|
|
2072
2074
|
}
|
|
2073
|
-
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2075
|
+
if ((await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2074
2076
|
type: "onabort",
|
|
2075
2077
|
error: new TypeError("request canceled"),
|
|
2076
2078
|
response: null,
|
|
2077
2079
|
details: details,
|
|
2078
|
-
}) == null) {
|
|
2080
|
+
})) == null) {
|
|
2079
2081
|
// reject(new TypeError("response is intercept with onabort"));
|
|
2080
2082
|
return;
|
|
2081
2083
|
}
|
|
@@ -2095,7 +2097,7 @@ class Httpx {
|
|
|
2095
2097
|
* @param reject 抛出错误
|
|
2096
2098
|
* @param argsResult 返回的参数列表
|
|
2097
2099
|
*/
|
|
2098
|
-
onError(details, resolve, reject, argsResult) {
|
|
2100
|
+
async onError(details, resolve, reject, argsResult) {
|
|
2099
2101
|
// console.log(argsResult);
|
|
2100
2102
|
if ("onerror" in details) {
|
|
2101
2103
|
details.onerror.apply(this, argsResult);
|
|
@@ -2107,12 +2109,12 @@ class Httpx {
|
|
|
2107
2109
|
if (response.length) {
|
|
2108
2110
|
response = response[0];
|
|
2109
2111
|
}
|
|
2110
|
-
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2112
|
+
if ((await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2111
2113
|
type: "onerror",
|
|
2112
2114
|
error: new TypeError("request error"),
|
|
2113
2115
|
response: response,
|
|
2114
2116
|
details: details,
|
|
2115
|
-
}) == null) {
|
|
2117
|
+
})) == null) {
|
|
2116
2118
|
// reject(new TypeError("response is intercept with onerror"));
|
|
2117
2119
|
return;
|
|
2118
2120
|
}
|
|
@@ -2132,7 +2134,7 @@ class Httpx {
|
|
|
2132
2134
|
* @param reject 抛出错误
|
|
2133
2135
|
* @param argsResult 返回的参数列表
|
|
2134
2136
|
*/
|
|
2135
|
-
onTimeout(details, resolve, reject, argsResult) {
|
|
2137
|
+
async onTimeout(details, resolve, reject, argsResult) {
|
|
2136
2138
|
// console.log(argsResult);
|
|
2137
2139
|
if ("ontimeout" in details) {
|
|
2138
2140
|
details.ontimeout.apply(this, argsResult);
|
|
@@ -2144,12 +2146,12 @@ class Httpx {
|
|
|
2144
2146
|
if (response.length) {
|
|
2145
2147
|
response = response[0];
|
|
2146
2148
|
}
|
|
2147
|
-
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2149
|
+
if ((await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
2148
2150
|
type: "ontimeout",
|
|
2149
2151
|
error: new TypeError("request timeout"),
|
|
2150
2152
|
response: (argsResult || [null])[0],
|
|
2151
2153
|
details: details,
|
|
2152
|
-
}) == null) {
|
|
2154
|
+
})) == null) {
|
|
2153
2155
|
// reject(new TypeError("response is intercept with ontimeout"));
|
|
2154
2156
|
return;
|
|
2155
2157
|
}
|
|
@@ -2183,7 +2185,7 @@ class Httpx {
|
|
|
2183
2185
|
* @param reject 抛出错误
|
|
2184
2186
|
* @param argsResult 返回的参数列表
|
|
2185
2187
|
*/
|
|
2186
|
-
onLoad(details, resolve, reject, argsResult) {
|
|
2188
|
+
async onLoad(details, resolve, reject, argsResult) {
|
|
2187
2189
|
// console.log(argsResult);
|
|
2188
2190
|
/* X浏览器会因为设置了responseType导致不返回responseText */
|
|
2189
2191
|
let originResponse = argsResult[0];
|
|
@@ -2254,7 +2256,7 @@ class Httpx {
|
|
|
2254
2256
|
}
|
|
2255
2257
|
/* 状态码2xx都是成功的 */
|
|
2256
2258
|
if (Math.floor(originResponse.status / 100) === 2) {
|
|
2257
|
-
if (this.context.HttpxResponseHook.successResponseCallBack(originResponse, details) == null) {
|
|
2259
|
+
if ((await this.context.HttpxResponseHook.successResponseCallBack(originResponse, details)) == null) {
|
|
2258
2260
|
// reject(new TypeError("response is intercept with onloada"));
|
|
2259
2261
|
return;
|
|
2260
2262
|
}
|
|
@@ -2306,20 +2308,20 @@ class Httpx {
|
|
|
2306
2308
|
* 发送请求
|
|
2307
2309
|
* @param details
|
|
2308
2310
|
*/
|
|
2309
|
-
request(details) {
|
|
2311
|
+
async request(details) {
|
|
2310
2312
|
if (this.context.#LOG_DETAILS) {
|
|
2311
2313
|
console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
|
|
2312
2314
|
}
|
|
2313
2315
|
if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
|
|
2314
2316
|
"function") {
|
|
2315
|
-
let hookResult = this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
2317
|
+
let hookResult = await this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
2316
2318
|
if (hookResult == null) {
|
|
2317
2319
|
return;
|
|
2318
2320
|
}
|
|
2319
2321
|
}
|
|
2320
2322
|
if (details.fetch) {
|
|
2321
2323
|
// 使用fetch请求
|
|
2322
|
-
const { fetchOption: fetchOption, fetchRequestOption: fetchRequestOption, abortController, } = this.context.
|
|
2324
|
+
const { fetchOption: fetchOption, fetchRequestOption: fetchRequestOption, abortController, } = this.context.HttpxRequestOption.handleFetchOption(details);
|
|
2323
2325
|
return this.fetch(fetchOption, fetchRequestOption, abortController);
|
|
2324
2326
|
}
|
|
2325
2327
|
else {
|
|
@@ -2607,15 +2609,15 @@ class Httpx {
|
|
|
2607
2609
|
* @param url 网址
|
|
2608
2610
|
* @param details 配置
|
|
2609
2611
|
*/
|
|
2610
|
-
|
|
2612
|
+
get(...args // @ts-ignore
|
|
2611
2613
|
) {
|
|
2612
|
-
let userRequestOption = this.
|
|
2614
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2613
2615
|
let abortFn = null;
|
|
2614
|
-
let promise = new globalThis.Promise((resolve, reject) => {
|
|
2615
|
-
let requestOption = this.
|
|
2616
|
+
let promise = new globalThis.Promise(async (resolve, reject) => {
|
|
2617
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("GET", userRequestOption, resolve, reject);
|
|
2616
2618
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2617
|
-
this.
|
|
2618
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2619
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2620
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2619
2621
|
if (requestResult != null &&
|
|
2620
2622
|
typeof requestResult.abort === "function") {
|
|
2621
2623
|
abortFn = requestResult.abort;
|
|
@@ -2627,21 +2629,22 @@ class Httpx {
|
|
|
2627
2629
|
abortFn();
|
|
2628
2630
|
}
|
|
2629
2631
|
};
|
|
2632
|
+
// @ts-ignore
|
|
2630
2633
|
return promise;
|
|
2631
2634
|
}
|
|
2632
2635
|
/**
|
|
2633
2636
|
* POST 请求
|
|
2634
2637
|
*/
|
|
2635
|
-
|
|
2638
|
+
post(...args // @ts-ignore
|
|
2636
2639
|
) {
|
|
2637
|
-
let userRequestOption = this.
|
|
2640
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2638
2641
|
let abortFn = null;
|
|
2639
|
-
let promise = new Promise((resolve, reject) => {
|
|
2640
|
-
let requestOption = this.
|
|
2642
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2643
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("POST", userRequestOption, resolve, reject);
|
|
2641
2644
|
// @ts-ignore
|
|
2642
2645
|
requestOption =
|
|
2643
|
-
this.
|
|
2644
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2646
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2647
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2645
2648
|
if (requestResult != null &&
|
|
2646
2649
|
typeof requestResult.abort === "function") {
|
|
2647
2650
|
abortFn = requestResult.abort;
|
|
@@ -2653,22 +2656,23 @@ class Httpx {
|
|
|
2653
2656
|
abortFn();
|
|
2654
2657
|
}
|
|
2655
2658
|
};
|
|
2659
|
+
// @ts-ignore
|
|
2656
2660
|
return promise;
|
|
2657
2661
|
}
|
|
2658
2662
|
/**
|
|
2659
2663
|
* HEAD 请求
|
|
2660
2664
|
*/
|
|
2661
|
-
|
|
2665
|
+
head(...args // @ts-ignore
|
|
2662
2666
|
) {
|
|
2663
|
-
let userRequestOption = this.
|
|
2667
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2664
2668
|
let abortFn = null;
|
|
2665
|
-
let promise = new Promise((resolve, reject) => {
|
|
2666
|
-
let requestOption = this.
|
|
2669
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2670
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("HEAD", userRequestOption, resolve, reject);
|
|
2667
2671
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2668
2672
|
// @ts-ignore
|
|
2669
2673
|
requestOption =
|
|
2670
|
-
this.
|
|
2671
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2674
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2675
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2672
2676
|
if (requestResult != null &&
|
|
2673
2677
|
typeof requestResult.abort === "function") {
|
|
2674
2678
|
abortFn = requestResult.abort;
|
|
@@ -2680,22 +2684,23 @@ class Httpx {
|
|
|
2680
2684
|
abortFn();
|
|
2681
2685
|
}
|
|
2682
2686
|
};
|
|
2687
|
+
// @ts-ignore
|
|
2683
2688
|
return promise;
|
|
2684
2689
|
}
|
|
2685
2690
|
/**
|
|
2686
2691
|
* OPTIONS 请求
|
|
2687
2692
|
*/
|
|
2688
|
-
|
|
2693
|
+
options(...args // @ts-ignore
|
|
2689
2694
|
) {
|
|
2690
|
-
let userRequestOption = this.
|
|
2695
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2691
2696
|
let abortFn = null;
|
|
2692
|
-
let promise = new Promise((resolve, reject) => {
|
|
2693
|
-
let requestOption = this.
|
|
2697
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2698
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("OPTIONS", userRequestOption, resolve, reject);
|
|
2694
2699
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2695
2700
|
// @ts-ignore
|
|
2696
2701
|
requestOption =
|
|
2697
|
-
this.
|
|
2698
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2702
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2703
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2699
2704
|
if (requestResult != null &&
|
|
2700
2705
|
typeof requestResult.abort === "function") {
|
|
2701
2706
|
abortFn = requestResult.abort;
|
|
@@ -2707,22 +2712,23 @@ class Httpx {
|
|
|
2707
2712
|
abortFn();
|
|
2708
2713
|
}
|
|
2709
2714
|
};
|
|
2715
|
+
// @ts-ignore
|
|
2710
2716
|
return promise;
|
|
2711
2717
|
}
|
|
2712
2718
|
/**
|
|
2713
2719
|
* DELETE 请求
|
|
2714
2720
|
*/
|
|
2715
|
-
|
|
2721
|
+
delete(...args // @ts-ignore
|
|
2716
2722
|
) {
|
|
2717
|
-
let userRequestOption = this.
|
|
2723
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2718
2724
|
let abortFn = null;
|
|
2719
|
-
let promise = new Promise((resolve, reject) => {
|
|
2720
|
-
let requestOption = this.
|
|
2725
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2726
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("DELETE", userRequestOption, resolve, reject);
|
|
2721
2727
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
2722
2728
|
// @ts-ignore
|
|
2723
2729
|
requestOption =
|
|
2724
|
-
this.
|
|
2725
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2730
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2731
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2726
2732
|
if (requestResult != null &&
|
|
2727
2733
|
typeof requestResult.abort === "function") {
|
|
2728
2734
|
abortFn = requestResult.abort;
|
|
@@ -2734,21 +2740,22 @@ class Httpx {
|
|
|
2734
2740
|
abortFn();
|
|
2735
2741
|
}
|
|
2736
2742
|
};
|
|
2743
|
+
// @ts-ignore
|
|
2737
2744
|
return promise;
|
|
2738
2745
|
}
|
|
2739
2746
|
/**
|
|
2740
2747
|
* PUT 请求
|
|
2741
2748
|
*/
|
|
2742
|
-
|
|
2749
|
+
put(...args // @ts-ignore
|
|
2743
2750
|
) {
|
|
2744
|
-
let userRequestOption = this.
|
|
2751
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
|
|
2745
2752
|
let abortFn = null;
|
|
2746
|
-
let promise = new Promise((resolve, reject) => {
|
|
2747
|
-
let requestOption = this.
|
|
2753
|
+
let promise = new Promise(async (resolve, reject) => {
|
|
2754
|
+
let requestOption = this.HttpxRequestOption.getRequestOption("PUT", userRequestOption, resolve, reject);
|
|
2748
2755
|
// @ts-ignore
|
|
2749
2756
|
requestOption =
|
|
2750
|
-
this.
|
|
2751
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
2757
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
2758
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
2752
2759
|
if (requestResult != null &&
|
|
2753
2760
|
typeof requestResult.abort === "function") {
|
|
2754
2761
|
abortFn = requestResult.abort;
|
|
@@ -2760,6 +2767,7 @@ class Httpx {
|
|
|
2760
2767
|
abortFn();
|
|
2761
2768
|
}
|
|
2762
2769
|
};
|
|
2770
|
+
// @ts-ignore
|
|
2763
2771
|
return promise;
|
|
2764
2772
|
}
|
|
2765
2773
|
}
|
|
@@ -4064,7 +4072,7 @@ class Utils {
|
|
|
4064
4072
|
this.windowApi = new WindowApi(option);
|
|
4065
4073
|
}
|
|
4066
4074
|
/** 版本号 */
|
|
4067
|
-
version = "2024.
|
|
4075
|
+
version = "2024.11.1";
|
|
4068
4076
|
addStyle(cssText) {
|
|
4069
4077
|
if (typeof cssText !== "string") {
|
|
4070
4078
|
throw new Error("Utils.addStyle 参数cssText 必须为String类型");
|
|
@@ -7082,6 +7090,23 @@ class Utils {
|
|
|
7082
7090
|
});
|
|
7083
7091
|
}
|
|
7084
7092
|
}
|
|
7093
|
+
/**
|
|
7094
|
+
* 深度获取对象属性
|
|
7095
|
+
* @param target 待获取的对象
|
|
7096
|
+
* @param handler 获取属性的回调
|
|
7097
|
+
*/
|
|
7098
|
+
queryProperty(target, handler) {
|
|
7099
|
+
if (target == null) {
|
|
7100
|
+
return;
|
|
7101
|
+
}
|
|
7102
|
+
let handleResult = handler(target);
|
|
7103
|
+
if (handleResult &&
|
|
7104
|
+
typeof handleResult.isFind === "boolean" &&
|
|
7105
|
+
handleResult.isFind) {
|
|
7106
|
+
return handleResult.data;
|
|
7107
|
+
}
|
|
7108
|
+
return this.queryProperty(handleResult.data, handler);
|
|
7109
|
+
}
|
|
7085
7110
|
/**
|
|
7086
7111
|
* 创建一个新的Utils实例
|
|
7087
7112
|
* @param option
|