@whitesev/utils 1.4.9 → 1.5.1

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.
@@ -1105,6 +1105,11 @@ export declare interface HttpxAsyncResult<T = HttpxDetails> {
1105
1105
  */
1106
1106
  type: HttpxResponseCallBackType;
1107
1107
  }
1108
+ export declare interface HttpxHookErrorData {
1109
+ type: "onerror" | "ontimeout" | "onabort";
1110
+ error: Error;
1111
+ response: any;
1112
+ }
1108
1113
  declare class Httpx {
1109
1114
  #private;
1110
1115
  private GM_Api;
@@ -1134,7 +1139,7 @@ declare class Httpx {
1134
1139
  * 添加拦截器
1135
1140
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
1136
1141
  */
1137
- use(fn: (details: Required<HttpxDetails>) => void): string | undefined;
1142
+ use(fn: <T = Required<HttpxDetails>>(details: T) => void | T): string | undefined;
1138
1143
  /**
1139
1144
  * 移除拦截器
1140
1145
  * @param id 通过use返回的id
@@ -1157,11 +1162,7 @@ declare class Httpx {
1157
1162
  * @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
1158
1163
  * + 超出 2xx 范围的状态码都会触发该函数
1159
1164
  */
1160
- use(successFn: (response: HttpxAsyncResultData) => void, errorFn: (data: {
1161
- type: "onerror" | "ontimeout";
1162
- error: Error;
1163
- response: any;
1164
- }) => void): string | undefined;
1165
+ use(successFn?: (<T_1 = HttpxAsyncResultData<HttpxDetails>>(response: HttpxAsyncResultData) => void | T_1) | undefined, errorFn?: (<T_2 = HttpxHookErrorData>(data: T_2) => void | T_2) | undefined): string | undefined;
1165
1166
  /**
1166
1167
  * 移除拦截器
1167
1168
  * @param id 通过use返回的id
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.4.9",
3
+ "version": "1.5.1",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
package/src/Httpx.ts CHANGED
@@ -1202,6 +1202,12 @@ export declare interface HttpxAsyncResult<T = HttpxDetails> {
1202
1202
  type: HttpxResponseCallBackType;
1203
1203
  }
1204
1204
 
1205
+ export declare interface HttpxHookErrorData {
1206
+ type: "onerror" | "ontimeout" | "onabort";
1207
+ error: Error;
1208
+ response: any;
1209
+ }
1210
+
1205
1211
  const GenerateUUID = () => {
1206
1212
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
1207
1213
  var r = (Math.random() * 16) | 0,
@@ -1292,8 +1298,8 @@ class Httpx {
1292
1298
  configList: <
1293
1299
  {
1294
1300
  id: string;
1295
- successFn: Function;
1296
- errorFn: Function;
1301
+ successFn?: Function;
1302
+ errorFn?: Function;
1297
1303
  }[]
1298
1304
  >[],
1299
1305
  },
@@ -1317,7 +1323,7 @@ class Httpx {
1317
1323
  * @param response
1318
1324
  */
1319
1325
  errorResponseCallBack(data: {
1320
- type: "onerror" | "ontimeout";
1326
+ type: "onerror" | "ontimeout" | "onabort";
1321
1327
  error: Error;
1322
1328
  response: any;
1323
1329
  }) {
@@ -1334,7 +1340,7 @@ class Httpx {
1334
1340
  /**
1335
1341
  * 添加请求前的回调处理配置
1336
1342
  */
1337
- add(successFn: Function, errorFn: Function) {
1343
+ add(successFn?: Function, errorFn?: Function) {
1338
1344
  let id = GenerateUUID();
1339
1345
  this.$config.configList.push({
1340
1346
  id: id,
@@ -1621,6 +1627,15 @@ class Httpx {
1621
1627
  } else if ("onabort" in this.context.#defaultDetails) {
1622
1628
  this.context.#defaultDetails!.onabort!.apply(this, argumentsList);
1623
1629
  }
1630
+ if (
1631
+ this.context.HttpxResponseHook.errorResponseCallBack({
1632
+ type: "onabort",
1633
+ error: new TypeError("request canceled"),
1634
+ response: null,
1635
+ }) == null
1636
+ ) {
1637
+ return;
1638
+ }
1624
1639
  resolve({
1625
1640
  status: false,
1626
1641
  data: [...argumentsList],
@@ -2067,7 +2082,7 @@ class Httpx {
2067
2082
  * 添加拦截器
2068
2083
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2069
2084
  */
2070
- use(fn: (details: Required<HttpxDetails>) => void) {
2085
+ use(fn: <T = Required<HttpxDetails>>(details: T) => void | T) {
2071
2086
  if (typeof fn !== "function") {
2072
2087
  console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
2073
2088
  return;
@@ -2101,18 +2116,16 @@ class Httpx {
2101
2116
  * + 超出 2xx 范围的状态码都会触发该函数
2102
2117
  */
2103
2118
  use(
2104
- successFn: (response: HttpxAsyncResultData) => void,
2105
- errorFn: (data: {
2106
- type: "onerror" | "ontimeout";
2107
- error: Error;
2108
- response: any;
2109
- }) => void
2119
+ successFn?: <T = HttpxAsyncResultData>(
2120
+ response: HttpxAsyncResultData
2121
+ ) => void | T,
2122
+ errorFn?: <T = HttpxHookErrorData>(data: T) => void | T
2110
2123
  ) {
2111
2124
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2112
- console.warn("[Httpx-interceptors-response] 请传入拦截器函数");
2125
+ console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
2113
2126
  return;
2114
2127
  }
2115
- return this.context.HttpxResponseHook.add(successFn, errorFn);
2128
+ return this.context.HttpxResponseHook.add(successFn!, errorFn!);
2116
2129
  },
2117
2130
  /**
2118
2131
  * 移除拦截器