@hapaul/api 0.1.9 → 0.1.11
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.d.ts +342 -1154
- package/dist/index.js +36 -16
- package/package.json +8 -7
package/dist/index.js
CHANGED
|
@@ -18,16 +18,19 @@ var Client = class {
|
|
|
18
18
|
requestConfig = await this.beforeRequest({ config: requestConfig });
|
|
19
19
|
method = requestConfig.method;
|
|
20
20
|
const baseUrl = requestConfig.baseUrl ?? this.baseUrl;
|
|
21
|
-
|
|
21
|
+
let url = baseUrl + path;
|
|
22
|
+
const query = requestConfig.params?.query;
|
|
23
|
+
if (query && typeof query === "object" && Object.entries(query).filter(([_, v]) => v !== void 0).length > 0) url = url + "?" + defaultQuerySerializer(query);
|
|
22
24
|
const headersOptions = requestConfig.headers;
|
|
23
25
|
const headers = mergeHeaders(headersOptions);
|
|
24
26
|
const contentType = headers.get("Content-Type");
|
|
25
27
|
const body = requestConfig.body;
|
|
26
28
|
if (!contentType) headers.set("Content-Type", "application/json");
|
|
27
29
|
try {
|
|
30
|
+
const serializedBody = defaultBodySerializer(body, headers);
|
|
28
31
|
let response = await fetch(url, {
|
|
29
32
|
...requestConfig,
|
|
30
|
-
body:
|
|
33
|
+
body: serializedBody,
|
|
31
34
|
method: method.toUpperCase(),
|
|
32
35
|
headers
|
|
33
36
|
});
|
|
@@ -46,17 +49,16 @@ var Client = class {
|
|
|
46
49
|
if (status >= 200 && status < 300) isOk = true;
|
|
47
50
|
else if (status >= 400) isOk = false;
|
|
48
51
|
const contentType$1 = response.headers.get("Content-Type") || "application/json";
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
default: assign(await response.bytes());
|
|
52
|
+
if (contentType$1.includes("application/json")) assign(await response.json());
|
|
53
|
+
else if (contentType$1.includes("text/plain")) assign(await response.text());
|
|
54
|
+
else assign(await response.arrayBuffer());
|
|
55
|
+
if (!isOk) {
|
|
56
|
+
let message = response.statusText;
|
|
57
|
+
if (typeof error === "object" && "message" in error) message = error.message;
|
|
58
|
+
for (const middleware of this.middlewares) middleware.onError?.(requestConfig, response, {
|
|
59
|
+
status,
|
|
60
|
+
message
|
|
61
|
+
});
|
|
60
62
|
}
|
|
61
63
|
return {
|
|
62
64
|
data,
|
|
@@ -84,12 +86,12 @@ var Client = class {
|
|
|
84
86
|
}
|
|
85
87
|
async beforeRequest(params) {
|
|
86
88
|
let config = params.config;
|
|
87
|
-
for (const middleware of this.middlewares) config = await middleware.onRequest?.(config);
|
|
89
|
+
for (const middleware of this.middlewares) config = await middleware.onRequest?.(config) ?? config;
|
|
88
90
|
return config;
|
|
89
91
|
}
|
|
90
92
|
async afterResponse(params) {
|
|
91
93
|
let response = params.response;
|
|
92
|
-
for (const middleware of this.middlewares) response = await middleware.onResponse?.(params.config, response);
|
|
94
|
+
for (const middleware of this.middlewares) response = await middleware.onResponse?.(params.config, response) ?? response;
|
|
93
95
|
return response;
|
|
94
96
|
}
|
|
95
97
|
};
|
|
@@ -104,11 +106,29 @@ function mergeHeaders(...allHeaders) {
|
|
|
104
106
|
}
|
|
105
107
|
return finalHeaders;
|
|
106
108
|
}
|
|
109
|
+
function defaultQuerySerializer(query) {
|
|
110
|
+
return new URLSearchParams(query).toString();
|
|
111
|
+
}
|
|
107
112
|
function defaultBodySerializer(body, headers) {
|
|
108
113
|
if (body instanceof FormData) return body;
|
|
109
114
|
if (headers) {
|
|
110
115
|
const contentType = headers.get("Content-Type");
|
|
111
|
-
|
|
116
|
+
switch (contentType) {
|
|
117
|
+
case "application/x-www-form-urlencoded": return new URLSearchParams(body).toString();
|
|
118
|
+
case "multipart/form-data": {
|
|
119
|
+
headers.delete("Content-Type");
|
|
120
|
+
const formData = new FormData();
|
|
121
|
+
Object.entries(body).forEach(([k, v]) => {
|
|
122
|
+
if (isArray(v)) v.forEach((x) => {
|
|
123
|
+
formData.append(k, x);
|
|
124
|
+
});
|
|
125
|
+
else if (isObject(v)) formData.append(k, JSON.stringify(v));
|
|
126
|
+
else if (isDate(v)) formData.append(k, v.toISOString());
|
|
127
|
+
else formData.append(k, v);
|
|
128
|
+
});
|
|
129
|
+
return formData;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
112
132
|
}
|
|
113
133
|
return JSON.stringify(body);
|
|
114
134
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hapaul/api",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -15,19 +15,20 @@
|
|
|
15
15
|
},
|
|
16
16
|
"types": "./dist/index.d.ts",
|
|
17
17
|
"scripts": {
|
|
18
|
-
"generate": "openapi-typescript http://localhost:4000/
|
|
18
|
+
"generate": "openapi-typescript http://localhost:4000/docs/openapi.yaml -o ./schema.d.ts",
|
|
19
|
+
"prebuild": "npm run generate",
|
|
19
20
|
"build": "rolldown -c rolldown.config.ts"
|
|
20
21
|
},
|
|
21
22
|
"keywords": [],
|
|
22
23
|
"author": "",
|
|
23
24
|
"license": "ISC",
|
|
24
|
-
"packageManager": "pnpm@10.
|
|
25
|
+
"packageManager": "pnpm@10.23.0+sha512.21c4e5698002ade97e4efe8b8b4a89a8de3c85a37919f957e7a0f30f38fbc5bbdd05980ffe29179b2fb6e6e691242e098d945d1601772cad0fef5fb6411e2a4b",
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"@hapaul/dev": "^0.0.
|
|
27
|
-
"openapi-typescript": "^7.
|
|
27
|
+
"@hapaul/dev": "^0.0.3",
|
|
28
|
+
"openapi-typescript": "^7.10.1",
|
|
28
29
|
"openapi-typescript-helpers": "^0.0.15",
|
|
29
30
|
"rolldown": "1.0.0-beta.30",
|
|
30
|
-
"rolldown-plugin-dts": "^0.14.
|
|
31
|
-
"typescript": "^5.
|
|
31
|
+
"rolldown-plugin-dts": "^0.14.3",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
32
33
|
}
|
|
33
34
|
}
|