kbfetch 1.1.0 → 2.1.1

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
  };
@@ -65,12 +73,13 @@ var defaultKbFetch = {
65
73
  url = help_default.synthesizeUrlWithParams(url, requestInit.params);
66
74
  if (requestInit.body && requestInit.body.constructor.name === "Object" && requestInit.headers?.["Content-Type"] !== "application/json") {
67
75
  requestInit.headers ||= {};
68
- requestInit.headers["Content-Type"] == "application/json";
76
+ requestInit.headers["Content-Type"] = "application/json";
69
77
  baseBody && help_default.objSetDefVal(requestInit, "body", baseBody);
70
78
  requestInit.body = JSON.stringify(requestInit.body);
71
79
  }
72
80
  requestInit.headers && (requestInit.headers = help_default.obj2header(requestInit.headers));
73
- url = help_default.urlWithProtocol(url) ? url : help_default.addProtocol(baseUrl) + url;
81
+ url = help_default.urlWithDomain(url) ? url : (baseUrl || "") + url;
82
+ url = help_default.addProtocol(url) || url;
74
83
  const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) })).finally(() => clearTimeout(timeoutIns));
75
84
  return Object.assign(after(response, { url, init }), {
76
85
  abort: () => abortController?.abort()
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "kbfetch",
3
- "version": "1.1.0",
3
+ "version": "2.1.1",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "devDependencies": {