kbfetch 0.0.21 → 0.0.26
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/dist/index.js +11 -10
- package/dist/typings/help.d.ts +0 -1
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -8,13 +8,6 @@ var help = {
|
|
|
8
8
|
const fullQueryStr = [originalQueryStr, queryStr].filter(Boolean).join("&");
|
|
9
9
|
return [pre, fullQueryStr].filter(Boolean).join("?");
|
|
10
10
|
},
|
|
11
|
-
createTimeoutAbortController: (timeout) => {
|
|
12
|
-
const abortController = new AbortController;
|
|
13
|
-
timeout > 0 && setTimeout(() => {
|
|
14
|
-
abortController.abort();
|
|
15
|
-
}, timeout);
|
|
16
|
-
return abortController;
|
|
17
|
-
},
|
|
18
11
|
parseResBody: (res) => {
|
|
19
12
|
const contentType = res.headers.get("content-type") || "";
|
|
20
13
|
if (contentType.includes("application/json"))
|
|
@@ -41,13 +34,21 @@ var defaultKbFetch = {
|
|
|
41
34
|
let { before = _t.before, ..._init } = init || {};
|
|
42
35
|
_init = before(_init);
|
|
43
36
|
let { parser = _t.parser, after = _t.after, baseUrl = _t.baseUrl, timeout = _t.timeout, canAbort, headers, ...requestInit } = _init;
|
|
44
|
-
let abortController
|
|
45
|
-
|
|
37
|
+
let abortController;
|
|
38
|
+
let timeoutIns;
|
|
39
|
+
if (timeout > 0 || canAbort) {
|
|
40
|
+
abortController = new AbortController;
|
|
41
|
+
abortController.signal.onabort = () => clearTimeout(timeoutIns);
|
|
42
|
+
timeoutIns = setTimeout(() => {
|
|
43
|
+
abortController.abort();
|
|
44
|
+
}, timeout);
|
|
45
|
+
return abortController;
|
|
46
|
+
}
|
|
46
47
|
abortController && Object.assign(requestInit, { signal: abortController.signal });
|
|
47
48
|
headers && Object.assign(requestInit, { headers: help_default.obj2header(headers) });
|
|
48
49
|
if (!url.startsWith("http://") && !url.startsWith("https://"))
|
|
49
50
|
url = baseUrl + url;
|
|
50
|
-
const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) }));
|
|
51
|
+
const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) })).finally(() => clearTimeout(timeoutIns));
|
|
51
52
|
return Object.assign(after(response), {
|
|
52
53
|
abort: () => abortController?.abort()
|
|
53
54
|
});
|
package/dist/typings/help.d.ts
CHANGED
|
@@ -30,7 +30,6 @@ export interface DefaultKbFetch {
|
|
|
30
30
|
}
|
|
31
31
|
declare const help: {
|
|
32
32
|
synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => string;
|
|
33
|
-
createTimeoutAbortController: (timeout: number) => AbortController;
|
|
34
33
|
parseResBody: (res: Response) => Promise<any>;
|
|
35
34
|
obj2header: (obj: Record<string, string>) => Headers;
|
|
36
35
|
};
|
package/package.json
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
"name": "kbfetch",
|
|
3
3
|
"main": "./dist/index.js",
|
|
4
4
|
"module": "./dist/index.js",
|
|
5
|
+
"description": "一个轻量基于fetch封装的API请求工具库",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"fetch",
|
|
7
8
|
"ajax"
|
|
8
9
|
],
|
|
9
10
|
"type": "module",
|
|
10
|
-
"version": "0.0.
|
|
11
|
+
"version": "0.0.26",
|
|
11
12
|
"license": "MIT",
|
|
12
13
|
"types": "./dist/typings/index.d.ts",
|
|
13
14
|
"files": [
|
|
@@ -16,9 +17,9 @@
|
|
|
16
17
|
"scripts": {
|
|
17
18
|
"pub": "bun bld && bun tsc && bun patch && npm publish --registry https://registry.npmjs.org/ ; bun updatetaobao ",
|
|
18
19
|
"patch": "git stash && npm version patch && git stash pop",
|
|
20
|
+
"updatetaobao": "cnpm sync kbfetch",
|
|
19
21
|
"tsc": "tsc index.ts -d --emitDeclarationOnly --declarationDir ./dist/typings",
|
|
20
|
-
"bld": "bun build ./index.ts --outdir ./dist"
|
|
21
|
-
"updatetaobao": "cnpm sync kbfetch"
|
|
22
|
+
"bld": "bun build ./index.ts --outdir ./dist"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
25
|
"bun-types": "latest",
|