@hapaul/api 0.1.4 → 0.1.5
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 +18 -29
- package/dist/index.js +23 -31
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2236,52 +2236,41 @@ type InitParam<Init> = RequiredKeysOf<Init> extends never ? [(Init & {
|
|
|
2236
2236
|
[key: string]: unknown;
|
|
2237
2237
|
}];
|
|
2238
2238
|
type Methods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
2239
|
-
type RequestParams<T extends PathsWithMethod<paths, M>, M extends Methods> = InitParam<MaybeOptionalInit<paths[T], M
|
|
2239
|
+
type RequestParams<T extends PathsWithMethod<paths, M>, M extends Methods, C> = InitParam<MaybeOptionalInit<paths[T], M> & C>;
|
|
2240
2240
|
type ResponseData<T extends PathsWithMethod<paths, M>, M extends Methods> = ParseAsResponse<SuccessResponse<ResponseObjectMap<paths[T][M]>, `${string}/${string}`>, MaybeOptionalInit<paths[T], M>>;
|
|
2241
2241
|
type ResponseError<T extends PathsWithMethod<paths, M>, M extends Methods> = ErrorResponse<ResponseObjectMap<paths[T][M]>, `${string}/${string}`>;
|
|
2242
|
-
|
|
2242
|
+
type ClientConfig = {
|
|
2243
2243
|
baseUrl?: string;
|
|
2244
|
-
|
|
2245
|
-
}
|
|
2246
|
-
|
|
2244
|
+
};
|
|
2245
|
+
type RequestConfig<T extends PathsWithMethod<paths, M>, M extends Methods, C = {}> = ClientConfig & {
|
|
2246
|
+
method: M;
|
|
2247
|
+
path: T;
|
|
2248
|
+
} & MaybeOptionalInit<paths[T], M> & C;
|
|
2247
2249
|
type RequestResponse<T extends PathsWithMethod<paths, M>, M extends Methods> = Promise<{
|
|
2248
2250
|
data: ResponseData<T, M>;
|
|
2249
2251
|
error: ResponseError<T, M>;
|
|
2250
2252
|
response: Response;
|
|
2251
2253
|
}>;
|
|
2252
2254
|
interface Middleware<C> {
|
|
2253
|
-
onRequest?: (config: RequestConfig<C>) => RequestConfig<C> | Promise<RequestConfig<C>>;
|
|
2254
|
-
onResponse?: (config: RequestConfig<C>, response: Response) => Response | Promise<Response>;
|
|
2255
|
+
onRequest?: (config: RequestConfig<any, any, C>) => RequestConfig<any, any, C> | Promise<RequestConfig<any, any, C>>;
|
|
2256
|
+
onResponse?: (config: RequestConfig<any, any, C>, response: Response) => Response | Promise<Response>;
|
|
2255
2257
|
}
|
|
2256
2258
|
declare class Client<C = {}> {
|
|
2257
2259
|
private middlewares;
|
|
2258
2260
|
private baseUrl;
|
|
2259
2261
|
constructor(config?: ClientConfig);
|
|
2260
2262
|
use(middleware: Middleware<C>): void;
|
|
2261
|
-
request<T extends PathsWithMethod<paths, M>, M extends Methods>(params:
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
} & RequestParams<T, 'get'>, config?: RequestConfig<C>): RequestResponse<T, 'get'>;
|
|
2268
|
-
post<T extends PathsWithMethod<paths, 'post'>>(params: {
|
|
2269
|
-
path: T;
|
|
2270
|
-
} & RequestParams<T, 'post'>, config?: RequestConfig<C>): RequestResponse<T, 'post'>;
|
|
2271
|
-
put<T extends PathsWithMethod<paths, 'put'>>(params: {
|
|
2272
|
-
path: T;
|
|
2273
|
-
} & RequestParams<T, 'put'>, config?: RequestConfig<C>): RequestResponse<T, 'put'>;
|
|
2274
|
-
patch<T extends PathsWithMethod<paths, 'patch'>>(params: {
|
|
2275
|
-
path: T;
|
|
2276
|
-
} & RequestParams<T, 'patch'>, config?: RequestConfig<C>): RequestResponse<T, 'patch'>;
|
|
2277
|
-
delete<T extends PathsWithMethod<paths, 'delete'>>(params: {
|
|
2278
|
-
path: T;
|
|
2279
|
-
} & RequestParams<T, 'delete'>, config?: RequestConfig<C>): RequestResponse<T, 'delete'>;
|
|
2263
|
+
request<T extends PathsWithMethod<paths, M>, M extends Methods>(method: M, path: T, ...params: RequestParams<T, M, C>): RequestResponse<T, M>;
|
|
2264
|
+
get<T extends PathsWithMethod<paths, 'get'>>(path: T, params: RequestConfig<T, 'get', C>): RequestResponse<T, 'get'>;
|
|
2265
|
+
post<T extends PathsWithMethod<paths, 'post'>>(path: T, params: RequestConfig<T, 'post', C>): RequestResponse<T, 'post'>;
|
|
2266
|
+
patch<T extends PathsWithMethod<paths, 'patch'>>(path: T, params: RequestConfig<T, 'patch', C>): RequestResponse<T, 'patch'>;
|
|
2267
|
+
put<T extends PathsWithMethod<paths, 'put'>>(path: T, params: RequestConfig<T, 'put', C>): RequestResponse<T, 'put'>;
|
|
2268
|
+
delete<T extends PathsWithMethod<paths, 'delete'>>(path: T, params: RequestConfig<T, 'delete', C>): RequestResponse<T, 'delete'>;
|
|
2280
2269
|
beforeRequest(params: {
|
|
2281
|
-
config:
|
|
2282
|
-
}): Promise<
|
|
2270
|
+
config: any;
|
|
2271
|
+
}): Promise<any>;
|
|
2283
2272
|
afterResponse(params: {
|
|
2284
|
-
config:
|
|
2273
|
+
config: any;
|
|
2285
2274
|
response: Response;
|
|
2286
2275
|
}): Promise<Response>;
|
|
2287
2276
|
}
|
package/dist/index.js
CHANGED
|
@@ -9,14 +9,21 @@ var Client = class {
|
|
|
9
9
|
use(middleware) {
|
|
10
10
|
this.middlewares.push(middleware);
|
|
11
11
|
}
|
|
12
|
-
async request(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
async request(method, path, ...params) {
|
|
13
|
+
let requestConfig = {
|
|
14
|
+
method,
|
|
15
|
+
path,
|
|
16
|
+
...params[0]
|
|
17
|
+
};
|
|
18
|
+
requestConfig = await this.beforeRequest({ config: requestConfig });
|
|
19
|
+
method = requestConfig.method;
|
|
20
|
+
const baseUrl = requestConfig.baseUrl ?? this.baseUrl;
|
|
21
|
+
const url = baseUrl + path;
|
|
16
22
|
try {
|
|
17
|
-
|
|
23
|
+
const {} = params;
|
|
24
|
+
let response = await fetch(url, { method: method.toUpperCase() });
|
|
18
25
|
response = await this.afterResponse({
|
|
19
|
-
config,
|
|
26
|
+
config: requestConfig,
|
|
20
27
|
response
|
|
21
28
|
});
|
|
22
29
|
const { status } = response;
|
|
@@ -51,35 +58,20 @@ var Client = class {
|
|
|
51
58
|
throw error;
|
|
52
59
|
}
|
|
53
60
|
}
|
|
54
|
-
get(
|
|
55
|
-
return this.request(
|
|
56
|
-
method: "get",
|
|
57
|
-
...params
|
|
58
|
-
}, config);
|
|
61
|
+
get(path, params) {
|
|
62
|
+
return this.request("get", path, params);
|
|
59
63
|
}
|
|
60
|
-
post(
|
|
61
|
-
return this.request(
|
|
62
|
-
method: "post",
|
|
63
|
-
...params
|
|
64
|
-
}, config);
|
|
64
|
+
post(path, params) {
|
|
65
|
+
return this.request("post", path, params);
|
|
65
66
|
}
|
|
66
|
-
|
|
67
|
-
return this.request(
|
|
68
|
-
method: "put",
|
|
69
|
-
...params
|
|
70
|
-
}, config);
|
|
67
|
+
patch(path, params) {
|
|
68
|
+
return this.request("patch", path, params);
|
|
71
69
|
}
|
|
72
|
-
|
|
73
|
-
return this.request(
|
|
74
|
-
method: "patch",
|
|
75
|
-
...params
|
|
76
|
-
}, config);
|
|
70
|
+
put(path, params) {
|
|
71
|
+
return this.request("put", path, params);
|
|
77
72
|
}
|
|
78
|
-
delete(
|
|
79
|
-
return this.request(
|
|
80
|
-
method: "delete",
|
|
81
|
-
...params
|
|
82
|
-
}, config);
|
|
73
|
+
delete(path, params) {
|
|
74
|
+
return this.request("delete", path, params);
|
|
83
75
|
}
|
|
84
76
|
async beforeRequest(params) {
|
|
85
77
|
let config = params.config;
|