@beyonk/http 12.2.0 → 12.4.2

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/README.MD CHANGED
@@ -240,6 +240,19 @@ Names are the same as the local handlers:
240
240
  .get()
241
241
  ```
242
242
 
243
+ A global `default` handler catches anything that no other handler does. A request-level `.default()` takes precedence over it, but global status handlers (such as `notFound`) do not check it.
244
+
245
+ ```js
246
+ Api.configure({
247
+ baseUrl: 'https://example.com/your/api/base',
248
+ handlers: {
249
+ default (e) {
250
+ throw error(500, e.message)
251
+ }
252
+ }
253
+ })
254
+ ```
255
+
243
256
  ## Retries
244
257
 
245
258
  The http client can retry if a network error is encountered. The default is `retry: false`, and requests won't be retried.
package/dist/index.cjs CHANGED
@@ -141,7 +141,7 @@ var Api = class {
141
141
  handle(e, ctx) {
142
142
  const constructorName = Object.getPrototypeOf(e).constructor.name;
143
143
  const globalHandlerName = `${constructorName[0].toLowerCase()}${constructorName.slice(1, -5)}`;
144
- const handler = this.handlers[constructorName] || this.options.handlers && this.options.handlers[globalHandlerName] || this.defaultHandler || ((e2) => {
144
+ const handler = this.handlers[constructorName] || this.options.handlers && this.options.handlers[globalHandlerName] || this.defaultHandler || this.options.handlers && this.options.handlers.default || ((e2) => {
145
145
  console.error(constructorName, e2.message, e2);
146
146
  });
147
147
  return handler(e, ctx);
@@ -180,8 +180,8 @@ var Api = class {
180
180
  } finally {
181
181
  this.resetRequest();
182
182
  }
183
- const { json, httpStatus } = result;
184
- return fn ? fn(json, httpStatus) : json;
183
+ const { json, httpStatus, response } = result;
184
+ return fn ? fn(json, httpStatus, response) : json;
185
185
  }
186
186
  /**
187
187
  * Check if the response has content
@@ -219,7 +219,7 @@ var Api = class {
219
219
  console.error("Unable to parse response json", e.message);
220
220
  }
221
221
  }
222
- return { httpStatus: r.status, json };
222
+ return { httpStatus: r.status, json, response: r };
223
223
  }
224
224
  let content = "";
225
225
  try {
package/dist/index.d.cts CHANGED
@@ -52,12 +52,14 @@ type ErrorHandlers = {
52
52
  expectationFailed?: ErrorHandler;
53
53
  badData?: ErrorHandler;
54
54
  tooManyRequests?: ErrorHandler;
55
+ /** Called when no request-level or status-specific handler matches */
56
+ default?: ErrorHandler;
55
57
  [key: string]: ErrorHandler | undefined;
56
58
  };
57
59
  /**
58
60
  * Function to transform API response
59
61
  */
60
- type ResponseTransformer<T = any> = (json: any, httpStatus: number) => T;
62
+ type ResponseTransformer<T = any> = (json: any, httpStatus: number, response: Response) => T;
61
63
  /**
62
64
  * Fetch client interface
63
65
  */
package/dist/index.d.ts CHANGED
@@ -52,12 +52,14 @@ type ErrorHandlers = {
52
52
  expectationFailed?: ErrorHandler;
53
53
  badData?: ErrorHandler;
54
54
  tooManyRequests?: ErrorHandler;
55
+ /** Called when no request-level or status-specific handler matches */
56
+ default?: ErrorHandler;
55
57
  [key: string]: ErrorHandler | undefined;
56
58
  };
57
59
  /**
58
60
  * Function to transform API response
59
61
  */
60
- type ResponseTransformer<T = any> = (json: any, httpStatus: number) => T;
62
+ type ResponseTransformer<T = any> = (json: any, httpStatus: number, response: Response) => T;
61
63
  /**
62
64
  * Fetch client interface
63
65
  */
package/dist/index.js CHANGED
@@ -116,7 +116,7 @@ var Api = class {
116
116
  handle(e, ctx) {
117
117
  const constructorName = Object.getPrototypeOf(e).constructor.name;
118
118
  const globalHandlerName = `${constructorName[0].toLowerCase()}${constructorName.slice(1, -5)}`;
119
- const handler = this.handlers[constructorName] || this.options.handlers && this.options.handlers[globalHandlerName] || this.defaultHandler || ((e2) => {
119
+ const handler = this.handlers[constructorName] || this.options.handlers && this.options.handlers[globalHandlerName] || this.defaultHandler || this.options.handlers && this.options.handlers.default || ((e2) => {
120
120
  console.error(constructorName, e2.message, e2);
121
121
  });
122
122
  return handler(e, ctx);
@@ -155,8 +155,8 @@ var Api = class {
155
155
  } finally {
156
156
  this.resetRequest();
157
157
  }
158
- const { json, httpStatus } = result;
159
- return fn ? fn(json, httpStatus) : json;
158
+ const { json, httpStatus, response } = result;
159
+ return fn ? fn(json, httpStatus, response) : json;
160
160
  }
161
161
  /**
162
162
  * Check if the response has content
@@ -194,7 +194,7 @@ var Api = class {
194
194
  console.error("Unable to parse response json", e.message);
195
195
  }
196
196
  }
197
- return { httpStatus: r.status, json };
197
+ return { httpStatus: r.status, json, response: r };
198
198
  }
199
199
  let content = "";
200
200
  try {
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "@beyonk/http",
3
- "version": "12.2.0",
3
+ "version": "12.4.2",
4
4
  "description": "An isomorphic http client for Svelte apps",
5
5
  "type": "module",
6
- "repository": {
7
- "type": "git",
8
- "url": "https://github.com/beyonk-adventures/http.git"
9
- },
6
+ "repository": "https://github.com/beyonk/http.git",
10
7
  "files": [
11
8
  "dist"
12
9
  ],
@@ -48,9 +45,6 @@
48
45
  ],
49
46
  "author": "Antony Jones",
50
47
  "license": "MIT",
51
- "volta": {
52
- "node": "18.15.0"
53
- },
54
48
  "scripts": {
55
49
  "test": "mocha './!(node_modules)/**/**.+(spec).js'",
56
50
  "lint": "eslint lib --ext .js",