@marianmeres/http-utils 1.4.0 → 1.6.0

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/error.d.ts CHANGED
@@ -2,7 +2,7 @@ declare class HttpError extends Error {
2
2
  name: string;
3
3
  status: number;
4
4
  statusText: string;
5
- detail: any;
5
+ body: any;
6
6
  }
7
7
  declare class BadRequest extends HttpError {
8
8
  name: string;
@@ -83,5 +83,5 @@ export declare const HTTP_ERROR: {
83
83
  BadGateway: typeof BadGateway;
84
84
  ServiceUnavailable: typeof ServiceUnavailable;
85
85
  };
86
- export declare const createHttpError: (code: number | string, message?: string | null, detail?: any, cause?: any) => BadRequest | Unauthorized | Forbidden | NotFound | MethodNotAllowed | RequestTimeout | Conflict | Gone | ImATeapot | InternalServerError | NotImplemented | BadGateway | ServiceUnavailable;
86
+ export declare const createHttpError: (code: number | string, message?: string | null, body?: string | null, cause?: any) => BadRequest | Unauthorized | Forbidden | NotFound | MethodNotAllowed | RequestTimeout | Conflict | Gone | ImATeapot | InternalServerError | NotImplemented | BadGateway | ServiceUnavailable;
87
87
  export {};
package/dist/index.cjs CHANGED
@@ -106,9 +106,10 @@ class HTTP_STATUS {
106
106
  // opinionated base for all
107
107
  class HttpError extends Error {
108
108
  name = 'HttpError';
109
+ // props simulating fetch Response
109
110
  status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
110
111
  statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
111
- detail = null;
112
+ body = null;
112
113
  }
113
114
  // some more specific instances of the well known ones...
114
115
  // client
@@ -213,10 +214,11 @@ const _wellKnownCtorMap = {
213
214
  '503': ServiceUnavailable,
214
215
  };
215
216
  const createHttpError = (code, message,
216
- // arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
217
- detail,
217
+ // arbitrary content, typically http response body which threw this error
218
+ // (will be JSON.parse-d if the content is a valid json string)
219
+ body,
218
220
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
219
- // arbitrary further details provided manually
221
+ // arbitrary details, typically response text (will be JSON.parse-d if text is a valid json string)
220
222
  cause) => {
221
223
  const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
222
224
  code = Number(code);
@@ -225,10 +227,18 @@ cause) => {
225
227
  const found = HTTP_STATUS.findByCode(code);
226
228
  const statusText = found?.TEXT ?? fallback.TEXT;
227
229
  // opinionated convention
228
- if (typeof detail === 'string') {
230
+ if (typeof body === 'string') {
229
231
  // prettier-ignore
230
232
  try {
231
- detail = JSON.parse(detail);
233
+ body = JSON.parse(body);
234
+ }
235
+ catch (e) { }
236
+ }
237
+ // opinionated convention
238
+ if (typeof cause === 'string') {
239
+ // prettier-ignore
240
+ try {
241
+ cause = JSON.parse(cause);
232
242
  }
233
243
  catch (e) { }
234
244
  }
@@ -237,7 +247,7 @@ cause) => {
237
247
  let e = new ctor(message || statusText, { cause });
238
248
  e.status = found?.CODE ?? fallback.CODE;
239
249
  e.statusText = statusText;
240
- e.detail = detail;
250
+ e.body = body;
241
251
  return e;
242
252
  };
243
253
 
@@ -271,14 +281,13 @@ const _fetch = async (params, respHeaders = null, _dumpParams = false) => {
271
281
  const r = await _fetchRaw(params);
272
282
  if (params.raw)
273
283
  return r;
284
+ //
285
+ const headers = [...r.headers.entries()].reduce((m, [k, v]) => ({ ...m, [k]: v }), {});
274
286
  // quick-n-dirty reference to headers (so it's still accessible over this api wrap)
275
287
  if (respHeaders) {
276
- Object.assign(respHeaders, [...r.headers.entries()].reduce((m, [k, v]) => ({ ...m, [k]: v }), {}),
288
+ Object.assign(respHeaders, { ...headers },
277
289
  // adding status/text under special keys
278
- {
279
- __http_status_code__: r.status,
280
- __http_status_text__: r.statusText,
281
- });
290
+ { __http_status_code__: r.status, __http_status_text__: r.statusText });
282
291
  }
283
292
  let body = await r.text();
284
293
  // prettier-ignore
@@ -288,7 +297,15 @@ const _fetch = async (params, respHeaders = null, _dumpParams = false) => {
288
297
  catch (e) { }
289
298
  params.assert ??= true; // default is true
290
299
  if (!r.ok && params.assert) {
291
- throw createHttpError(r.status, null, body);
300
+ throw createHttpError(r.status, null, body, {
301
+ method: params.method,
302
+ path: params.path,
303
+ response: {
304
+ status: r.status,
305
+ statusText: r.statusText,
306
+ headers,
307
+ },
308
+ });
292
309
  }
293
310
  return body;
294
311
  };
package/dist/index.js CHANGED
@@ -104,9 +104,10 @@ class HTTP_STATUS {
104
104
  // opinionated base for all
105
105
  class HttpError extends Error {
106
106
  name = 'HttpError';
107
+ // props simulating fetch Response
107
108
  status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
108
109
  statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
109
- detail = null;
110
+ body = null;
110
111
  }
111
112
  // some more specific instances of the well known ones...
112
113
  // client
@@ -211,10 +212,11 @@ const _wellKnownCtorMap = {
211
212
  '503': ServiceUnavailable,
212
213
  };
213
214
  const createHttpError = (code, message,
214
- // arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
215
- detail,
215
+ // arbitrary content, typically http response body which threw this error
216
+ // (will be JSON.parse-d if the content is a valid json string)
217
+ body,
216
218
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
217
- // arbitrary further details provided manually
219
+ // arbitrary details, typically response text (will be JSON.parse-d if text is a valid json string)
218
220
  cause) => {
219
221
  const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
220
222
  code = Number(code);
@@ -223,10 +225,18 @@ cause) => {
223
225
  const found = HTTP_STATUS.findByCode(code);
224
226
  const statusText = found?.TEXT ?? fallback.TEXT;
225
227
  // opinionated convention
226
- if (typeof detail === 'string') {
228
+ if (typeof body === 'string') {
227
229
  // prettier-ignore
228
230
  try {
229
- detail = JSON.parse(detail);
231
+ body = JSON.parse(body);
232
+ }
233
+ catch (e) { }
234
+ }
235
+ // opinionated convention
236
+ if (typeof cause === 'string') {
237
+ // prettier-ignore
238
+ try {
239
+ cause = JSON.parse(cause);
230
240
  }
231
241
  catch (e) { }
232
242
  }
@@ -235,7 +245,7 @@ cause) => {
235
245
  let e = new ctor(message || statusText, { cause });
236
246
  e.status = found?.CODE ?? fallback.CODE;
237
247
  e.statusText = statusText;
238
- e.detail = detail;
248
+ e.body = body;
239
249
  return e;
240
250
  };
241
251
 
@@ -269,14 +279,13 @@ const _fetch = async (params, respHeaders = null, _dumpParams = false) => {
269
279
  const r = await _fetchRaw(params);
270
280
  if (params.raw)
271
281
  return r;
282
+ //
283
+ const headers = [...r.headers.entries()].reduce((m, [k, v]) => ({ ...m, [k]: v }), {});
272
284
  // quick-n-dirty reference to headers (so it's still accessible over this api wrap)
273
285
  if (respHeaders) {
274
- Object.assign(respHeaders, [...r.headers.entries()].reduce((m, [k, v]) => ({ ...m, [k]: v }), {}),
286
+ Object.assign(respHeaders, { ...headers },
275
287
  // adding status/text under special keys
276
- {
277
- __http_status_code__: r.status,
278
- __http_status_text__: r.statusText,
279
- });
288
+ { __http_status_code__: r.status, __http_status_text__: r.statusText });
280
289
  }
281
290
  let body = await r.text();
282
291
  // prettier-ignore
@@ -286,7 +295,15 @@ const _fetch = async (params, respHeaders = null, _dumpParams = false) => {
286
295
  catch (e) { }
287
296
  params.assert ??= true; // default is true
288
297
  if (!r.ok && params.assert) {
289
- throw createHttpError(r.status, null, body);
298
+ throw createHttpError(r.status, null, body, {
299
+ method: params.method,
300
+ path: params.path,
301
+ response: {
302
+ status: r.status,
303
+ statusText: r.statusText,
304
+ headers,
305
+ },
306
+ });
290
307
  }
291
308
  return body;
292
309
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/http-utils",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "Misc DRY http fetch related helpers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",