@marianmeres/http-utils 1.3.0 → 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 +2 -2
- package/dist/error.d.ts +2 -2
- package/dist/index.cjs +17 -15
- package/dist/index.js +17 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @marianmeres/http-utils
|
|
2
2
|
|
|
3
|
-
A few [sweet](https://en.wikipedia.org/wiki/Syntactic_sugar) `fetch` helpers.
|
|
3
|
+
A few opinionated [sweet](https://en.wikipedia.org/wiki/Syntactic_sugar) `fetch` helpers.
|
|
4
4
|
|
|
5
5
|
## Example
|
|
6
6
|
|
|
@@ -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.
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
228
|
+
if (typeof detail === 'string') {
|
|
225
229
|
// prettier-ignore
|
|
226
230
|
try {
|
|
227
|
-
|
|
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.
|
|
240
|
+
e.detail = detail;
|
|
237
241
|
return e;
|
|
238
242
|
};
|
|
239
243
|
|
|
@@ -294,16 +298,14 @@ const createHttpApi = (base, defaults) => {
|
|
|
294
298
|
merge.dset(wrap, 'result', b);
|
|
295
299
|
return wrap.result;
|
|
296
300
|
};
|
|
297
|
-
const _getDefs = async () => {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
});
|
|
306
|
-
};
|
|
301
|
+
const _getDefs = async () => new Promise(async (resolve) => {
|
|
302
|
+
if (typeof defaults === 'function') {
|
|
303
|
+
resolve({ ...(await defaults()) });
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
resolve({ ...(defaults || {}) });
|
|
307
|
+
}
|
|
308
|
+
});
|
|
307
309
|
return {
|
|
308
310
|
// GET
|
|
309
311
|
async get(path, params, respHeaders = null, _dumpParams = false) {
|
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
|
-
|
|
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
|
-
|
|
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
|
|
226
|
+
if (typeof detail === 'string') {
|
|
223
227
|
// prettier-ignore
|
|
224
228
|
try {
|
|
225
|
-
|
|
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.
|
|
238
|
+
e.detail = detail;
|
|
235
239
|
return e;
|
|
236
240
|
};
|
|
237
241
|
|
|
@@ -292,16 +296,14 @@ const createHttpApi = (base, defaults) => {
|
|
|
292
296
|
dset(wrap, 'result', b);
|
|
293
297
|
return wrap.result;
|
|
294
298
|
};
|
|
295
|
-
const _getDefs = async () => {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
});
|
|
304
|
-
};
|
|
299
|
+
const _getDefs = async () => new Promise(async (resolve) => {
|
|
300
|
+
if (typeof defaults === 'function') {
|
|
301
|
+
resolve({ ...(await defaults()) });
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
resolve({ ...(defaults || {}) });
|
|
305
|
+
}
|
|
306
|
+
});
|
|
305
307
|
return {
|
|
306
308
|
// GET
|
|
307
309
|
async get(path, params, respHeaders = null, _dumpParams = false) {
|