@whitesev/utils 1.4.6 → 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/package.json +1 -1
- package/src/Httpx.ts +207 -8
package/dist/src/Httpx.d.ts
CHANGED
|
@@ -1018,12 +1018,6 @@ export declare interface HttpxDetailsConfig extends HttpxDetails {
|
|
|
1018
1018
|
* (可选)是否输出请求配置
|
|
1019
1019
|
*/
|
|
1020
1020
|
logDetails?: boolean;
|
|
1021
|
-
/**
|
|
1022
|
-
* 发送请求前的回调
|
|
1023
|
-
* 如果返回false则阻止本次返回
|
|
1024
|
-
* @param details 当前的请求配置
|
|
1025
|
-
*/
|
|
1026
|
-
beforeRequestCallBack?(details: HttpxDetails): boolean | void;
|
|
1027
1021
|
}
|
|
1028
1022
|
/**
|
|
1029
1023
|
* 响应的数据的data
|
|
@@ -1115,6 +1109,7 @@ declare class Httpx {
|
|
|
1115
1109
|
#private;
|
|
1116
1110
|
private GM_Api;
|
|
1117
1111
|
private HttpxRequestHook;
|
|
1112
|
+
private HttpxResponseHook;
|
|
1118
1113
|
private HttpxRequestDetails;
|
|
1119
1114
|
private HttpxCallBack;
|
|
1120
1115
|
private HttpxRequest;
|
|
@@ -1126,6 +1121,46 @@ declare class Httpx {
|
|
|
1126
1121
|
config(details?: {
|
|
1127
1122
|
[K in keyof HttpxDetailsConfig]?: HttpxDetailsConfig[K];
|
|
1128
1123
|
}): void;
|
|
1124
|
+
/**
|
|
1125
|
+
* 拦截器
|
|
1126
|
+
*/
|
|
1127
|
+
interceptors: {
|
|
1128
|
+
/**
|
|
1129
|
+
* 请求拦截器
|
|
1130
|
+
*/
|
|
1131
|
+
request: {
|
|
1132
|
+
context: Httpx;
|
|
1133
|
+
/**
|
|
1134
|
+
* 添加拦截器
|
|
1135
|
+
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
1136
|
+
*/
|
|
1137
|
+
use(fn: (details: Required<HttpxDetails>) => void): string | undefined;
|
|
1138
|
+
/**
|
|
1139
|
+
* 移除拦截器
|
|
1140
|
+
* @param id 通过use返回的id
|
|
1141
|
+
*/
|
|
1142
|
+
eject(id: string): boolean;
|
|
1143
|
+
};
|
|
1144
|
+
/**
|
|
1145
|
+
* 响应拦截器
|
|
1146
|
+
*/
|
|
1147
|
+
response: {
|
|
1148
|
+
context: Httpx;
|
|
1149
|
+
/**
|
|
1150
|
+
* 添加拦截器
|
|
1151
|
+
* @param successFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
1152
|
+
* + 2xx 范围内的状态码都会触发该函数
|
|
1153
|
+
* @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
1154
|
+
* + 超出 2xx 范围的状态码都会触发该函数
|
|
1155
|
+
*/
|
|
1156
|
+
use(successFn: () => void, errorFn: () => void): string | undefined;
|
|
1157
|
+
/**
|
|
1158
|
+
* 移除拦截器
|
|
1159
|
+
* @param id 通过use返回的id
|
|
1160
|
+
*/
|
|
1161
|
+
eject(id: string): boolean;
|
|
1162
|
+
};
|
|
1163
|
+
};
|
|
1129
1164
|
/**
|
|
1130
1165
|
* 修改xmlHttpRequest
|
|
1131
1166
|
* @param httpRequest 网络请求函数
|
package/package.json
CHANGED
package/src/Httpx.ts
CHANGED
|
@@ -1114,12 +1114,6 @@ export declare interface HttpxDetailsConfig extends HttpxDetails {
|
|
|
1114
1114
|
* (可选)是否输出请求配置
|
|
1115
1115
|
*/
|
|
1116
1116
|
logDetails?: boolean;
|
|
1117
|
-
/**
|
|
1118
|
-
* 发送请求前的回调
|
|
1119
|
-
* 如果返回false则阻止本次返回
|
|
1120
|
-
* @param details 当前的请求配置
|
|
1121
|
-
*/
|
|
1122
|
-
beforeRequestCallBack?(details: HttpxDetails): boolean | void;
|
|
1123
1117
|
}
|
|
1124
1118
|
/**
|
|
1125
1119
|
* 响应的数据的data
|
|
@@ -1208,17 +1202,165 @@ export declare interface HttpxAsyncResult<T = HttpxDetails> {
|
|
|
1208
1202
|
type: HttpxResponseCallBackType;
|
|
1209
1203
|
}
|
|
1210
1204
|
|
|
1205
|
+
const GenerateUUID = () => {
|
|
1206
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
1207
|
+
var r = (Math.random() * 16) | 0,
|
|
1208
|
+
v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
1209
|
+
return v.toString(16);
|
|
1210
|
+
});
|
|
1211
|
+
};
|
|
1212
|
+
|
|
1211
1213
|
class Httpx {
|
|
1212
1214
|
private GM_Api = {
|
|
1213
1215
|
xmlHttpRequest: null as any,
|
|
1214
1216
|
};
|
|
1215
1217
|
private HttpxRequestHook = {
|
|
1218
|
+
/**
|
|
1219
|
+
* @private
|
|
1220
|
+
*/
|
|
1221
|
+
$config: {
|
|
1222
|
+
configList: <
|
|
1223
|
+
{
|
|
1224
|
+
id: string;
|
|
1225
|
+
fn: Function;
|
|
1226
|
+
}[]
|
|
1227
|
+
>[],
|
|
1228
|
+
},
|
|
1216
1229
|
/**
|
|
1217
1230
|
* 发送请求前的回调
|
|
1218
1231
|
* 如果返回false则阻止本次返回
|
|
1219
1232
|
* @param details 当前的请求配置
|
|
1233
|
+
* @private
|
|
1234
|
+
*/
|
|
1235
|
+
beforeRequestCallBack(details: HttpxDetails) {
|
|
1236
|
+
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1237
|
+
let item = this.$config.configList[index];
|
|
1238
|
+
if (typeof item.fn === "function") {
|
|
1239
|
+
let result = item.fn(details);
|
|
1240
|
+
if (result == null) {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
return details;
|
|
1246
|
+
},
|
|
1247
|
+
/**
|
|
1248
|
+
* 添加请求前的回调处理配置
|
|
1249
|
+
*/
|
|
1250
|
+
addBeforeRequestCallBack(fn: Function) {
|
|
1251
|
+
if (typeof fn === "function") {
|
|
1252
|
+
let uuid = GenerateUUID();
|
|
1253
|
+
this.$config.configList.push({
|
|
1254
|
+
id: uuid,
|
|
1255
|
+
fn: fn,
|
|
1256
|
+
});
|
|
1257
|
+
return uuid;
|
|
1258
|
+
} else {
|
|
1259
|
+
console.warn(
|
|
1260
|
+
"HttpxRequestHook.addBeforeRequestCallBack: fn is not a function"
|
|
1261
|
+
);
|
|
1262
|
+
}
|
|
1263
|
+
},
|
|
1264
|
+
/**
|
|
1265
|
+
* 删除请求前的回调处理配置
|
|
1266
|
+
* @param id
|
|
1267
|
+
*/
|
|
1268
|
+
deleteBeforeRequestCallBack(id: string) {
|
|
1269
|
+
if (typeof id === "string") {
|
|
1270
|
+
let findIndex = this.$config.configList.findIndex(
|
|
1271
|
+
(item) => item.id === id
|
|
1272
|
+
);
|
|
1273
|
+
if (findIndex !== -1) {
|
|
1274
|
+
this.$config.configList.splice(findIndex, 1);
|
|
1275
|
+
return true;
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
return false;
|
|
1279
|
+
},
|
|
1280
|
+
/**
|
|
1281
|
+
* 清空设置的请求前的回调处理配置
|
|
1282
|
+
*/
|
|
1283
|
+
clearBeforeRequestCallBack() {
|
|
1284
|
+
this.$config.configList = [];
|
|
1285
|
+
},
|
|
1286
|
+
};
|
|
1287
|
+
private HttpxResponseHook = {
|
|
1288
|
+
/**
|
|
1289
|
+
* @private
|
|
1290
|
+
*/
|
|
1291
|
+
$config: {
|
|
1292
|
+
configList: <
|
|
1293
|
+
{
|
|
1294
|
+
id: string;
|
|
1295
|
+
successFn: Function;
|
|
1296
|
+
errorFn: Function;
|
|
1297
|
+
}[]
|
|
1298
|
+
>[],
|
|
1299
|
+
},
|
|
1300
|
+
/**
|
|
1301
|
+
* 成功的回调
|
|
1302
|
+
* @param response
|
|
1303
|
+
*/
|
|
1304
|
+
successResponseCallBack(response: any) {
|
|
1305
|
+
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1306
|
+
let item = this.$config.configList[index];
|
|
1307
|
+
if (typeof item.successFn === "function") {
|
|
1308
|
+
if (item.successFn(response) == null) {
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
return response;
|
|
1314
|
+
},
|
|
1315
|
+
/**
|
|
1316
|
+
* 失败的回调
|
|
1317
|
+
* @param response
|
|
1318
|
+
*/
|
|
1319
|
+
errorResponseCallBack(response: any) {
|
|
1320
|
+
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
1321
|
+
let item = this.$config.configList[index];
|
|
1322
|
+
if (typeof item.errorFn === "function") {
|
|
1323
|
+
if (item.errorFn(response) == null) {
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
return response;
|
|
1329
|
+
},
|
|
1330
|
+
/**
|
|
1331
|
+
* 添加请求前的回调处理配置
|
|
1332
|
+
*/
|
|
1333
|
+
addAfterResponseCallBack(successFn: Function, errorFn: Function) {
|
|
1334
|
+
let id = GenerateUUID();
|
|
1335
|
+
this.$config.configList.push({
|
|
1336
|
+
id: id,
|
|
1337
|
+
successFn: successFn,
|
|
1338
|
+
errorFn: errorFn,
|
|
1339
|
+
});
|
|
1340
|
+
return id;
|
|
1341
|
+
},
|
|
1342
|
+
/**
|
|
1343
|
+
* 删除请求前的回调处理配置
|
|
1344
|
+
* @param id
|
|
1345
|
+
*/
|
|
1346
|
+
deleteAfterResponseCallBack(id: string) {
|
|
1347
|
+
if (typeof id === "string") {
|
|
1348
|
+
let findIndex = this.$config.configList.findIndex(
|
|
1349
|
+
(item) => item.id === id
|
|
1350
|
+
);
|
|
1351
|
+
if (findIndex !== -1) {
|
|
1352
|
+
this.$config.configList.splice(findIndex, 1);
|
|
1353
|
+
return true;
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
return false;
|
|
1357
|
+
},
|
|
1358
|
+
/**
|
|
1359
|
+
* 清空设置的请求前的回调处理配置
|
|
1220
1360
|
*/
|
|
1221
|
-
|
|
1361
|
+
clearAfterResponseCallBack() {
|
|
1362
|
+
this.$config.configList = [];
|
|
1363
|
+
},
|
|
1222
1364
|
};
|
|
1223
1365
|
private HttpxRequestDetails = {
|
|
1224
1366
|
context: this,
|
|
@@ -1670,7 +1812,7 @@ class Httpx {
|
|
|
1670
1812
|
) {
|
|
1671
1813
|
let hookResult =
|
|
1672
1814
|
this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
1673
|
-
if (
|
|
1815
|
+
if (hookResult == null) {
|
|
1674
1816
|
return;
|
|
1675
1817
|
}
|
|
1676
1818
|
}
|
|
@@ -1860,6 +2002,8 @@ class Httpx {
|
|
|
1860
2002
|
"Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch"
|
|
1861
2003
|
);
|
|
1862
2004
|
}
|
|
2005
|
+
this.interceptors.request = this as any;
|
|
2006
|
+
this.interceptors.response = this as any;
|
|
1863
2007
|
this.GM_Api.xmlHttpRequest = __xmlHttpRequest__;
|
|
1864
2008
|
}
|
|
1865
2009
|
|
|
@@ -1880,6 +2024,61 @@ class Httpx {
|
|
|
1880
2024
|
}
|
|
1881
2025
|
this.#defaultDetails = Utils.assign(this.#defaultDetails, details);
|
|
1882
2026
|
}
|
|
2027
|
+
/**
|
|
2028
|
+
* 拦截器
|
|
2029
|
+
*/
|
|
2030
|
+
public interceptors = {
|
|
2031
|
+
/**
|
|
2032
|
+
* 请求拦截器
|
|
2033
|
+
*/
|
|
2034
|
+
request: {
|
|
2035
|
+
context: null as any as Httpx,
|
|
2036
|
+
/**
|
|
2037
|
+
* 添加拦截器
|
|
2038
|
+
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
2039
|
+
*/
|
|
2040
|
+
use(fn: (details: Required<HttpxDetails>) => void) {
|
|
2041
|
+
return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
|
|
2042
|
+
},
|
|
2043
|
+
/**
|
|
2044
|
+
* 移除拦截器
|
|
2045
|
+
* @param id 通过use返回的id
|
|
2046
|
+
*/
|
|
2047
|
+
eject(id: string) {
|
|
2048
|
+
return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
|
|
2049
|
+
},
|
|
2050
|
+
},
|
|
2051
|
+
/**
|
|
2052
|
+
* 响应拦截器
|
|
2053
|
+
*/
|
|
2054
|
+
response: {
|
|
2055
|
+
context: null as any as Httpx,
|
|
2056
|
+
/**
|
|
2057
|
+
* 添加拦截器
|
|
2058
|
+
* @param successFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
2059
|
+
* + 2xx 范围内的状态码都会触发该函数
|
|
2060
|
+
* @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
2061
|
+
* + 超出 2xx 范围的状态码都会触发该函数
|
|
2062
|
+
*/
|
|
2063
|
+
use(successFn: () => void, errorFn: () => void) {
|
|
2064
|
+
if (typeof successFn !== "function" && typeof errorFn !== "function") {
|
|
2065
|
+
console.warn("[Httpx] 请传入拦截器函数");
|
|
2066
|
+
return;
|
|
2067
|
+
}
|
|
2068
|
+
return this.context.HttpxResponseHook.addAfterResponseCallBack(
|
|
2069
|
+
successFn,
|
|
2070
|
+
errorFn
|
|
2071
|
+
);
|
|
2072
|
+
},
|
|
2073
|
+
/**
|
|
2074
|
+
* 移除拦截器
|
|
2075
|
+
* @param id 通过use返回的id
|
|
2076
|
+
*/
|
|
2077
|
+
eject(id: string) {
|
|
2078
|
+
return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
|
|
2079
|
+
},
|
|
2080
|
+
},
|
|
2081
|
+
};
|
|
1883
2082
|
/**
|
|
1884
2083
|
* 修改xmlHttpRequest
|
|
1885
2084
|
* @param httpRequest 网络请求函数
|