@hapaul/api 0.1.8 → 0.1.10
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 +10 -2
- package/dist/index.js +14 -3
- package/package.json +1 -4
package/dist/index.d.ts
CHANGED
|
@@ -2246,14 +2246,22 @@ type RequestConfig<T extends PathsWithMethod<paths, M>, M extends Methods, C = {
|
|
|
2246
2246
|
method: M;
|
|
2247
2247
|
path: T;
|
|
2248
2248
|
} & MaybeOptionalInit<paths[T], M> & C;
|
|
2249
|
+
type RequestLooseConfig<C = {}> = ClientConfig & {
|
|
2250
|
+
method: Methods;
|
|
2251
|
+
path: keyof paths;
|
|
2252
|
+
} & RequestInit & C;
|
|
2249
2253
|
type RequestResponse<T extends PathsWithMethod<paths, M>, M extends Methods> = Promise<{
|
|
2250
2254
|
data: ResponseData<T, M>;
|
|
2251
2255
|
error: ResponseError<T, M>;
|
|
2252
2256
|
response: Response;
|
|
2253
2257
|
}>;
|
|
2254
2258
|
interface Middleware<C> {
|
|
2255
|
-
onRequest?: (config:
|
|
2259
|
+
onRequest?: (config: RequestLooseConfig<C>) => RequestConfig<any, any, C> | Promise<RequestConfig<any, any, C>>;
|
|
2256
2260
|
onResponse?: (config: RequestConfig<any, any, C>, response: Response) => Response | Promise<Response>;
|
|
2261
|
+
onError?: (config: RequestLooseConfig<C>, error: {
|
|
2262
|
+
code: string;
|
|
2263
|
+
message: string;
|
|
2264
|
+
}) => void;
|
|
2257
2265
|
}
|
|
2258
2266
|
declare class Client<C = {}> {
|
|
2259
2267
|
private middlewares;
|
|
@@ -2275,4 +2283,4 @@ declare class Client<C = {}> {
|
|
|
2275
2283
|
}): Promise<Response>;
|
|
2276
2284
|
}
|
|
2277
2285
|
//#endregion
|
|
2278
|
-
export { Client, ClientConfig, Middleware, RequestConfig, RequestResponse };
|
|
2286
|
+
export { Client, ClientConfig, Middleware, RequestConfig, RequestLooseConfig, RequestResponse };
|
package/dist/index.js
CHANGED
|
@@ -22,10 +22,12 @@ var Client = class {
|
|
|
22
22
|
const headersOptions = requestConfig.headers;
|
|
23
23
|
const headers = mergeHeaders(headersOptions);
|
|
24
24
|
const contentType = headers.get("Content-Type");
|
|
25
|
+
const body = requestConfig.body;
|
|
25
26
|
if (!contentType) headers.set("Content-Type", "application/json");
|
|
26
27
|
try {
|
|
27
|
-
const {} = params;
|
|
28
28
|
let response = await fetch(url, {
|
|
29
|
+
...requestConfig,
|
|
30
|
+
body: defaultBodySerializer(body, headers),
|
|
29
31
|
method: method.toUpperCase(),
|
|
30
32
|
headers
|
|
31
33
|
});
|
|
@@ -56,6 +58,7 @@ var Client = class {
|
|
|
56
58
|
break;
|
|
57
59
|
default: assign(await response.bytes());
|
|
58
60
|
}
|
|
61
|
+
if (!isOk) for (const middleware of this.middlewares) middleware.onError?.(requestConfig, error);
|
|
59
62
|
return {
|
|
60
63
|
data,
|
|
61
64
|
error,
|
|
@@ -82,12 +85,12 @@ var Client = class {
|
|
|
82
85
|
}
|
|
83
86
|
async beforeRequest(params) {
|
|
84
87
|
let config = params.config;
|
|
85
|
-
for (const middleware of this.middlewares) config = await middleware.onRequest?.(config);
|
|
88
|
+
for (const middleware of this.middlewares) config = await middleware.onRequest?.(config) ?? config;
|
|
86
89
|
return config;
|
|
87
90
|
}
|
|
88
91
|
async afterResponse(params) {
|
|
89
92
|
let response = params.response;
|
|
90
|
-
for (const middleware of this.middlewares) response = await middleware.onResponse?.(params.config, response);
|
|
93
|
+
for (const middleware of this.middlewares) response = await middleware.onResponse?.(params.config, response) ?? response;
|
|
91
94
|
return response;
|
|
92
95
|
}
|
|
93
96
|
};
|
|
@@ -102,6 +105,14 @@ function mergeHeaders(...allHeaders) {
|
|
|
102
105
|
}
|
|
103
106
|
return finalHeaders;
|
|
104
107
|
}
|
|
108
|
+
function defaultBodySerializer(body, headers) {
|
|
109
|
+
if (body instanceof FormData) return body;
|
|
110
|
+
if (headers) {
|
|
111
|
+
const contentType = headers.get("Content-Type");
|
|
112
|
+
if (contentType === "application/x-www-form-urlencoded") return new URLSearchParams(body).toString();
|
|
113
|
+
}
|
|
114
|
+
return JSON.stringify(body);
|
|
115
|
+
}
|
|
105
116
|
|
|
106
117
|
//#endregion
|
|
107
118
|
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.10",
|
|
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
|
}
|