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