@whitesev/utils 1.4.5 → 1.4.7
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 +186 -2
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +186 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +186 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +186 -2
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +186 -2
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +186 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/src/Httpx.d.ts +41 -6
- package/dist/src/VueObject.d.ts +6 -6
- package/package.json +1 -1
- package/src/Httpx.ts +207 -8
- package/src/VueObject.ts +33 -28
package/dist/index.iife.js
CHANGED
|
@@ -1426,17 +1426,147 @@ var Utils = (function () {
|
|
|
1426
1426
|
}
|
|
1427
1427
|
}
|
|
1428
1428
|
|
|
1429
|
+
const GenerateUUID = () => {
|
|
1430
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
1431
|
+
var r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
1432
|
+
return v.toString(16);
|
|
1433
|
+
});
|
|
1434
|
+
};
|
|
1429
1435
|
class Httpx {
|
|
1430
1436
|
GM_Api = {
|
|
1431
1437
|
xmlHttpRequest: null,
|
|
1432
1438
|
};
|
|
1433
1439
|
HttpxRequestHook = {
|
|
1440
|
+
/**
|
|
1441
|
+
* @private
|
|
1442
|
+
*/
|
|
1443
|
+
$config: {
|
|
1444
|
+
configList: [],
|
|
1445
|
+
},
|
|
1434
1446
|
/**
|
|
1435
1447
|
* 发送请求前的回调
|
|
1436
1448
|
* 如果返回false则阻止本次返回
|
|
1437
1449
|
* @param details 当前的请求配置
|
|
1450
|
+
* @private
|
|
1451
|
+
*/
|
|
1452
|
+
beforeRequestCallBack(details) {
|
|
1453
|
+
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1454
|
+
let item = this.$config.configList[index];
|
|
1455
|
+
if (typeof item.fn === "function") {
|
|
1456
|
+
let result = item.fn(details);
|
|
1457
|
+
if (result == null) {
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
return details;
|
|
1463
|
+
},
|
|
1464
|
+
/**
|
|
1465
|
+
* 添加请求前的回调处理配置
|
|
1466
|
+
*/
|
|
1467
|
+
addBeforeRequestCallBack(fn) {
|
|
1468
|
+
if (typeof fn === "function") {
|
|
1469
|
+
let uuid = GenerateUUID();
|
|
1470
|
+
this.$config.configList.push({
|
|
1471
|
+
id: uuid,
|
|
1472
|
+
fn: fn,
|
|
1473
|
+
});
|
|
1474
|
+
return uuid;
|
|
1475
|
+
}
|
|
1476
|
+
else {
|
|
1477
|
+
console.warn("HttpxRequestHook.addBeforeRequestCallBack: fn is not a function");
|
|
1478
|
+
}
|
|
1479
|
+
},
|
|
1480
|
+
/**
|
|
1481
|
+
* 删除请求前的回调处理配置
|
|
1482
|
+
* @param id
|
|
1483
|
+
*/
|
|
1484
|
+
deleteBeforeRequestCallBack(id) {
|
|
1485
|
+
if (typeof id === "string") {
|
|
1486
|
+
let findIndex = this.$config.configList.findIndex((item) => item.id === id);
|
|
1487
|
+
if (findIndex !== -1) {
|
|
1488
|
+
this.$config.configList.splice(findIndex, 1);
|
|
1489
|
+
return true;
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
return false;
|
|
1493
|
+
},
|
|
1494
|
+
/**
|
|
1495
|
+
* 清空设置的请求前的回调处理配置
|
|
1496
|
+
*/
|
|
1497
|
+
clearBeforeRequestCallBack() {
|
|
1498
|
+
this.$config.configList = [];
|
|
1499
|
+
},
|
|
1500
|
+
};
|
|
1501
|
+
HttpxResponseHook = {
|
|
1502
|
+
/**
|
|
1503
|
+
* @private
|
|
1504
|
+
*/
|
|
1505
|
+
$config: {
|
|
1506
|
+
configList: [],
|
|
1507
|
+
},
|
|
1508
|
+
/**
|
|
1509
|
+
* 成功的回调
|
|
1510
|
+
* @param response
|
|
1511
|
+
*/
|
|
1512
|
+
successResponseCallBack(response) {
|
|
1513
|
+
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1514
|
+
let item = this.$config.configList[index];
|
|
1515
|
+
if (typeof item.successFn === "function") {
|
|
1516
|
+
if (item.successFn(response) == null) {
|
|
1517
|
+
return;
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
return response;
|
|
1522
|
+
},
|
|
1523
|
+
/**
|
|
1524
|
+
* 失败的回调
|
|
1525
|
+
* @param response
|
|
1526
|
+
*/
|
|
1527
|
+
errorResponseCallBack(response) {
|
|
1528
|
+
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1529
|
+
let item = this.$config.configList[index];
|
|
1530
|
+
if (typeof item.errorFn === "function") {
|
|
1531
|
+
if (item.errorFn(response) == null) {
|
|
1532
|
+
return;
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
return response;
|
|
1537
|
+
},
|
|
1538
|
+
/**
|
|
1539
|
+
* 添加请求前的回调处理配置
|
|
1438
1540
|
*/
|
|
1439
|
-
|
|
1541
|
+
addAfterResponseCallBack(successFn, errorFn) {
|
|
1542
|
+
let id = GenerateUUID();
|
|
1543
|
+
this.$config.configList.push({
|
|
1544
|
+
id: id,
|
|
1545
|
+
successFn: successFn,
|
|
1546
|
+
errorFn: errorFn,
|
|
1547
|
+
});
|
|
1548
|
+
return id;
|
|
1549
|
+
},
|
|
1550
|
+
/**
|
|
1551
|
+
* 删除请求前的回调处理配置
|
|
1552
|
+
* @param id
|
|
1553
|
+
*/
|
|
1554
|
+
deleteAfterResponseCallBack(id) {
|
|
1555
|
+
if (typeof id === "string") {
|
|
1556
|
+
let findIndex = this.$config.configList.findIndex((item) => item.id === id);
|
|
1557
|
+
if (findIndex !== -1) {
|
|
1558
|
+
this.$config.configList.splice(findIndex, 1);
|
|
1559
|
+
return true;
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
return false;
|
|
1563
|
+
},
|
|
1564
|
+
/**
|
|
1565
|
+
* 清空设置的请求前的回调处理配置
|
|
1566
|
+
*/
|
|
1567
|
+
clearAfterResponseCallBack() {
|
|
1568
|
+
this.$config.configList = [];
|
|
1569
|
+
},
|
|
1440
1570
|
};
|
|
1441
1571
|
HttpxRequestDetails = {
|
|
1442
1572
|
context: this,
|
|
@@ -1828,7 +1958,7 @@ var Utils = (function () {
|
|
|
1828
1958
|
if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
|
|
1829
1959
|
"function") {
|
|
1830
1960
|
let hookResult = this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
1831
|
-
if (
|
|
1961
|
+
if (hookResult == null) {
|
|
1832
1962
|
return;
|
|
1833
1963
|
}
|
|
1834
1964
|
}
|
|
@@ -1996,6 +2126,8 @@ var Utils = (function () {
|
|
|
1996
2126
|
if (typeof __xmlHttpRequest__ !== "function") {
|
|
1997
2127
|
console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
|
|
1998
2128
|
}
|
|
2129
|
+
this.interceptors.request = this;
|
|
2130
|
+
this.interceptors.response = this;
|
|
1999
2131
|
this.GM_Api.xmlHttpRequest = __xmlHttpRequest__;
|
|
2000
2132
|
}
|
|
2001
2133
|
/**
|
|
@@ -2008,6 +2140,58 @@ var Utils = (function () {
|
|
|
2008
2140
|
}
|
|
2009
2141
|
this.#defaultDetails = utils.assign(this.#defaultDetails, details);
|
|
2010
2142
|
}
|
|
2143
|
+
/**
|
|
2144
|
+
* 拦截器
|
|
2145
|
+
*/
|
|
2146
|
+
interceptors = {
|
|
2147
|
+
/**
|
|
2148
|
+
* 请求拦截器
|
|
2149
|
+
*/
|
|
2150
|
+
request: {
|
|
2151
|
+
context: null,
|
|
2152
|
+
/**
|
|
2153
|
+
* 添加拦截器
|
|
2154
|
+
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
2155
|
+
*/
|
|
2156
|
+
use(fn) {
|
|
2157
|
+
return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
|
|
2158
|
+
},
|
|
2159
|
+
/**
|
|
2160
|
+
* 移除拦截器
|
|
2161
|
+
* @param id 通过use返回的id
|
|
2162
|
+
*/
|
|
2163
|
+
eject(id) {
|
|
2164
|
+
return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
|
|
2165
|
+
},
|
|
2166
|
+
},
|
|
2167
|
+
/**
|
|
2168
|
+
* 响应拦截器
|
|
2169
|
+
*/
|
|
2170
|
+
response: {
|
|
2171
|
+
context: null,
|
|
2172
|
+
/**
|
|
2173
|
+
* 添加拦截器
|
|
2174
|
+
* @param successFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
2175
|
+
* + 2xx 范围内的状态码都会触发该函数
|
|
2176
|
+
* @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
2177
|
+
* + 超出 2xx 范围的状态码都会触发该函数
|
|
2178
|
+
*/
|
|
2179
|
+
use(successFn, errorFn) {
|
|
2180
|
+
if (typeof successFn !== "function" && typeof errorFn !== "function") {
|
|
2181
|
+
console.warn("[Httpx] 请传入拦截器函数");
|
|
2182
|
+
return;
|
|
2183
|
+
}
|
|
2184
|
+
return this.context.HttpxResponseHook.addAfterResponseCallBack(successFn, errorFn);
|
|
2185
|
+
},
|
|
2186
|
+
/**
|
|
2187
|
+
* 移除拦截器
|
|
2188
|
+
* @param id 通过use返回的id
|
|
2189
|
+
*/
|
|
2190
|
+
eject(id) {
|
|
2191
|
+
return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
|
|
2192
|
+
},
|
|
2193
|
+
},
|
|
2194
|
+
};
|
|
2011
2195
|
/**
|
|
2012
2196
|
* 修改xmlHttpRequest
|
|
2013
2197
|
* @param httpRequest 网络请求函数
|