@hapaul/api 0.1.9 → 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 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: RequestConfig<any, any, C>) => RequestConfig<any, any, C> | Promise<RequestConfig<any, any, C>>;
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
@@ -58,6 +58,7 @@ var Client = class {
58
58
  break;
59
59
  default: assign(await response.bytes());
60
60
  }
61
+ if (!isOk) for (const middleware of this.middlewares) middleware.onError?.(requestConfig, error);
61
62
  return {
62
63
  data,
63
64
  error,
@@ -84,12 +85,12 @@ var Client = class {
84
85
  }
85
86
  async beforeRequest(params) {
86
87
  let config = params.config;
87
- for (const middleware of this.middlewares) config = await middleware.onRequest?.(config);
88
+ for (const middleware of this.middlewares) config = await middleware.onRequest?.(config) ?? config;
88
89
  return config;
89
90
  }
90
91
  async afterResponse(params) {
91
92
  let response = params.response;
92
- 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;
93
94
  return response;
94
95
  }
95
96
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapaul/api",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [