@marianmeres/http-utils 1.6.0 → 1.6.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 +9 -1
- package/dist/api.d.ts +5 -5
- package/dist/index.cjs +10 -8
- package/dist/index.js +10 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,7 +30,10 @@ 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
|
-
|
|
33
|
+
// `body` is a custom prop containing the raw http response body text (JSON.parse-d if available)
|
|
34
|
+
assert(e.body.message === 'hey');
|
|
35
|
+
// `cause` is a standart Error prop, containg here some default debug info
|
|
36
|
+
assert(err.cause.response.headers)
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
// EXAMPLE: assuming `/foo` returns 404 header and json {"message":"hey"}
|
|
@@ -46,6 +49,11 @@ assert(r.message === 'created');
|
|
|
46
49
|
// EXAMPLE: raw Response
|
|
47
50
|
const r = await api.get('/resource', { raw: true });
|
|
48
51
|
assert(r instanceof Response);
|
|
52
|
+
|
|
53
|
+
// EXAMPLE: access to response headers
|
|
54
|
+
let respHeaders = {};
|
|
55
|
+
const r = await api.get('/resource', null, respHeaders);
|
|
56
|
+
assert(Object.keys(respHeaders).length)
|
|
49
57
|
```
|
|
50
58
|
|
|
51
59
|
See [`HTTP_STATUS`](./src/status.ts) and [`HTTP_ERROR`](./src/error.ts) for more.
|
package/dist/api.d.ts
CHANGED
|
@@ -13,10 +13,10 @@ interface FetchParams {
|
|
|
13
13
|
}
|
|
14
14
|
type BaseFetchParams = BaseParams & FetchParams;
|
|
15
15
|
export declare const createHttpApi: (base?: string | null, defaults?: Partial<BaseFetchParams> | (() => Promise<Partial<BaseFetchParams>>)) => {
|
|
16
|
-
get(path: string, params?: FetchParams, respHeaders?:
|
|
17
|
-
post(path: string, data?: null, params?: FetchParams, respHeaders?:
|
|
18
|
-
put(path: string, data?: null, params?: FetchParams, respHeaders?:
|
|
19
|
-
patch(path: string, data?: null, params?: FetchParams, respHeaders?:
|
|
20
|
-
del(path: string, data?: null, params?: FetchParams, respHeaders?:
|
|
16
|
+
get(path: string, params?: FetchParams, respHeaders?: any, _dumpParams?: boolean): Promise<any>;
|
|
17
|
+
post(path: string, data?: null, params?: FetchParams, respHeaders?: any, _dumpParams?: boolean): Promise<any>;
|
|
18
|
+
put(path: string, data?: null, params?: FetchParams, respHeaders?: any, _dumpParams?: boolean): Promise<any>;
|
|
19
|
+
patch(path: string, data?: null, params?: FetchParams, respHeaders?: any, _dumpParams?: boolean): Promise<any>;
|
|
20
|
+
del(path: string, data?: null, params?: FetchParams, respHeaders?: any, _dumpParams?: boolean): Promise<any>;
|
|
21
21
|
};
|
|
22
22
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -218,14 +218,12 @@ const createHttpError = (code, message,
|
|
|
218
218
|
// (will be JSON.parse-d if the content is a valid json string)
|
|
219
219
|
body,
|
|
220
220
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
221
|
-
// arbitrary details, typically response text (will be JSON.parse-d if
|
|
221
|
+
// arbitrary details, typically response text (will be JSON.parse-d if the content is a valid json string)
|
|
222
222
|
cause) => {
|
|
223
223
|
const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
|
|
224
224
|
code = Number(code);
|
|
225
225
|
if (isNaN(code) || !(code >= 400 && code < 600))
|
|
226
226
|
code = fallback.CODE;
|
|
227
|
-
const found = HTTP_STATUS.findByCode(code);
|
|
228
|
-
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
229
227
|
// opinionated convention
|
|
230
228
|
if (typeof body === 'string') {
|
|
231
229
|
// prettier-ignore
|
|
@@ -244,6 +242,10 @@ cause) => {
|
|
|
244
242
|
}
|
|
245
243
|
// try to find the well known one, otherwise fallback to generic
|
|
246
244
|
const ctor = _wellKnownCtorMap[`${code}`] ?? HttpError;
|
|
245
|
+
//
|
|
246
|
+
const found = HTTP_STATUS.findByCode(code);
|
|
247
|
+
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
248
|
+
//
|
|
247
249
|
let e = new ctor(message || statusText, { cause });
|
|
248
250
|
e.status = found?.CODE ?? fallback.CODE;
|
|
249
251
|
e.statusText = statusText;
|
|
@@ -326,28 +328,28 @@ const createHttpApi = (base, defaults) => {
|
|
|
326
328
|
return {
|
|
327
329
|
// GET
|
|
328
330
|
async get(path, params, respHeaders = null, _dumpParams = false) {
|
|
329
|
-
path = `${base || ''}
|
|
331
|
+
path = `${base || ''}${path || ''}`;
|
|
330
332
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, _dumpParams);
|
|
331
333
|
},
|
|
332
334
|
// POST
|
|
333
335
|
async post(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
334
|
-
path = `${base || ''}
|
|
336
|
+
path = `${base || ''}${path || ''}`;
|
|
335
337
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, _dumpParams);
|
|
336
338
|
},
|
|
337
339
|
// PUT
|
|
338
340
|
async put(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
339
|
-
path = `${base || ''}
|
|
341
|
+
path = `${base || ''}${path || ''}`;
|
|
340
342
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, _dumpParams);
|
|
341
343
|
},
|
|
342
344
|
// PATCH
|
|
343
345
|
async patch(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
344
|
-
path = `${base || ''}
|
|
346
|
+
path = `${base || ''}${path || ''}`;
|
|
345
347
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, _dumpParams);
|
|
346
348
|
},
|
|
347
349
|
// DELETE
|
|
348
350
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
349
351
|
async del(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
350
|
-
path = `${base || ''}
|
|
352
|
+
path = `${base || ''}${path || ''}`;
|
|
351
353
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, _dumpParams);
|
|
352
354
|
},
|
|
353
355
|
};
|
package/dist/index.js
CHANGED
|
@@ -216,14 +216,12 @@ const createHttpError = (code, message,
|
|
|
216
216
|
// (will be JSON.parse-d if the content is a valid json string)
|
|
217
217
|
body,
|
|
218
218
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
219
|
-
// arbitrary details, typically response text (will be JSON.parse-d if
|
|
219
|
+
// arbitrary details, typically response text (will be JSON.parse-d if the content is a valid json string)
|
|
220
220
|
cause) => {
|
|
221
221
|
const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
|
|
222
222
|
code = Number(code);
|
|
223
223
|
if (isNaN(code) || !(code >= 400 && code < 600))
|
|
224
224
|
code = fallback.CODE;
|
|
225
|
-
const found = HTTP_STATUS.findByCode(code);
|
|
226
|
-
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
227
225
|
// opinionated convention
|
|
228
226
|
if (typeof body === 'string') {
|
|
229
227
|
// prettier-ignore
|
|
@@ -242,6 +240,10 @@ cause) => {
|
|
|
242
240
|
}
|
|
243
241
|
// try to find the well known one, otherwise fallback to generic
|
|
244
242
|
const ctor = _wellKnownCtorMap[`${code}`] ?? HttpError;
|
|
243
|
+
//
|
|
244
|
+
const found = HTTP_STATUS.findByCode(code);
|
|
245
|
+
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
246
|
+
//
|
|
245
247
|
let e = new ctor(message || statusText, { cause });
|
|
246
248
|
e.status = found?.CODE ?? fallback.CODE;
|
|
247
249
|
e.statusText = statusText;
|
|
@@ -324,28 +326,28 @@ const createHttpApi = (base, defaults) => {
|
|
|
324
326
|
return {
|
|
325
327
|
// GET
|
|
326
328
|
async get(path, params, respHeaders = null, _dumpParams = false) {
|
|
327
|
-
path = `${base || ''}
|
|
329
|
+
path = `${base || ''}${path || ''}`;
|
|
328
330
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, _dumpParams);
|
|
329
331
|
},
|
|
330
332
|
// POST
|
|
331
333
|
async post(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
332
|
-
path = `${base || ''}
|
|
334
|
+
path = `${base || ''}${path || ''}`;
|
|
333
335
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, _dumpParams);
|
|
334
336
|
},
|
|
335
337
|
// PUT
|
|
336
338
|
async put(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
337
|
-
path = `${base || ''}
|
|
339
|
+
path = `${base || ''}${path || ''}`;
|
|
338
340
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, _dumpParams);
|
|
339
341
|
},
|
|
340
342
|
// PATCH
|
|
341
343
|
async patch(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
342
|
-
path = `${base || ''}
|
|
344
|
+
path = `${base || ''}${path || ''}`;
|
|
343
345
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, _dumpParams);
|
|
344
346
|
},
|
|
345
347
|
// DELETE
|
|
346
348
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
347
349
|
async del(path, data = null, params, respHeaders = null, _dumpParams = false) {
|
|
348
|
-
path = `${base || ''}
|
|
350
|
+
path = `${base || ''}${path || ''}`;
|
|
349
351
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, _dumpParams);
|
|
350
352
|
},
|
|
351
353
|
};
|