xxf_react 0.1.7 → 0.1.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.
@@ -1 +1 @@
1
- {"version":3,"file":"Fetch.d.ts","sourceRoot":"","sources":["../../src/fetch/Fetch.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAsB,gBAAgB,CAClC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,GAAE,CAAC,WAAW,GAAG;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,CAAM,GAChD,OAAO,CAAC,QAAQ,CAAC,CAkDnB"}
1
+ {"version":3,"file":"Fetch.d.ts","sourceRoot":"","sources":["../../src/fetch/Fetch.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAsB,gBAAgB,CAClC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,GAAE,CAAC,WAAW,GAAG;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,CAAM,GAChD,OAAO,CAAC,QAAQ,CAAC,CAqDnB"}
@@ -1,3 +1,4 @@
1
+ import { TimeoutError } from "./TimeoutError";
1
2
  /**
2
3
  * 带超时时间的请求,默认没有超时时间限制
3
4
  * @param input
@@ -37,8 +38,12 @@ export async function fetchWithTimeout(input, init = {}) {
37
38
  ? input.url
38
39
  : input.toString();
39
40
  console.error(`[http timeout] ${url} (${timeout}ms)`);
41
+ // 抛自定义超时错误
42
+ throw new TimeoutError(timeout, url);
43
+ }
44
+ else {
45
+ throw e;
40
46
  }
41
- throw e;
42
47
  }
43
48
  finally {
44
49
  clearTimeout(timer);
@@ -0,0 +1,7 @@
1
+ export declare class TimeoutError extends Error {
2
+ timeout: number;
3
+ url: string;
4
+ constructor(timeout: number, url: string);
5
+ private static extractLastTwoSegments;
6
+ }
7
+ //# sourceMappingURL=TimeoutError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TimeoutError.d.ts","sourceRoot":"","sources":["../../src/fetch/TimeoutError.ts"],"names":[],"mappings":"AACA,qBAAa,YAAa,SAAQ,KAAK;IAChB,OAAO,EAAE,MAAM;IAAS,GAAG,EAAE,MAAM;gBAAnC,OAAO,EAAE,MAAM,EAAS,GAAG,EAAE,MAAM;IAQtD,OAAO,CAAC,MAAM,CAAC,sBAAsB;CAWxC"}
@@ -0,0 +1,23 @@
1
+ /// 超时错误封装
2
+ export class TimeoutError extends Error {
3
+ constructor(timeout, url) {
4
+ // 提取最后两级路径
5
+ const path = TimeoutError.extractLastTwoSegments(url);
6
+ super(`Request timed out ${timeout}ms: ${path}`);
7
+ this.timeout = timeout;
8
+ this.url = url;
9
+ this.name = 'TimeoutError';
10
+ }
11
+ static extractLastTwoSegments(url) {
12
+ try {
13
+ const u = new URL(url, window.location.origin); // 支持相对路径
14
+ const segments = u.pathname.split('/').filter(Boolean); // 去掉空段
15
+ const lastTwo = segments.slice(-2); // 取最后两段
16
+ return '/' + lastTwo.join('/');
17
+ }
18
+ catch {
19
+ // 如果不是合法 URL,直接返回原始字符串
20
+ return url;
21
+ }
22
+ }
23
+ }
@@ -1,2 +1,3 @@
1
1
  export * from './Fetch';
2
+ export * from './TimeoutError';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC"}
@@ -1 +1,2 @@
1
1
  export * from './Fetch';
2
+ export * from './TimeoutError';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xxf_react",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "private": false,
5
5
 
6
6
  "type": "module",