@whitesev/utils 1.4.8 → 1.5.0
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 +55 -14
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +55 -14
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +55 -14
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +55 -14
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +55 -14
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +55 -14
- package/dist/index.umd.js.map +1 -1
- package/dist/src/Httpx.d.ts +13 -1
- package/package.json +1 -1
- package/src/Httpx.ts +80 -21
package/dist/src/Httpx.d.ts
CHANGED
|
@@ -1140,6 +1140,10 @@ declare class Httpx {
|
|
|
1140
1140
|
* @param id 通过use返回的id
|
|
1141
1141
|
*/
|
|
1142
1142
|
eject(id: string): boolean;
|
|
1143
|
+
/**
|
|
1144
|
+
* 移除所有拦截器
|
|
1145
|
+
*/
|
|
1146
|
+
ejectAll(): void;
|
|
1143
1147
|
};
|
|
1144
1148
|
/**
|
|
1145
1149
|
* 响应拦截器
|
|
@@ -1153,12 +1157,20 @@ declare class Httpx {
|
|
|
1153
1157
|
* @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
1154
1158
|
* + 超出 2xx 范围的状态码都会触发该函数
|
|
1155
1159
|
*/
|
|
1156
|
-
use(successFn:
|
|
1160
|
+
use(successFn?: (response: HttpxAsyncResultData) => void, errorFn?: (data: {
|
|
1161
|
+
type: "onerror" | "ontimeout" | "onabort";
|
|
1162
|
+
error: Error;
|
|
1163
|
+
response: any;
|
|
1164
|
+
}) => void): string | undefined;
|
|
1157
1165
|
/**
|
|
1158
1166
|
* 移除拦截器
|
|
1159
1167
|
* @param id 通过use返回的id
|
|
1160
1168
|
*/
|
|
1161
1169
|
eject(id: string): boolean;
|
|
1170
|
+
/**
|
|
1171
|
+
* 移除所有拦截器
|
|
1172
|
+
*/
|
|
1173
|
+
ejectAll(): void;
|
|
1162
1174
|
};
|
|
1163
1175
|
};
|
|
1164
1176
|
/**
|
package/package.json
CHANGED
package/src/Httpx.ts
CHANGED
|
@@ -1247,7 +1247,7 @@ class Httpx {
|
|
|
1247
1247
|
/**
|
|
1248
1248
|
* 添加请求前的回调处理配置
|
|
1249
1249
|
*/
|
|
1250
|
-
|
|
1250
|
+
add(fn: Function) {
|
|
1251
1251
|
if (typeof fn === "function") {
|
|
1252
1252
|
let uuid = GenerateUUID();
|
|
1253
1253
|
this.$config.configList.push({
|
|
@@ -1265,7 +1265,7 @@ class Httpx {
|
|
|
1265
1265
|
* 删除请求前的回调处理配置
|
|
1266
1266
|
* @param id
|
|
1267
1267
|
*/
|
|
1268
|
-
|
|
1268
|
+
delete(id: string) {
|
|
1269
1269
|
if (typeof id === "string") {
|
|
1270
1270
|
let findIndex = this.$config.configList.findIndex(
|
|
1271
1271
|
(item) => item.id === id
|
|
@@ -1280,7 +1280,7 @@ class Httpx {
|
|
|
1280
1280
|
/**
|
|
1281
1281
|
* 清空设置的请求前的回调处理配置
|
|
1282
1282
|
*/
|
|
1283
|
-
|
|
1283
|
+
clearAll() {
|
|
1284
1284
|
this.$config.configList = [];
|
|
1285
1285
|
},
|
|
1286
1286
|
};
|
|
@@ -1292,8 +1292,8 @@ class Httpx {
|
|
|
1292
1292
|
configList: <
|
|
1293
1293
|
{
|
|
1294
1294
|
id: string;
|
|
1295
|
-
successFn
|
|
1296
|
-
errorFn
|
|
1295
|
+
successFn?: Function;
|
|
1296
|
+
errorFn?: Function;
|
|
1297
1297
|
}[]
|
|
1298
1298
|
>[],
|
|
1299
1299
|
},
|
|
@@ -1301,7 +1301,7 @@ class Httpx {
|
|
|
1301
1301
|
* 成功的回调
|
|
1302
1302
|
* @param response
|
|
1303
1303
|
*/
|
|
1304
|
-
successResponseCallBack(response:
|
|
1304
|
+
successResponseCallBack(response: HttpxAsyncResultData) {
|
|
1305
1305
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1306
1306
|
let item = this.$config.configList[index];
|
|
1307
1307
|
if (typeof item.successFn === "function") {
|
|
@@ -1316,21 +1316,25 @@ class Httpx {
|
|
|
1316
1316
|
* 失败的回调
|
|
1317
1317
|
* @param response
|
|
1318
1318
|
*/
|
|
1319
|
-
errorResponseCallBack(
|
|
1319
|
+
errorResponseCallBack(data: {
|
|
1320
|
+
type: "onerror" | "ontimeout" | "onabort";
|
|
1321
|
+
error: Error;
|
|
1322
|
+
response: any;
|
|
1323
|
+
}) {
|
|
1320
1324
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1321
1325
|
let item = this.$config.configList[index];
|
|
1322
1326
|
if (typeof item.errorFn === "function") {
|
|
1323
|
-
if (item.errorFn(
|
|
1327
|
+
if (item.errorFn(data) == null) {
|
|
1324
1328
|
return;
|
|
1325
1329
|
}
|
|
1326
1330
|
}
|
|
1327
1331
|
}
|
|
1328
|
-
return
|
|
1332
|
+
return data;
|
|
1329
1333
|
},
|
|
1330
1334
|
/**
|
|
1331
1335
|
* 添加请求前的回调处理配置
|
|
1332
1336
|
*/
|
|
1333
|
-
|
|
1337
|
+
add(successFn?: Function, errorFn?: Function) {
|
|
1334
1338
|
let id = GenerateUUID();
|
|
1335
1339
|
this.$config.configList.push({
|
|
1336
1340
|
id: id,
|
|
@@ -1343,7 +1347,7 @@ class Httpx {
|
|
|
1343
1347
|
* 删除请求前的回调处理配置
|
|
1344
1348
|
* @param id
|
|
1345
1349
|
*/
|
|
1346
|
-
|
|
1350
|
+
delete(id: string) {
|
|
1347
1351
|
if (typeof id === "string") {
|
|
1348
1352
|
let findIndex = this.$config.configList.findIndex(
|
|
1349
1353
|
(item) => item.id === id
|
|
@@ -1358,7 +1362,7 @@ class Httpx {
|
|
|
1358
1362
|
/**
|
|
1359
1363
|
* 清空设置的请求前的回调处理配置
|
|
1360
1364
|
*/
|
|
1361
|
-
|
|
1365
|
+
clearAll() {
|
|
1362
1366
|
this.$config.configList = [];
|
|
1363
1367
|
},
|
|
1364
1368
|
};
|
|
@@ -1617,6 +1621,15 @@ class Httpx {
|
|
|
1617
1621
|
} else if ("onabort" in this.context.#defaultDetails) {
|
|
1618
1622
|
this.context.#defaultDetails!.onabort!.apply(this, argumentsList);
|
|
1619
1623
|
}
|
|
1624
|
+
if (
|
|
1625
|
+
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
1626
|
+
type: "onabort",
|
|
1627
|
+
error: new TypeError("request canceled"),
|
|
1628
|
+
response: null,
|
|
1629
|
+
}) == null
|
|
1630
|
+
) {
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1620
1633
|
resolve({
|
|
1621
1634
|
status: false,
|
|
1622
1635
|
data: [...argumentsList],
|
|
@@ -1645,6 +1658,15 @@ class Httpx {
|
|
|
1645
1658
|
if (response.length) {
|
|
1646
1659
|
response = response[0];
|
|
1647
1660
|
}
|
|
1661
|
+
if (
|
|
1662
|
+
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
1663
|
+
type: "onerror",
|
|
1664
|
+
error: new TypeError("request error"),
|
|
1665
|
+
response: response,
|
|
1666
|
+
}) == null
|
|
1667
|
+
) {
|
|
1668
|
+
return;
|
|
1669
|
+
}
|
|
1648
1670
|
resolve({
|
|
1649
1671
|
status: false,
|
|
1650
1672
|
data: response,
|
|
@@ -1669,6 +1691,16 @@ class Httpx {
|
|
|
1669
1691
|
} else if ("ontimeout" in this.context.#defaultDetails) {
|
|
1670
1692
|
this.context.#defaultDetails!.ontimeout!.apply(this, argumentsList);
|
|
1671
1693
|
}
|
|
1694
|
+
|
|
1695
|
+
if (
|
|
1696
|
+
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
1697
|
+
type: "ontimeout",
|
|
1698
|
+
error: new TypeError("request timeout"),
|
|
1699
|
+
response: (argumentsList || [null])[0],
|
|
1700
|
+
}) == null
|
|
1701
|
+
) {
|
|
1702
|
+
return;
|
|
1703
|
+
}
|
|
1672
1704
|
resolve({
|
|
1673
1705
|
status: false,
|
|
1674
1706
|
data: [...argumentsList],
|
|
@@ -1755,8 +1787,15 @@ class Httpx {
|
|
|
1755
1787
|
) {
|
|
1756
1788
|
Response["finalUrl"] = (Response as any)["responseURL"];
|
|
1757
1789
|
}
|
|
1790
|
+
|
|
1758
1791
|
/* 状态码2xx都是成功的 */
|
|
1759
1792
|
if (Math.floor(Response.status / 100) === 2) {
|
|
1793
|
+
if (
|
|
1794
|
+
this.context.HttpxResponseHook.successResponseCallBack(Response) ==
|
|
1795
|
+
null
|
|
1796
|
+
) {
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1760
1799
|
resolve({
|
|
1761
1800
|
status: true,
|
|
1762
1801
|
data: Response,
|
|
@@ -2038,14 +2077,24 @@ class Httpx {
|
|
|
2038
2077
|
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
2039
2078
|
*/
|
|
2040
2079
|
use(fn: (details: Required<HttpxDetails>) => void) {
|
|
2041
|
-
|
|
2080
|
+
if (typeof fn !== "function") {
|
|
2081
|
+
console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
return this.context.HttpxRequestHook.add(fn);
|
|
2042
2085
|
},
|
|
2043
2086
|
/**
|
|
2044
2087
|
* 移除拦截器
|
|
2045
2088
|
* @param id 通过use返回的id
|
|
2046
2089
|
*/
|
|
2047
2090
|
eject(id: string) {
|
|
2048
|
-
return this.context.HttpxRequestHook.
|
|
2091
|
+
return this.context.HttpxRequestHook.delete(id);
|
|
2092
|
+
},
|
|
2093
|
+
/**
|
|
2094
|
+
* 移除所有拦截器
|
|
2095
|
+
*/
|
|
2096
|
+
ejectAll() {
|
|
2097
|
+
this.context.HttpxRequestHook.clearAll();
|
|
2049
2098
|
},
|
|
2050
2099
|
},
|
|
2051
2100
|
/**
|
|
@@ -2060,22 +2109,32 @@ class Httpx {
|
|
|
2060
2109
|
* @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
2061
2110
|
* + 超出 2xx 范围的状态码都会触发该函数
|
|
2062
2111
|
*/
|
|
2063
|
-
use(
|
|
2112
|
+
use(
|
|
2113
|
+
successFn?: (response: HttpxAsyncResultData) => void,
|
|
2114
|
+
errorFn?: (data: {
|
|
2115
|
+
type: "onerror" | "ontimeout" | "onabort";
|
|
2116
|
+
error: Error;
|
|
2117
|
+
response: any;
|
|
2118
|
+
}) => void
|
|
2119
|
+
) {
|
|
2064
2120
|
if (typeof successFn !== "function" && typeof errorFn !== "function") {
|
|
2065
|
-
console.warn("[Httpx]
|
|
2121
|
+
console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
|
|
2066
2122
|
return;
|
|
2067
2123
|
}
|
|
2068
|
-
return this.context.HttpxResponseHook.
|
|
2069
|
-
successFn,
|
|
2070
|
-
errorFn
|
|
2071
|
-
);
|
|
2124
|
+
return this.context.HttpxResponseHook.add(successFn!, errorFn!);
|
|
2072
2125
|
},
|
|
2073
2126
|
/**
|
|
2074
2127
|
* 移除拦截器
|
|
2075
2128
|
* @param id 通过use返回的id
|
|
2076
2129
|
*/
|
|
2077
2130
|
eject(id: string) {
|
|
2078
|
-
return this.context.HttpxResponseHook.
|
|
2131
|
+
return this.context.HttpxResponseHook.delete(id);
|
|
2132
|
+
},
|
|
2133
|
+
/**
|
|
2134
|
+
* 移除所有拦截器
|
|
2135
|
+
*/
|
|
2136
|
+
ejectAll() {
|
|
2137
|
+
this.context.HttpxResponseHook.clearAll();
|
|
2079
2138
|
},
|
|
2080
2139
|
},
|
|
2081
2140
|
};
|