@marianmeres/http-utils 1.3.2 → 1.4.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/README.md CHANGED
@@ -30,7 +30,7 @@ try {
30
30
  assert(e.toString() === 'HttpNotFoundError: Not Found');
31
31
  assert(e.status === HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.CODE);
32
32
  assert(e.statusText === HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.TEXT);
33
- assert(e.body.message === 'hey');
33
+ assert(e.detail.message === 'hey');
34
34
  }
35
35
 
36
36
  // EXAMPLE: assuming `/foo` returns 404 header and json {"message":"hey"}
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
- body: string | null;
5
+ detail: 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, body?: 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, detail?: any, cause?: any) => BadRequest | Unauthorized | Forbidden | NotFound | MethodNotAllowed | RequestTimeout | Conflict | Gone | ImATeapot | InternalServerError | NotImplemented | BadGateway | ServiceUnavailable;
87
87
  export {};
package/dist/index.cjs CHANGED
@@ -108,7 +108,7 @@ class HttpError extends Error {
108
108
  name = 'HttpError';
109
109
  status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
110
110
  statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
111
- body = null;
111
+ detail = null;
112
112
  }
113
113
  // some more specific instances of the well known ones...
114
114
  // client
@@ -212,8 +212,12 @@ const _wellKnownCtorMap = {
212
212
  '502': BadGateway,
213
213
  '503': ServiceUnavailable,
214
214
  };
215
+ const createHttpError = (code, message,
216
+ // arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
217
+ detail,
215
218
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
216
- const createHttpError = (code, message, body, cause) => {
219
+ // arbitrary further details provided manually
220
+ cause) => {
217
221
  const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
218
222
  code = Number(code);
219
223
  if (isNaN(code) || !(code >= 400 && code < 600))
@@ -221,10 +225,10 @@ const createHttpError = (code, message, body, cause) => {
221
225
  const found = HTTP_STATUS.findByCode(code);
222
226
  const statusText = found?.TEXT ?? fallback.TEXT;
223
227
  // opinionated convention
224
- if (typeof body === 'string') {
228
+ if (typeof detail === 'string') {
225
229
  // prettier-ignore
226
230
  try {
227
- body = JSON.parse(body);
231
+ detail = JSON.parse(detail);
228
232
  }
229
233
  catch (e) { }
230
234
  }
@@ -233,7 +237,7 @@ const createHttpError = (code, message, body, cause) => {
233
237
  let e = new ctor(message || statusText, { cause });
234
238
  e.status = found?.CODE ?? fallback.CODE;
235
239
  e.statusText = statusText;
236
- e.body = body;
240
+ e.detail = detail;
237
241
  return e;
238
242
  };
239
243
 
package/dist/index.js CHANGED
@@ -106,7 +106,7 @@ class HttpError extends Error {
106
106
  name = 'HttpError';
107
107
  status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
108
108
  statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
109
- body = null;
109
+ detail = null;
110
110
  }
111
111
  // some more specific instances of the well known ones...
112
112
  // client
@@ -210,8 +210,12 @@ const _wellKnownCtorMap = {
210
210
  '502': BadGateway,
211
211
  '503': ServiceUnavailable,
212
212
  };
213
+ const createHttpError = (code, message,
214
+ // arbitrary details, typically response text (will be JSON.parse-d if text is valid json string)
215
+ detail,
213
216
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
214
- const createHttpError = (code, message, body, cause) => {
217
+ // arbitrary further details provided manually
218
+ cause) => {
215
219
  const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
216
220
  code = Number(code);
217
221
  if (isNaN(code) || !(code >= 400 && code < 600))
@@ -219,10 +223,10 @@ const createHttpError = (code, message, body, cause) => {
219
223
  const found = HTTP_STATUS.findByCode(code);
220
224
  const statusText = found?.TEXT ?? fallback.TEXT;
221
225
  // opinionated convention
222
- if (typeof body === 'string') {
226
+ if (typeof detail === 'string') {
223
227
  // prettier-ignore
224
228
  try {
225
- body = JSON.parse(body);
229
+ detail = JSON.parse(detail);
226
230
  }
227
231
  catch (e) { }
228
232
  }
@@ -231,7 +235,7 @@ const createHttpError = (code, message, body, cause) => {
231
235
  let e = new ctor(message || statusText, { cause });
232
236
  e.status = found?.CODE ?? fallback.CODE;
233
237
  e.statusText = statusText;
234
- e.body = body;
238
+ e.detail = detail;
235
239
  return e;
236
240
  };
237
241
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/http-utils",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "Misc DRY http fetch related helpers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",