kbfetch 1.0.6 → 1.0.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.
- package/README.md +1 -2
- package/dist/index.js +7 -5
- package/dist/typings/help.d.ts +4 -1
- package/dist/typings/index.d.ts +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,7 +37,6 @@ kbfetch.g('https://baidu.com', { method: 'PATCH' })
|
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
取消请求示例:
|
|
40
|
-
`timeout!=-1时,canAbort可以不设置`
|
|
41
40
|
|
|
42
41
|
```js
|
|
43
42
|
const ft = kbfetch.post('https://api.example.com/data', { id: 1 }, { canAbort: true })
|
|
@@ -45,7 +44,7 @@ ft.abort()
|
|
|
45
44
|
```
|
|
46
45
|
|
|
47
46
|
`or`
|
|
48
|
-
|
|
47
|
+
`timeout>=0`
|
|
49
48
|
```js
|
|
50
49
|
const ft = kbfetch.g('https://api.example.com/data', { params: { id: 1 }, timeout: 0 })
|
|
51
50
|
ft.abort()
|
package/dist/index.js
CHANGED
|
@@ -54,10 +54,12 @@ var defaultKbFetch = {
|
|
|
54
54
|
if (timeout > 0 || canAbort) {
|
|
55
55
|
abortController = new AbortController;
|
|
56
56
|
Object.assign(requestInit, { signal: abortController.signal });
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
if (timeout >= 0) {
|
|
58
|
+
abortController.signal.onabort = () => clearTimeout(timeoutIns);
|
|
59
|
+
timeoutIns = setTimeout(() => {
|
|
60
|
+
abortController.abort();
|
|
61
|
+
}, timeout);
|
|
62
|
+
}
|
|
61
63
|
}
|
|
62
64
|
if (requestInit.params)
|
|
63
65
|
url = help_default.synthesizeUrlWithParams(url, requestInit.params);
|
|
@@ -70,7 +72,7 @@ var defaultKbFetch = {
|
|
|
70
72
|
requestInit.headers && (requestInit.headers = help_default.obj2header(requestInit.headers));
|
|
71
73
|
url = help_default.urlWithProtocol(url) ? url : help_default.addProtocol(baseUrl) + url;
|
|
72
74
|
const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) })).finally(() => clearTimeout(timeoutIns));
|
|
73
|
-
return Object.assign(after(response), {
|
|
75
|
+
return Object.assign(after(response, { url, init }), {
|
|
74
76
|
abort: () => abortController?.abort()
|
|
75
77
|
});
|
|
76
78
|
},
|
package/dist/typings/help.d.ts
CHANGED
|
@@ -18,7 +18,10 @@ export interface DefaultKbFetch {
|
|
|
18
18
|
before: <T extends any>(init: T) => T | void;
|
|
19
19
|
after: (res: Promise<Response & {
|
|
20
20
|
data: any;
|
|
21
|
-
}
|
|
21
|
+
}>, req: {
|
|
22
|
+
url: string;
|
|
23
|
+
init: KbFetchInit | undefined;
|
|
24
|
+
}) => any;
|
|
22
25
|
parser: (res: Response) => any;
|
|
23
26
|
timeout: number;
|
|
24
27
|
do: <T>(url: string, init?: KbFetchInit) => Res<T>;
|
package/dist/typings/index.d.ts
CHANGED
|
@@ -2,7 +2,10 @@ import { DefaultKbFetch } from './help';
|
|
|
2
2
|
export declare const createKbFetch: (config: Partial<Pick<DefaultKbFetch, "baseUrl" | "baseParams" | "baseBody" | "timeout" | "before" | "parser" | "after">>) => {
|
|
3
3
|
after: (res: Promise<Response & {
|
|
4
4
|
data: any;
|
|
5
|
-
}
|
|
5
|
+
}>, req: {
|
|
6
|
+
url: string;
|
|
7
|
+
init: import("./help").KbFetchInit | undefined;
|
|
8
|
+
}) => any;
|
|
6
9
|
before: <T extends any>(init: T) => T | void;
|
|
7
10
|
timeout: number;
|
|
8
11
|
baseUrl?: string;
|