@marianmeres/http-utils 1.18.0 → 1.20.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/api.d.ts +1 -0
- package/dist/error.d.ts +6 -0
- package/dist/index.cjs +21 -6
- package/dist/index.js +21 -6
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export declare function createHttpApi(base?: string | null, defaults?: Partial<B
|
|
|
19
19
|
put(path: string, data?: any, params?: FetchParams, respHeaders?: any, errorMessageExtractor?: ErrorMessageExtractor | null | undefined, _dumpParams?: boolean): Promise<any>;
|
|
20
20
|
patch(path: string, data?: any, params?: FetchParams, respHeaders?: any, errorMessageExtractor?: ErrorMessageExtractor | null | undefined, _dumpParams?: boolean): Promise<any>;
|
|
21
21
|
del(path: string, data?: any, params?: FetchParams, respHeaders?: any, errorMessageExtractor?: ErrorMessageExtractor | null | undefined, _dumpParams?: boolean): Promise<any>;
|
|
22
|
+
url: (path: string) => string;
|
|
22
23
|
};
|
|
23
24
|
export declare namespace createHttpApi {
|
|
24
25
|
var defaultErrorMessageExtractor: ErrorMessageExtractor | null | undefined;
|
package/dist/error.d.ts
CHANGED
|
@@ -49,6 +49,11 @@ declare class UnprocessableContent extends HttpError {
|
|
|
49
49
|
status: number;
|
|
50
50
|
statusText: string;
|
|
51
51
|
}
|
|
52
|
+
declare class TooManyRequests extends HttpError {
|
|
53
|
+
name: string;
|
|
54
|
+
status: number;
|
|
55
|
+
statusText: string;
|
|
56
|
+
}
|
|
52
57
|
declare class ImATeapot extends HttpError {
|
|
53
58
|
name: string;
|
|
54
59
|
status: number;
|
|
@@ -84,6 +89,7 @@ export declare const HTTP_ERROR: {
|
|
|
84
89
|
Gone: typeof Gone;
|
|
85
90
|
ImATeapot: typeof ImATeapot;
|
|
86
91
|
UnprocessableContent: typeof UnprocessableContent;
|
|
92
|
+
TooManyRequests: typeof TooManyRequests;
|
|
87
93
|
InternalServerError: typeof InternalServerError;
|
|
88
94
|
NotImplemented: typeof NotImplemented;
|
|
89
95
|
BadGateway: typeof BadGateway;
|
package/dist/index.cjs
CHANGED
|
@@ -179,6 +179,11 @@ class UnprocessableContent extends HttpError {
|
|
|
179
179
|
status = HTTP_STATUS.ERROR_CLIENT.UNPROCESSABLE_CONTENT.CODE;
|
|
180
180
|
statusText = HTTP_STATUS.ERROR_CLIENT.UNPROCESSABLE_CONTENT.TEXT;
|
|
181
181
|
}
|
|
182
|
+
class TooManyRequests extends HttpError {
|
|
183
|
+
name = 'HttpTooManyRequestsError';
|
|
184
|
+
status = HTTP_STATUS.ERROR_CLIENT.TOO_MANY_REQUESTS.CODE;
|
|
185
|
+
statusText = HTTP_STATUS.ERROR_CLIENT.TOO_MANY_REQUESTS.TEXT;
|
|
186
|
+
}
|
|
182
187
|
class ImATeapot extends HttpError {
|
|
183
188
|
name = 'HttpImATeapotError';
|
|
184
189
|
status = HTTP_STATUS.ERROR_CLIENT.IM_A_TEAPOT.CODE;
|
|
@@ -218,6 +223,7 @@ const HTTP_ERROR = {
|
|
|
218
223
|
Gone,
|
|
219
224
|
ImATeapot,
|
|
220
225
|
UnprocessableContent,
|
|
226
|
+
TooManyRequests,
|
|
221
227
|
// server
|
|
222
228
|
InternalServerError,
|
|
223
229
|
NotImplemented,
|
|
@@ -235,6 +241,7 @@ const _wellKnownCtorMap = {
|
|
|
235
241
|
'410': Gone,
|
|
236
242
|
'418': ImATeapot,
|
|
237
243
|
'422': UnprocessableContent,
|
|
244
|
+
'429': TooManyRequests,
|
|
238
245
|
//
|
|
239
246
|
'500': InternalServerError,
|
|
240
247
|
'501': NotImplemented,
|
|
@@ -309,7 +316,7 @@ const getErrorMessage = (e, stripErrorPrefix = true) => {
|
|
|
309
316
|
// ensure we're sending string
|
|
310
317
|
msg = `${msg}`;
|
|
311
318
|
if (stripErrorPrefix) {
|
|
312
|
-
msg = msg.replace(/^[^:]*Error:
|
|
319
|
+
msg = msg.replace(/^[^:]*Error: /i, '');
|
|
313
320
|
}
|
|
314
321
|
return msg;
|
|
315
322
|
};
|
|
@@ -404,33 +411,41 @@ function createHttpApi(base, defaults, factoryErrorMessageExtractor) {
|
|
|
404
411
|
resolve({ ...(defaults || {}) });
|
|
405
412
|
}
|
|
406
413
|
});
|
|
414
|
+
const _buildPath = (path, base) => {
|
|
415
|
+
base = `${base || ''}`;
|
|
416
|
+
path = `${path || ''}`;
|
|
417
|
+
return /^https?:/.test(path) ? path : base + path;
|
|
418
|
+
};
|
|
407
419
|
return {
|
|
408
420
|
// GET
|
|
409
421
|
async get(path, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
410
|
-
path =
|
|
422
|
+
path = _buildPath(path, base);
|
|
411
423
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
412
424
|
},
|
|
413
425
|
// POST
|
|
414
426
|
async post(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
415
|
-
path =
|
|
427
|
+
path = _buildPath(path, base);
|
|
416
428
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
417
429
|
},
|
|
418
430
|
// PUT
|
|
419
431
|
async put(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
420
|
-
path =
|
|
432
|
+
path = _buildPath(path, base);
|
|
421
433
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
422
434
|
},
|
|
423
435
|
// PATCH
|
|
424
436
|
async patch(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
425
|
-
path =
|
|
437
|
+
path = _buildPath(path, base);
|
|
426
438
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
427
439
|
},
|
|
428
440
|
// DELETE
|
|
429
441
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
430
442
|
async del(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
431
|
-
path =
|
|
443
|
+
path = _buildPath(path, base);
|
|
432
444
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
433
445
|
},
|
|
446
|
+
// helper method to return api's resolved url
|
|
447
|
+
// note: cannot use URL(...) as relative would be invalid
|
|
448
|
+
url: (path) => _buildPath(path, base),
|
|
434
449
|
};
|
|
435
450
|
}
|
|
436
451
|
createHttpApi.defaultErrorMessageExtractor = null;
|
package/dist/index.js
CHANGED
|
@@ -177,6 +177,11 @@ class UnprocessableContent extends HttpError {
|
|
|
177
177
|
status = HTTP_STATUS.ERROR_CLIENT.UNPROCESSABLE_CONTENT.CODE;
|
|
178
178
|
statusText = HTTP_STATUS.ERROR_CLIENT.UNPROCESSABLE_CONTENT.TEXT;
|
|
179
179
|
}
|
|
180
|
+
class TooManyRequests extends HttpError {
|
|
181
|
+
name = 'HttpTooManyRequestsError';
|
|
182
|
+
status = HTTP_STATUS.ERROR_CLIENT.TOO_MANY_REQUESTS.CODE;
|
|
183
|
+
statusText = HTTP_STATUS.ERROR_CLIENT.TOO_MANY_REQUESTS.TEXT;
|
|
184
|
+
}
|
|
180
185
|
class ImATeapot extends HttpError {
|
|
181
186
|
name = 'HttpImATeapotError';
|
|
182
187
|
status = HTTP_STATUS.ERROR_CLIENT.IM_A_TEAPOT.CODE;
|
|
@@ -216,6 +221,7 @@ const HTTP_ERROR = {
|
|
|
216
221
|
Gone,
|
|
217
222
|
ImATeapot,
|
|
218
223
|
UnprocessableContent,
|
|
224
|
+
TooManyRequests,
|
|
219
225
|
// server
|
|
220
226
|
InternalServerError,
|
|
221
227
|
NotImplemented,
|
|
@@ -233,6 +239,7 @@ const _wellKnownCtorMap = {
|
|
|
233
239
|
'410': Gone,
|
|
234
240
|
'418': ImATeapot,
|
|
235
241
|
'422': UnprocessableContent,
|
|
242
|
+
'429': TooManyRequests,
|
|
236
243
|
//
|
|
237
244
|
'500': InternalServerError,
|
|
238
245
|
'501': NotImplemented,
|
|
@@ -307,7 +314,7 @@ const getErrorMessage = (e, stripErrorPrefix = true) => {
|
|
|
307
314
|
// ensure we're sending string
|
|
308
315
|
msg = `${msg}`;
|
|
309
316
|
if (stripErrorPrefix) {
|
|
310
|
-
msg = msg.replace(/^[^:]*Error:
|
|
317
|
+
msg = msg.replace(/^[^:]*Error: /i, '');
|
|
311
318
|
}
|
|
312
319
|
return msg;
|
|
313
320
|
};
|
|
@@ -402,33 +409,41 @@ function createHttpApi(base, defaults, factoryErrorMessageExtractor) {
|
|
|
402
409
|
resolve({ ...(defaults || {}) });
|
|
403
410
|
}
|
|
404
411
|
});
|
|
412
|
+
const _buildPath = (path, base) => {
|
|
413
|
+
base = `${base || ''}`;
|
|
414
|
+
path = `${path || ''}`;
|
|
415
|
+
return /^https?:/.test(path) ? path : base + path;
|
|
416
|
+
};
|
|
405
417
|
return {
|
|
406
418
|
// GET
|
|
407
419
|
async get(path, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
408
|
-
path =
|
|
420
|
+
path = _buildPath(path, base);
|
|
409
421
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
410
422
|
},
|
|
411
423
|
// POST
|
|
412
424
|
async post(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
413
|
-
path =
|
|
425
|
+
path = _buildPath(path, base);
|
|
414
426
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
415
427
|
},
|
|
416
428
|
// PUT
|
|
417
429
|
async put(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
418
|
-
path =
|
|
430
|
+
path = _buildPath(path, base);
|
|
419
431
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
420
432
|
},
|
|
421
433
|
// PATCH
|
|
422
434
|
async patch(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
423
|
-
path =
|
|
435
|
+
path = _buildPath(path, base);
|
|
424
436
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
425
437
|
},
|
|
426
438
|
// DELETE
|
|
427
439
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
428
440
|
async del(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
429
|
-
path =
|
|
441
|
+
path = _buildPath(path, base);
|
|
430
442
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
431
443
|
},
|
|
444
|
+
// helper method to return api's resolved url
|
|
445
|
+
// note: cannot use URL(...) as relative would be invalid
|
|
446
|
+
url: (path) => _buildPath(path, base),
|
|
432
447
|
};
|
|
433
448
|
}
|
|
434
449
|
createHttpApi.defaultErrorMessageExtractor = null;
|