@hapaul/api 0.1.2 → 0.1.3
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 +36 -39
- package/dist/index.js +30 -27
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MaybeOptionalInit, ParseAsResponse } from "openapi-fetch";
|
|
2
|
-
import { PathsWithMethod, ResponseObjectMap, SuccessResponse } from "openapi-typescript-helpers";
|
|
1
|
+
import { MaybeOptionalInit, Middleware, ParseAsResponse } from "openapi-fetch";
|
|
2
|
+
import { ErrorResponse, PathsWithMethod, ResponseObjectMap, SuccessResponse } from "openapi-typescript-helpers";
|
|
3
3
|
|
|
4
4
|
//#region schema.d.ts
|
|
5
5
|
|
|
@@ -2199,47 +2199,44 @@ type InitParam<Init> = RequiredKeysOf<Init> extends never ? [(Init & {
|
|
|
2199
2199
|
})?] : [Init & {
|
|
2200
2200
|
[key: string]: unknown;
|
|
2201
2201
|
}];
|
|
2202
|
-
type
|
|
2203
|
-
type
|
|
2204
|
-
type
|
|
2205
|
-
type
|
|
2206
|
-
|
|
2207
|
-
type GetParams<T extends GetPaths> = InitParam<MaybeOptionalInit<paths[T], 'get'>>;
|
|
2208
|
-
type PostParams<T extends PostPaths> = InitParam<MaybeOptionalInit<paths[T], 'post'>>;
|
|
2209
|
-
type PutParams<T extends PutPaths> = InitParam<MaybeOptionalInit<paths[T], 'put'>>;
|
|
2210
|
-
type PatchParams<T extends PatchPaths> = InitParam<MaybeOptionalInit<paths[T], 'patch'>>;
|
|
2211
|
-
type DeleteParams<T extends DeletePaths> = InitParam<MaybeOptionalInit<paths[T], 'delete'>>;
|
|
2212
|
-
type ResponseData<T extends keyof paths, M extends 'get' | 'post' | 'put' | 'patch' | 'delete'> = ParseAsResponse<SuccessResponse<ResponseObjectMap<paths[T][M]>, `${string}/${string}`>, MaybeOptionalInit<paths[T], M>>;
|
|
2213
|
-
interface ApiError {
|
|
2214
|
-
status: number;
|
|
2215
|
-
code: string;
|
|
2216
|
-
message: string;
|
|
2217
|
-
}
|
|
2218
|
-
interface IClientConfig {
|
|
2202
|
+
type Methods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
2203
|
+
type RequestParams<T extends PathsWithMethod<paths, M>, M extends Methods> = InitParam<MaybeOptionalInit<paths[T], M>>;
|
|
2204
|
+
type ResponseData<T extends PathsWithMethod<paths, M>, M extends Methods> = ParseAsResponse<SuccessResponse<ResponseObjectMap<paths[T][M]>, `${string}/${string}`>, MaybeOptionalInit<paths[T], M>>;
|
|
2205
|
+
type ErrorResponseData<T extends PathsWithMethod<paths, M>, M extends Methods> = ErrorResponse<ResponseObjectMap<paths[T][M]>, `${string}/${string}`>;
|
|
2206
|
+
interface ClientConfig {
|
|
2219
2207
|
baseUrl?: string;
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2208
|
+
custom?: any;
|
|
2209
|
+
}
|
|
2210
|
+
interface RequestConfig<T extends PathsWithMethod<paths, M>, M extends Methods> extends ClientConfig {}
|
|
2211
|
+
interface ApiClientInstance<T extends PathsWithMethod<paths, M>, M extends Methods> {
|
|
2212
|
+
send: (config?: RequestConfig<T, M>) => Promise<{
|
|
2213
|
+
data: ResponseData<T, M>;
|
|
2214
|
+
error: ErrorResponseData<T, M>;
|
|
2215
|
+
}>;
|
|
2223
2216
|
}
|
|
2224
2217
|
declare class ApiClient {
|
|
2225
2218
|
private client;
|
|
2226
2219
|
private baseUrl;
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2220
|
+
constructor(config?: ClientConfig);
|
|
2221
|
+
config(config?: ClientConfig): ApiClient;
|
|
2222
|
+
use(middleware: Middleware): void;
|
|
2223
|
+
request<T extends PathsWithMethod<paths, M>, M extends Methods>(method: M, path: T, ...params: RequestParams<T, M>): ApiClientInstance<T, M>;
|
|
2224
|
+
get<T extends PathsWithMethod<paths, 'get'>>(path: T, ...params: RequestParams<T, 'get'>): ApiClientInstance<T, 'get'>;
|
|
2225
|
+
post<T extends PathsWithMethod<paths, 'post'>>(path: T, ...params: RequestParams<T, 'post'>): ApiClientInstance<T, 'post'>;
|
|
2226
|
+
put<T extends PathsWithMethod<paths, 'put'>>(path: T, ...params: RequestParams<T, 'put'>): ApiClientInstance<T, 'put'>;
|
|
2227
|
+
patch<T extends PathsWithMethod<paths, 'patch'>>(path: T, ...params: RequestParams<T, 'patch'>): ApiClientInstance<T, 'patch'>;
|
|
2228
|
+
delete<T extends PathsWithMethod<paths, 'delete'>>(path: T, ...params: RequestParams<T, 'delete'>): ApiClientInstance<T, 'delete'>;
|
|
2229
|
+
beforeRequest<T extends PathsWithMethod<paths, M>, M extends Methods>(params: {
|
|
2230
|
+
config: RequestConfig<T, M>;
|
|
2231
|
+
}): Promise<void>;
|
|
2232
|
+
afterResponse<D, U>(params: {
|
|
2233
|
+
config: ClientConfig;
|
|
2234
|
+
data: D;
|
|
2235
|
+
error: U;
|
|
2236
|
+
}): Promise<{
|
|
2237
|
+
data: D;
|
|
2238
|
+
error: U;
|
|
2239
|
+
}>;
|
|
2243
2240
|
}
|
|
2244
2241
|
//#endregion
|
|
2245
|
-
export { ApiClient,
|
|
2242
|
+
export { ApiClient, ApiClientInstance, ClientConfig, RequestConfig };
|
package/dist/index.js
CHANGED
|
@@ -372,14 +372,8 @@ function removeTrailingSlash(url) {
|
|
|
372
372
|
var ApiClient = class ApiClient {
|
|
373
373
|
client;
|
|
374
374
|
baseUrl;
|
|
375
|
-
showLoading;
|
|
376
|
-
showMessage;
|
|
377
|
-
handleMessage;
|
|
378
375
|
constructor(config) {
|
|
379
376
|
this.baseUrl = config?.baseUrl ?? "";
|
|
380
|
-
this.showLoading = config?.showLoading ?? true;
|
|
381
|
-
this.showMessage = config?.showMessage ?? true;
|
|
382
|
-
this.handleMessage = config?.handleMessage ?? ((error) => error.message);
|
|
383
377
|
this.client = createClient({ baseUrl: this.baseUrl });
|
|
384
378
|
}
|
|
385
379
|
config(config) {
|
|
@@ -389,32 +383,41 @@ var ApiClient = class ApiClient {
|
|
|
389
383
|
};
|
|
390
384
|
return new ApiClient(newConfig);
|
|
391
385
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
return this.handleResponse(data, error);
|
|
386
|
+
use(middleware) {
|
|
387
|
+
this.client.use(middleware);
|
|
395
388
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
389
|
+
request(method, path, ...params) {
|
|
390
|
+
return { send: async (config) => {
|
|
391
|
+
await this.beforeRequest({ config });
|
|
392
|
+
const { data, error } = await this.client.request(method, path, ...params);
|
|
393
|
+
return this.afterResponse({
|
|
394
|
+
config,
|
|
395
|
+
data,
|
|
396
|
+
error
|
|
397
|
+
});
|
|
398
|
+
} };
|
|
399
|
+
}
|
|
400
|
+
get(path, ...params) {
|
|
401
|
+
return this.request("get", path, ...params);
|
|
402
|
+
}
|
|
403
|
+
post(path, ...params) {
|
|
404
|
+
return this.request("post", path, ...params);
|
|
399
405
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
return this.handleResponse(data, error);
|
|
406
|
+
put(path, ...params) {
|
|
407
|
+
return this.request("put", path, ...params);
|
|
403
408
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
return this.handleResponse(data, error);
|
|
409
|
+
patch(path, ...params) {
|
|
410
|
+
return this.request("patch", path, ...params);
|
|
407
411
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
return this.handleResponse(data, error);
|
|
412
|
+
delete(path, ...params) {
|
|
413
|
+
return this.request("delete", path, ...params);
|
|
411
414
|
}
|
|
412
|
-
async
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
415
|
+
async beforeRequest(params) {}
|
|
416
|
+
async afterResponse(params) {
|
|
417
|
+
return {
|
|
418
|
+
data: params.data,
|
|
419
|
+
error: params.error
|
|
420
|
+
};
|
|
418
421
|
}
|
|
419
422
|
};
|
|
420
423
|
|