@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/index.cjs.js
CHANGED
|
@@ -1463,7 +1463,7 @@ class Httpx {
|
|
|
1463
1463
|
/**
|
|
1464
1464
|
* 添加请求前的回调处理配置
|
|
1465
1465
|
*/
|
|
1466
|
-
|
|
1466
|
+
add(fn) {
|
|
1467
1467
|
if (typeof fn === "function") {
|
|
1468
1468
|
let uuid = GenerateUUID();
|
|
1469
1469
|
this.$config.configList.push({
|
|
@@ -1480,7 +1480,7 @@ class Httpx {
|
|
|
1480
1480
|
* 删除请求前的回调处理配置
|
|
1481
1481
|
* @param id
|
|
1482
1482
|
*/
|
|
1483
|
-
|
|
1483
|
+
delete(id) {
|
|
1484
1484
|
if (typeof id === "string") {
|
|
1485
1485
|
let findIndex = this.$config.configList.findIndex((item) => item.id === id);
|
|
1486
1486
|
if (findIndex !== -1) {
|
|
@@ -1493,7 +1493,7 @@ class Httpx {
|
|
|
1493
1493
|
/**
|
|
1494
1494
|
* 清空设置的请求前的回调处理配置
|
|
1495
1495
|
*/
|
|
1496
|
-
|
|
1496
|
+
clearAll() {
|
|
1497
1497
|
this.$config.configList = [];
|
|
1498
1498
|
},
|
|
1499
1499
|
};
|
|
@@ -1523,21 +1523,21 @@ class Httpx {
|
|
|
1523
1523
|
* 失败的回调
|
|
1524
1524
|
* @param response
|
|
1525
1525
|
*/
|
|
1526
|
-
errorResponseCallBack(
|
|
1526
|
+
errorResponseCallBack(data) {
|
|
1527
1527
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1528
1528
|
let item = this.$config.configList[index];
|
|
1529
1529
|
if (typeof item.errorFn === "function") {
|
|
1530
|
-
if (item.errorFn(
|
|
1530
|
+
if (item.errorFn(data) == null) {
|
|
1531
1531
|
return;
|
|
1532
1532
|
}
|
|
1533
1533
|
}
|
|
1534
1534
|
}
|
|
1535
|
-
return
|
|
1535
|
+
return data;
|
|
1536
1536
|
},
|
|
1537
1537
|
/**
|
|
1538
1538
|
* 添加请求前的回调处理配置
|
|
1539
1539
|
*/
|
|
1540
|
-
|
|
1540
|
+
add(successFn, errorFn) {
|
|
1541
1541
|
let id = GenerateUUID();
|
|
1542
1542
|
this.$config.configList.push({
|
|
1543
1543
|
id: id,
|
|
@@ -1550,7 +1550,7 @@ class Httpx {
|
|
|
1550
1550
|
* 删除请求前的回调处理配置
|
|
1551
1551
|
* @param id
|
|
1552
1552
|
*/
|
|
1553
|
-
|
|
1553
|
+
delete(id) {
|
|
1554
1554
|
if (typeof id === "string") {
|
|
1555
1555
|
let findIndex = this.$config.configList.findIndex((item) => item.id === id);
|
|
1556
1556
|
if (findIndex !== -1) {
|
|
@@ -1563,7 +1563,7 @@ class Httpx {
|
|
|
1563
1563
|
/**
|
|
1564
1564
|
* 清空设置的请求前的回调处理配置
|
|
1565
1565
|
*/
|
|
1566
|
-
|
|
1566
|
+
clearAll() {
|
|
1567
1567
|
this.$config.configList = [];
|
|
1568
1568
|
},
|
|
1569
1569
|
};
|
|
@@ -1804,6 +1804,13 @@ class Httpx {
|
|
|
1804
1804
|
if (response.length) {
|
|
1805
1805
|
response = response[0];
|
|
1806
1806
|
}
|
|
1807
|
+
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
1808
|
+
type: "onerror",
|
|
1809
|
+
error: new TypeError("request error"),
|
|
1810
|
+
response: response,
|
|
1811
|
+
}) == null) {
|
|
1812
|
+
return;
|
|
1813
|
+
}
|
|
1807
1814
|
resolve({
|
|
1808
1815
|
status: false,
|
|
1809
1816
|
data: response,
|
|
@@ -1825,6 +1832,13 @@ class Httpx {
|
|
|
1825
1832
|
else if ("ontimeout" in this.context.#defaultDetails) {
|
|
1826
1833
|
this.context.#defaultDetails.ontimeout.apply(this, argumentsList);
|
|
1827
1834
|
}
|
|
1835
|
+
if (this.context.HttpxResponseHook.errorResponseCallBack({
|
|
1836
|
+
type: "ontimeout",
|
|
1837
|
+
error: new TypeError("request timeout"),
|
|
1838
|
+
response: (argumentsList || [null])[0],
|
|
1839
|
+
}) == null) {
|
|
1840
|
+
return;
|
|
1841
|
+
}
|
|
1828
1842
|
resolve({
|
|
1829
1843
|
status: false,
|
|
1830
1844
|
data: [...argumentsList],
|
|
@@ -1905,6 +1919,10 @@ class Httpx {
|
|
|
1905
1919
|
}
|
|
1906
1920
|
/* 状态码2xx都是成功的 */
|
|
1907
1921
|
if (Math.floor(Response.status / 100) === 2) {
|
|
1922
|
+
if (this.context.HttpxResponseHook.successResponseCallBack(Response) ==
|
|
1923
|
+
null) {
|
|
1924
|
+
return;
|
|
1925
|
+
}
|
|
1908
1926
|
resolve({
|
|
1909
1927
|
status: true,
|
|
1910
1928
|
data: Response,
|
|
@@ -2153,14 +2171,24 @@ class Httpx {
|
|
|
2153
2171
|
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
2154
2172
|
*/
|
|
2155
2173
|
use(fn) {
|
|
2156
|
-
|
|
2174
|
+
if (typeof fn !== "function") {
|
|
2175
|
+
console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
|
|
2176
|
+
return;
|
|
2177
|
+
}
|
|
2178
|
+
return this.context.HttpxRequestHook.add(fn);
|
|
2157
2179
|
},
|
|
2158
2180
|
/**
|
|
2159
2181
|
* 移除拦截器
|
|
2160
2182
|
* @param id 通过use返回的id
|
|
2161
2183
|
*/
|
|
2162
2184
|
eject(id) {
|
|
2163
|
-
return this.context.HttpxRequestHook.
|
|
2185
|
+
return this.context.HttpxRequestHook.delete(id);
|
|
2186
|
+
},
|
|
2187
|
+
/**
|
|
2188
|
+
* 移除所有拦截器
|
|
2189
|
+
*/
|
|
2190
|
+
ejectAll() {
|
|
2191
|
+
this.context.HttpxRequestHook.clearAll();
|
|
2164
2192
|
},
|
|
2165
2193
|
},
|
|
2166
2194
|
/**
|
|
@@ -2177,17 +2205,23 @@ class Httpx {
|
|
|
2177
2205
|
*/
|
|
2178
2206
|
use(successFn, errorFn) {
|
|
2179
2207
|
if (typeof successFn !== "function" && typeof errorFn !== "function") {
|
|
2180
|
-
console.warn("[Httpx] 请传入拦截器函数");
|
|
2208
|
+
console.warn("[Httpx-interceptors-response] 请传入拦截器函数");
|
|
2181
2209
|
return;
|
|
2182
2210
|
}
|
|
2183
|
-
return this.context.HttpxResponseHook.
|
|
2211
|
+
return this.context.HttpxResponseHook.add(successFn, errorFn);
|
|
2184
2212
|
},
|
|
2185
2213
|
/**
|
|
2186
2214
|
* 移除拦截器
|
|
2187
2215
|
* @param id 通过use返回的id
|
|
2188
2216
|
*/
|
|
2189
2217
|
eject(id) {
|
|
2190
|
-
return this.context.HttpxResponseHook.
|
|
2218
|
+
return this.context.HttpxResponseHook.delete(id);
|
|
2219
|
+
},
|
|
2220
|
+
/**
|
|
2221
|
+
* 移除所有拦截器
|
|
2222
|
+
*/
|
|
2223
|
+
ejectAll() {
|
|
2224
|
+
this.context.HttpxResponseHook.clearAll();
|
|
2191
2225
|
},
|
|
2192
2226
|
},
|
|
2193
2227
|
};
|