@beyonk/http 12.1.2 → 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
@@ -123,8 +123,12 @@ var Api = class {
123
123
  }
124
124
  if (this.client) {
125
125
  return this.client;
126
- } else if (typeof window !== "undefined") {
127
- return window.fetch.bind(window);
126
+ }
127
+ if (config?.fetch) {
128
+ return config.fetch;
129
+ }
130
+ if ("fetch" in globalThis) {
131
+ return globalThis.fetch;
128
132
  }
129
133
  throw Error("No client provided and can't find one automatically");
130
134
  }
@@ -137,7 +141,7 @@ var Api = class {
137
141
  handle(e, ctx) {
138
142
  const constructorName = Object.getPrototypeOf(e).constructor.name;
139
143
  const globalHandlerName = `${constructorName[0].toLowerCase()}${constructorName.slice(1, -5)}`;
140
- 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) => {
141
145
  console.error(constructorName, e2.message, e2);
142
146
  });
143
147
  return handler(e, ctx);
@@ -176,8 +180,8 @@ var Api = class {
176
180
  } finally {
177
181
  this.resetRequest();
178
182
  }
179
- const { json, httpStatus } = result;
180
- return fn ? fn(json, httpStatus) : json;
183
+ const { json, httpStatus, response } = result;
184
+ return fn ? fn(json, httpStatus, response) : json;
181
185
  }
182
186
  /**
183
187
  * Check if the response has content
@@ -215,7 +219,7 @@ var Api = class {
215
219
  console.error("Unable to parse response json", e.message);
216
220
  }
217
221
  }
218
- return { httpStatus: r.status, json };
222
+ return { httpStatus: r.status, json, response: r };
219
223
  }
220
224
  let content = "";
221
225
  try {
package/dist/index.d.cts CHANGED
@@ -6,6 +6,8 @@ type ApiOptions = {
6
6
  baseUrl?: string;
7
7
  /** Mock client for testing */
8
8
  mock?: FetchClient;
9
+ /** Fetch client for HTTP requests */
10
+ fetch?: FetchClient;
9
11
  /** Whether to retry failed requests */
10
12
  retry?: RetryOptions | false;
11
13
  /** Whether to parse error responses as JSON */
@@ -50,12 +52,14 @@ type ErrorHandlers = {
50
52
  expectationFailed?: ErrorHandler;
51
53
  badData?: ErrorHandler;
52
54
  tooManyRequests?: ErrorHandler;
55
+ /** Called when no request-level or status-specific handler matches */
56
+ default?: ErrorHandler;
53
57
  [key: string]: ErrorHandler | undefined;
54
58
  };
55
59
  /**
56
60
  * Function to transform API response
57
61
  */
58
- type ResponseTransformer<T = any> = (json: any, httpStatus: number) => T;
62
+ type ResponseTransformer<T = any> = (json: any, httpStatus: number, response: Response) => T;
59
63
  /**
60
64
  * Fetch client interface
61
65
  */
package/dist/index.d.ts CHANGED
@@ -6,6 +6,8 @@ type ApiOptions = {
6
6
  baseUrl?: string;
7
7
  /** Mock client for testing */
8
8
  mock?: FetchClient;
9
+ /** Fetch client for HTTP requests */
10
+ fetch?: FetchClient;
9
11
  /** Whether to retry failed requests */
10
12
  retry?: RetryOptions | false;
11
13
  /** Whether to parse error responses as JSON */
@@ -50,12 +52,14 @@ type ErrorHandlers = {
50
52
  expectationFailed?: ErrorHandler;
51
53
  badData?: ErrorHandler;
52
54
  tooManyRequests?: ErrorHandler;
55
+ /** Called when no request-level or status-specific handler matches */
56
+ default?: ErrorHandler;
53
57
  [key: string]: ErrorHandler | undefined;
54
58
  };
55
59
  /**
56
60
  * Function to transform API response
57
61
  */
58
- type ResponseTransformer<T = any> = (json: any, httpStatus: number) => T;
62
+ type ResponseTransformer<T = any> = (json: any, httpStatus: number, response: Response) => T;
59
63
  /**
60
64
  * Fetch client interface
61
65
  */
package/dist/index.js CHANGED
@@ -98,8 +98,12 @@ var Api = class {
98
98
  }
99
99
  if (this.client) {
100
100
  return this.client;
101
- } else if (typeof window !== "undefined") {
102
- return window.fetch.bind(window);
101
+ }
102
+ if (config?.fetch) {
103
+ return config.fetch;
104
+ }
105
+ if ("fetch" in globalThis) {
106
+ return globalThis.fetch;
103
107
  }
104
108
  throw Error("No client provided and can't find one automatically");
105
109
  }
@@ -112,7 +116,7 @@ var Api = class {
112
116
  handle(e, ctx) {
113
117
  const constructorName = Object.getPrototypeOf(e).constructor.name;
114
118
  const globalHandlerName = `${constructorName[0].toLowerCase()}${constructorName.slice(1, -5)}`;
115
- 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) => {
116
120
  console.error(constructorName, e2.message, e2);
117
121
  });
118
122
  return handler(e, ctx);
@@ -151,8 +155,8 @@ var Api = class {
151
155
  } finally {
152
156
  this.resetRequest();
153
157
  }
154
- const { json, httpStatus } = result;
155
- return fn ? fn(json, httpStatus) : json;
158
+ const { json, httpStatus, response } = result;
159
+ return fn ? fn(json, httpStatus, response) : json;
156
160
  }
157
161
  /**
158
162
  * Check if the response has content
@@ -190,7 +194,7 @@ var Api = class {
190
194
  console.error("Unable to parse response json", e.message);
191
195
  }
192
196
  }
193
- return { httpStatus: r.status, json };
197
+ return { httpStatus: r.status, json, response: r };
194
198
  }
195
199
  let content = "";
196
200
  try {
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "@beyonk/http",
3
- "version": "12.1.2",
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",