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