@whitesev/utils 1.4.9 → 1.5.0

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.
@@ -1157,8 +1157,8 @@ declare class Httpx {
1157
1157
  * @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
1158
1158
  * + 超出 2xx 范围的状态码都会触发该函数
1159
1159
  */
1160
- use(successFn: (response: HttpxAsyncResultData) => void, errorFn: (data: {
1161
- type: "onerror" | "ontimeout";
1160
+ use(successFn?: (response: HttpxAsyncResultData) => void, errorFn?: (data: {
1161
+ type: "onerror" | "ontimeout" | "onabort";
1162
1162
  error: Error;
1163
1163
  response: any;
1164
1164
  }) => void): string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.4.9",
3
+ "version": "1.5.0",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
package/src/Httpx.ts CHANGED
@@ -1292,8 +1292,8 @@ class Httpx {
1292
1292
  configList: <
1293
1293
  {
1294
1294
  id: string;
1295
- successFn: Function;
1296
- errorFn: Function;
1295
+ successFn?: Function;
1296
+ errorFn?: Function;
1297
1297
  }[]
1298
1298
  >[],
1299
1299
  },
@@ -1317,7 +1317,7 @@ class Httpx {
1317
1317
  * @param response
1318
1318
  */
1319
1319
  errorResponseCallBack(data: {
1320
- type: "onerror" | "ontimeout";
1320
+ type: "onerror" | "ontimeout" | "onabort";
1321
1321
  error: Error;
1322
1322
  response: any;
1323
1323
  }) {
@@ -1334,7 +1334,7 @@ class Httpx {
1334
1334
  /**
1335
1335
  * 添加请求前的回调处理配置
1336
1336
  */
1337
- add(successFn: Function, errorFn: Function) {
1337
+ add(successFn?: Function, errorFn?: Function) {
1338
1338
  let id = GenerateUUID();
1339
1339
  this.$config.configList.push({
1340
1340
  id: id,
@@ -1621,6 +1621,15 @@ class Httpx {
1621
1621
  } else if ("onabort" in this.context.#defaultDetails) {
1622
1622
  this.context.#defaultDetails!.onabort!.apply(this, argumentsList);
1623
1623
  }
1624
+ if (
1625
+ this.context.HttpxResponseHook.errorResponseCallBack({
1626
+ type: "onabort",
1627
+ error: new TypeError("request canceled"),
1628
+ response: null,
1629
+ }) == null
1630
+ ) {
1631
+ return;
1632
+ }
1624
1633
  resolve({
1625
1634
  status: false,
1626
1635
  data: [...argumentsList],
@@ -2101,18 +2110,18 @@ class Httpx {
2101
2110
  * + 超出 2xx 范围的状态码都会触发该函数
2102
2111
  */
2103
2112
  use(
2104
- successFn: (response: HttpxAsyncResultData) => void,
2105
- errorFn: (data: {
2106
- type: "onerror" | "ontimeout";
2113
+ successFn?: (response: HttpxAsyncResultData) => void,
2114
+ errorFn?: (data: {
2115
+ type: "onerror" | "ontimeout" | "onabort";
2107
2116
  error: Error;
2108
2117
  response: any;
2109
2118
  }) => void
2110
2119
  ) {
2111
2120
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2112
- console.warn("[Httpx-interceptors-response] 请传入拦截器函数");
2121
+ console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
2113
2122
  return;
2114
2123
  }
2115
- return this.context.HttpxResponseHook.add(successFn, errorFn);
2124
+ return this.context.HttpxResponseHook.add(successFn!, errorFn!);
2116
2125
  },
2117
2126
  /**
2118
2127
  * 移除拦截器