@hapaul/api 0.1.7 → 0.1.9
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 +32 -4
- package/package.json +1 -4
package/dist/index.js
CHANGED
|
@@ -19,9 +19,18 @@ 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
|
+
const body = requestConfig.body;
|
|
26
|
+
if (!contentType) headers.set("Content-Type", "application/json");
|
|
22
27
|
try {
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
let response = await fetch(url, {
|
|
29
|
+
...requestConfig,
|
|
30
|
+
body: defaultBodySerializer(body, headers),
|
|
31
|
+
method: method.toUpperCase(),
|
|
32
|
+
headers
|
|
33
|
+
});
|
|
25
34
|
response = await this.afterResponse({
|
|
26
35
|
config: requestConfig,
|
|
27
36
|
response
|
|
@@ -36,8 +45,8 @@ var Client = class {
|
|
|
36
45
|
};
|
|
37
46
|
if (status >= 200 && status < 300) isOk = true;
|
|
38
47
|
else if (status >= 400) isOk = false;
|
|
39
|
-
const contentType = response.headers.get("Content-Type") || "application/json";
|
|
40
|
-
switch (contentType) {
|
|
48
|
+
const contentType$1 = response.headers.get("Content-Type") || "application/json";
|
|
49
|
+
switch (contentType$1) {
|
|
41
50
|
case "application/json":
|
|
42
51
|
assign(await response.json());
|
|
43
52
|
break;
|
|
@@ -84,6 +93,25 @@ var Client = class {
|
|
|
84
93
|
return response;
|
|
85
94
|
}
|
|
86
95
|
};
|
|
96
|
+
function mergeHeaders(...allHeaders) {
|
|
97
|
+
const finalHeaders = new Headers();
|
|
98
|
+
for (const h of allHeaders) {
|
|
99
|
+
if (!h || typeof h !== "object") continue;
|
|
100
|
+
const iterator = h instanceof Headers ? h.entries() : Object.entries(h);
|
|
101
|
+
for (const [k, v] of iterator) if (v === null) finalHeaders.delete(k);
|
|
102
|
+
else if (Array.isArray(v)) for (const v2 of v) finalHeaders.append(k, v2);
|
|
103
|
+
else if (v !== void 0) finalHeaders.set(k, v);
|
|
104
|
+
}
|
|
105
|
+
return finalHeaders;
|
|
106
|
+
}
|
|
107
|
+
function defaultBodySerializer(body, headers) {
|
|
108
|
+
if (body instanceof FormData) return body;
|
|
109
|
+
if (headers) {
|
|
110
|
+
const contentType = headers.get("Content-Type");
|
|
111
|
+
if (contentType === "application/x-www-form-urlencoded") return new URLSearchParams(body).toString();
|
|
112
|
+
}
|
|
113
|
+
return JSON.stringify(body);
|
|
114
|
+
}
|
|
87
115
|
|
|
88
116
|
//#endregion
|
|
89
117
|
export { Client };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hapaul/api",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -29,8 +29,5 @@
|
|
|
29
29
|
"rolldown": "1.0.0-beta.30",
|
|
30
30
|
"rolldown-plugin-dts": "^0.14.2",
|
|
31
31
|
"typescript": "^5.8.3"
|
|
32
|
-
},
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"openapi-fetch": "^0.14.0"
|
|
35
32
|
}
|
|
36
33
|
}
|