xxf_react 0.1.6 → 0.1.8

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.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 带超时时间的请求,默认没有超时时间限制
3
+ * @param input
4
+ * @param init
5
+ */
6
+ export declare function fetchWithTimeout(input: string | URL | Request, init?: (RequestInit & {
7
+ timeout?: number;
8
+ })): Promise<Response>;
9
+ //# sourceMappingURL=Fetch.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * 带超时时间的请求,默认没有超时时间限制
3
+ * @param input
4
+ * @param init
5
+ */
6
+ export async function fetchWithTimeout(input, init = {}) {
7
+ const { timeout, signal, ...rest } = init;
8
+ if (timeout == null) {
9
+ return fetch(input, init);
10
+ }
11
+ const controller = new AbortController();
12
+ let timeoutFired = false;
13
+ const onAbort = () => controller.abort();
14
+ if (signal) {
15
+ if (signal.aborted) {
16
+ controller.abort();
17
+ }
18
+ else {
19
+ signal.addEventListener('abort', onAbort, { once: true });
20
+ }
21
+ }
22
+ const timer = setTimeout(() => {
23
+ timeoutFired = true;
24
+ controller.abort();
25
+ }, timeout);
26
+ try {
27
+ return await fetch(input, {
28
+ ...rest,
29
+ signal: controller.signal,
30
+ });
31
+ }
32
+ catch (e) {
33
+ if (e.name === 'AbortError' && timeoutFired) {
34
+ const url = typeof input === 'string'
35
+ ? input
36
+ : input instanceof Request
37
+ ? input.url
38
+ : input.toString();
39
+ console.error(`[http timeout] ${url} (${timeout}ms)`);
40
+ }
41
+ throw e;
42
+ }
43
+ finally {
44
+ clearTimeout(timer);
45
+ if (signal) {
46
+ signal.removeEventListener('abort', onAbort);
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Fetch';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './Fetch';
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './sse';
2
+ export * from './fetch';
2
3
  export * from './utils';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './sse';
2
+ export * from './fetch';
2
3
  export * from './utils';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xxf_react",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "private": false,
5
5
 
6
6
  "type": "module",