@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 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 GetPaths = PathsWithMethod<paths, 'get'>;
2203
- type PostPaths = PathsWithMethod<paths, 'post'>;
2204
- type PutPaths = PathsWithMethod<paths, 'put'>;
2205
- type PatchPaths = PathsWithMethod<paths, 'patch'>;
2206
- type DeletePaths = PathsWithMethod<paths, 'delete'>;
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
- showLoading?: boolean;
2221
- showMessage?: boolean;
2222
- handleMessage?: (error: ApiError) => string;
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
- private showLoading;
2228
- private showMessage;
2229
- private handleMessage;
2230
- constructor(config?: IClientConfig);
2231
- config(config: IClientConfig): ApiClient;
2232
- get<T extends GetPaths>(path: T, ...params: GetParams<T>): Promise<ResponseData<T, 'get'>>;
2233
- post<T extends PostPaths>(path: T, ...params: PostParams<T>): Promise<ResponseData<T, 'post'>>;
2234
- put<T extends PutPaths>(path: T, ...params: PutParams<T>): Promise<ResponseData<T, 'put'>>;
2235
- patch<T extends PatchPaths>(path: T, ...params: PatchParams<T>): Promise<ResponseData<T, 'patch'>>;
2236
- delete<T extends DeletePaths>(path: T, ...params: DeleteParams<T>): Promise<ResponseData<T, 'delete'>>;
2237
- handleResponse<D extends {
2238
- data: unknown;
2239
- } | {
2240
- code: string;
2241
- message: string;
2242
- } | any, U>(data: D, error: U): Promise<D>;
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, ApiError, IClientConfig };
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
- async get(path, ...params) {
393
- const { data, error } = await this.client.GET(path, ...params);
394
- return this.handleResponse(data, error);
386
+ use(middleware) {
387
+ this.client.use(middleware);
395
388
  }
396
- async post(path, ...params) {
397
- const { data, error } = await this.client.POST(path, ...params);
398
- return this.handleResponse(data, error);
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
- async put(path, ...params) {
401
- const { data, error } = await this.client.PUT(path, ...params);
402
- return this.handleResponse(data, error);
406
+ put(path, ...params) {
407
+ return this.request("put", path, ...params);
403
408
  }
404
- async patch(path, ...params) {
405
- const { data, error } = await this.client.PATCH(path, ...params);
406
- return this.handleResponse(data, error);
409
+ patch(path, ...params) {
410
+ return this.request("patch", path, ...params);
407
411
  }
408
- async delete(path, ...params) {
409
- const { data, error } = await this.client.DELETE(path, ...params);
410
- return this.handleResponse(data, error);
412
+ delete(path, ...params) {
413
+ return this.request("delete", path, ...params);
411
414
  }
412
- async handleResponse(data, error) {
413
- if (error) return Promise.reject(error);
414
- if (typeof data === "object") if (data) if ("code" in data) return Promise.reject(data);
415
- else return Promise.resolve(data);
416
- else return Promise.reject(error);
417
- else return Promise.resolve(data);
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapaul/api",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [