kbfetch 2.1.0 → 2.1.2-beta.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.
package/dist/index.js CHANGED
@@ -29,8 +29,16 @@ var help = {
29
29
  });
30
30
  return headers;
31
31
  },
32
+ urlWithDomain: (url) => url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//"),
32
33
  urlWithProtocol: (url) => url.startsWith("http://") || url.startsWith("https://"),
33
- addProtocol: (url) => !url || !globalThis.location || help.urlWithProtocol(url) ? url : location.protocol + url,
34
+ addProtocol: (url) => {
35
+ if (!url)
36
+ return url;
37
+ if (help.urlWithProtocol(url))
38
+ return url;
39
+ const defProtocol = location?.protocol || "https:";
40
+ return defProtocol + url;
41
+ },
34
42
  objPick: (obj, keys) => Object.fromEntries(keys.map((key) => [key, obj[key]])),
35
43
  objSetDefVal: (obj, key, def) => def && (obj[key] = Object.assign({}, def, obj[key]))
36
44
  };
@@ -48,33 +56,37 @@ var defaultKbFetch = {
48
56
  const _init = Object.assign(defOpts, init);
49
57
  help_default.objSetDefVal(_init, "headers", baseHeaders);
50
58
  help_default.objSetDefVal(_init, "params", baseParams);
51
- let { parser, after, timeout, canAbort, ...requestInit } = _init.before(_init) || _init;
52
- let abortController;
53
- let timeoutIns;
54
- if (timeout > 0 || canAbort) {
55
- abortController = new AbortController;
56
- Object.assign(requestInit, { signal: abortController.signal });
57
- if (timeout >= 0) {
58
- abortController.signal.onabort = () => clearTimeout(timeoutIns);
59
- timeoutIns = setTimeout(() => {
60
- abortController.abort();
61
- }, timeout);
59
+ const core = async () => {
60
+ let { parser, after, timeout, canAbort, ...requestInit } = await _init.before(_init) || _init;
61
+ let abortController;
62
+ let timeoutIns;
63
+ if (timeout > 0 || canAbort) {
64
+ abortController = new AbortController;
65
+ Object.assign(requestInit, { signal: abortController.signal });
66
+ if (timeout >= 0) {
67
+ abortController.signal.onabort = () => clearTimeout(timeoutIns);
68
+ timeoutIns = setTimeout(() => {
69
+ abortController.abort();
70
+ }, timeout);
71
+ }
62
72
  }
63
- }
64
- if (requestInit.params)
65
- url = help_default.synthesizeUrlWithParams(url, requestInit.params);
66
- if (requestInit.body && requestInit.body.constructor.name === "Object" && requestInit.headers?.["Content-Type"] !== "application/json") {
67
- requestInit.headers ||= {};
68
- requestInit.headers["Content-Type"] = "application/json";
69
- baseBody && help_default.objSetDefVal(requestInit, "body", baseBody);
70
- requestInit.body = JSON.stringify(requestInit.body);
71
- }
72
- requestInit.headers && (requestInit.headers = help_default.obj2header(requestInit.headers));
73
- url = help_default.urlWithProtocol(url) ? url : help_default.addProtocol(baseUrl) + url;
74
- const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) })).finally(() => clearTimeout(timeoutIns));
75
- return Object.assign(after(response, { url, init }), {
76
- abort: () => abortController?.abort()
77
- });
73
+ if (requestInit.params)
74
+ url = help_default.synthesizeUrlWithParams(url, requestInit.params);
75
+ if (requestInit.body && requestInit.body.constructor.name === "Object" && requestInit.headers?.["Content-Type"] !== "application/json") {
76
+ requestInit.headers ||= {};
77
+ requestInit.headers["Content-Type"] = "application/json";
78
+ baseBody && help_default.objSetDefVal(requestInit, "body", baseBody);
79
+ requestInit.body = JSON.stringify(requestInit.body);
80
+ }
81
+ requestInit.headers && (requestInit.headers = help_default.obj2header(requestInit.headers));
82
+ url = help_default.urlWithDomain(url) ? url : (baseUrl || "") + url;
83
+ url = help_default.addProtocol(url) || url;
84
+ const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) })).finally(() => clearTimeout(timeoutIns));
85
+ return Object.assign(after(response, { url, init }), {
86
+ abort: () => abortController?.abort()
87
+ });
88
+ };
89
+ return core();
78
90
  },
79
91
  get: function(url, params, init) {
80
92
  params && (init ||= {}, init.params = params);
@@ -37,6 +37,7 @@ declare const help: {
37
37
  synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => string;
38
38
  parseResBody: (res: Response) => Promise<any>;
39
39
  obj2header: (obj: Object) => Headers;
40
+ urlWithDomain: (url: string) => boolean;
40
41
  urlWithProtocol: (url: string) => boolean;
41
42
  addProtocol: (url?: string) => string;
42
43
  objPick: <T extends Object, K extends keyof T>(obj: T, keys: K[]) => Pick<T, K>;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "kbfetch",
3
- "version": "2.1.0",
3
+ "version": "2.1.2-beta.0",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "devDependencies": {
7
7
  "@types/node": "^20.14.10",
8
8
  "bun-types": "latest",
9
- "typescript": "^5.5.3"
9
+ "typescript": "^6.0.3"
10
10
  },
11
11
  "peerDependencies": {
12
12
  "typescript": "^5.0.0"
@@ -21,12 +21,12 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "scripts": {
24
- "pub": "bun bld && bun tsc && npm publish --registry https://registry.npmjs.org/ ",
24
+ "pub": "bun bld && bun tsc && npm publish --tag beta --registry https://registry.npmjs.org/ ",
25
25
  "patch": "git stash && npm version patch && git stash pop",
26
26
  "updatetaobao": "cnpm sync kbfetch",
27
- "tsc": "tsc index.ts -d --emitDeclarationOnly --declarationDir ./dist/typings",
27
+ "tsc": "tsc -d --emitDeclarationOnly --declarationDir ./dist/typings",
28
28
  "bld": "bun build ./index.ts --outdir ./dist",
29
- "t":"bun t.ts"
29
+ "t": "bun t.ts"
30
30
  },
31
31
  "type": "module",
32
32
  "types": "./dist/typings/index.d.ts"