@whitesev/utils 1.4.8 → 1.4.9
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 +48 -14
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +48 -14
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +48 -14
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +48 -14
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +48 -14
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +48 -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 +69 -19
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: () => void, errorFn: (
|
|
1160
|
+
use(successFn: (response: HttpxAsyncResultData) => void, errorFn: (data: {
|
|
1161
|
+
type: "onerror" | "ontimeout";
|
|
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
|
};
|
|
@@ -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";
|
|
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
|
};
|
|
@@ -1645,6 +1649,15 @@ class Httpx {
|
|
|
1645
1649
|
if (response.length) {
|
|
1646
1650
|
response = response[0];
|
|
1647
1651
|
}
|
|
1652
|
+
if (
|
|
1653
|
+
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
1654
|
+
type: "onerror",
|
|
1655
|
+
error: new TypeError("request error"),
|
|
1656
|
+
response: response,
|
|
1657
|
+
}) == null
|
|
1658
|
+
) {
|
|
1659
|
+
return;
|
|
1660
|
+
}
|
|
1648
1661
|
resolve({
|
|
1649
1662
|
status: false,
|
|
1650
1663
|
data: response,
|
|
@@ -1669,6 +1682,16 @@ class Httpx {
|
|
|
1669
1682
|
} else if ("ontimeout" in this.context.#defaultDetails) {
|
|
1670
1683
|
this.context.#defaultDetails!.ontimeout!.apply(this, argumentsList);
|
|
1671
1684
|
}
|
|
1685
|
+
|
|
1686
|
+
if (
|
|
1687
|
+
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
1688
|
+
type: "ontimeout",
|
|
1689
|
+
error: new TypeError("request timeout"),
|
|
1690
|
+
response: (argumentsList || [null])[0],
|
|
1691
|
+
}) == null
|
|
1692
|
+
) {
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1672
1695
|
resolve({
|
|
1673
1696
|
status: false,
|
|
1674
1697
|
data: [...argumentsList],
|
|
@@ -1755,8 +1778,15 @@ class Httpx {
|
|
|
1755
1778
|
) {
|
|
1756
1779
|
Response["finalUrl"] = (Response as any)["responseURL"];
|
|
1757
1780
|
}
|
|
1781
|
+
|
|
1758
1782
|
/* 状态码2xx都是成功的 */
|
|
1759
1783
|
if (Math.floor(Response.status / 100) === 2) {
|
|
1784
|
+
if (
|
|
1785
|
+
this.context.HttpxResponseHook.successResponseCallBack(Response) ==
|
|
1786
|
+
null
|
|
1787
|
+
) {
|
|
1788
|
+
return;
|
|
1789
|
+
}
|
|
1760
1790
|
resolve({
|
|
1761
1791
|
status: true,
|
|
1762
1792
|
data: Response,
|
|
@@ -2038,14 +2068,24 @@ class Httpx {
|
|
|
2038
2068
|
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
2039
2069
|
*/
|
|
2040
2070
|
use(fn: (details: Required<HttpxDetails>) => void) {
|
|
2041
|
-
|
|
2071
|
+
if (typeof fn !== "function") {
|
|
2072
|
+
console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
|
|
2073
|
+
return;
|
|
2074
|
+
}
|
|
2075
|
+
return this.context.HttpxRequestHook.add(fn);
|
|
2042
2076
|
},
|
|
2043
2077
|
/**
|
|
2044
2078
|
* 移除拦截器
|
|
2045
2079
|
* @param id 通过use返回的id
|
|
2046
2080
|
*/
|
|
2047
2081
|
eject(id: string) {
|
|
2048
|
-
return this.context.HttpxRequestHook.
|
|
2082
|
+
return this.context.HttpxRequestHook.delete(id);
|
|
2083
|
+
},
|
|
2084
|
+
/**
|
|
2085
|
+
* 移除所有拦截器
|
|
2086
|
+
*/
|
|
2087
|
+
ejectAll() {
|
|
2088
|
+
this.context.HttpxRequestHook.clearAll();
|
|
2049
2089
|
},
|
|
2050
2090
|
},
|
|
2051
2091
|
/**
|
|
@@ -2060,22 +2100,32 @@ class Httpx {
|
|
|
2060
2100
|
* @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
2061
2101
|
* + 超出 2xx 范围的状态码都会触发该函数
|
|
2062
2102
|
*/
|
|
2063
|
-
use(
|
|
2103
|
+
use(
|
|
2104
|
+
successFn: (response: HttpxAsyncResultData) => void,
|
|
2105
|
+
errorFn: (data: {
|
|
2106
|
+
type: "onerror" | "ontimeout";
|
|
2107
|
+
error: Error;
|
|
2108
|
+
response: any;
|
|
2109
|
+
}) => void
|
|
2110
|
+
) {
|
|
2064
2111
|
if (typeof successFn !== "function" && typeof errorFn !== "function") {
|
|
2065
|
-
console.warn("[Httpx] 请传入拦截器函数");
|
|
2112
|
+
console.warn("[Httpx-interceptors-response] 请传入拦截器函数");
|
|
2066
2113
|
return;
|
|
2067
2114
|
}
|
|
2068
|
-
return this.context.HttpxResponseHook.
|
|
2069
|
-
successFn,
|
|
2070
|
-
errorFn
|
|
2071
|
-
);
|
|
2115
|
+
return this.context.HttpxResponseHook.add(successFn, errorFn);
|
|
2072
2116
|
},
|
|
2073
2117
|
/**
|
|
2074
2118
|
* 移除拦截器
|
|
2075
2119
|
* @param id 通过use返回的id
|
|
2076
2120
|
*/
|
|
2077
2121
|
eject(id: string) {
|
|
2078
|
-
return this.context.HttpxResponseHook.
|
|
2122
|
+
return this.context.HttpxResponseHook.delete(id);
|
|
2123
|
+
},
|
|
2124
|
+
/**
|
|
2125
|
+
* 移除所有拦截器
|
|
2126
|
+
*/
|
|
2127
|
+
ejectAll() {
|
|
2128
|
+
this.context.HttpxResponseHook.clearAll();
|
|
2079
2129
|
},
|
|
2080
2130
|
},
|
|
2081
2131
|
};
|