@hapaul/api 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.
Files changed (2) hide show
  1. package/dist/index.js +21 -3
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -19,9 +19,16 @@ var Client = class {
19
19
  method = requestConfig.method;
20
20
  const baseUrl = requestConfig.baseUrl ?? this.baseUrl;
21
21
  const url = baseUrl + path;
22
+ const headersOptions = requestConfig.headers;
23
+ const headers = mergeHeaders(headersOptions);
24
+ const contentType = headers.get("Content-Type");
25
+ if (!contentType) headers.set("Content-Type", "application/json");
22
26
  try {
23
27
  const {} = params;
24
- let response = await fetch(url, { method: method.toUpperCase() });
28
+ let response = await fetch(url, {
29
+ method: method.toUpperCase(),
30
+ headers
31
+ });
25
32
  response = await this.afterResponse({
26
33
  config: requestConfig,
27
34
  response
@@ -36,8 +43,8 @@ var Client = class {
36
43
  };
37
44
  if (status >= 200 && status < 300) isOk = true;
38
45
  else if (status >= 400) isOk = false;
39
- const contentType = response.headers.get("Content-Type") || "application/json";
40
- switch (contentType) {
46
+ const contentType$1 = response.headers.get("Content-Type") || "application/json";
47
+ switch (contentType$1) {
41
48
  case "application/json":
42
49
  assign(await response.json());
43
50
  break;
@@ -84,6 +91,17 @@ var Client = class {
84
91
  return response;
85
92
  }
86
93
  };
94
+ function mergeHeaders(...allHeaders) {
95
+ const finalHeaders = new Headers();
96
+ for (const h of allHeaders) {
97
+ if (!h || typeof h !== "object") continue;
98
+ const iterator = h instanceof Headers ? h.entries() : Object.entries(h);
99
+ for (const [k, v] of iterator) if (v === null) finalHeaders.delete(k);
100
+ else if (Array.isArray(v)) for (const v2 of v) finalHeaders.append(k, v2);
101
+ else if (v !== void 0) finalHeaders.set(k, v);
102
+ }
103
+ return finalHeaders;
104
+ }
87
105
 
88
106
  //#endregion
89
107
  export { Client };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapaul/api",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [