@marianmeres/http-utils 1.16.0 → 1.19.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/index.cjs +15 -6
- package/dist/index.js +15 -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/index.cjs
CHANGED
|
@@ -309,7 +309,7 @@ const getErrorMessage = (e, stripErrorPrefix = true) => {
|
|
|
309
309
|
// ensure we're sending string
|
|
310
310
|
msg = `${msg}`;
|
|
311
311
|
if (stripErrorPrefix) {
|
|
312
|
-
msg = msg.replace(/^[^:]*Error:
|
|
312
|
+
msg = msg.replace(/^[^:]*Error: /i, '');
|
|
313
313
|
}
|
|
314
314
|
return msg;
|
|
315
315
|
};
|
|
@@ -370,6 +370,7 @@ const _fetch = async (params, respHeaders = null, errorMessageExtractor = null,
|
|
|
370
370
|
// try opinionated convention first
|
|
371
371
|
_body?.error?.message ||
|
|
372
372
|
_body?.message ||
|
|
373
|
+
_body?.error ||
|
|
373
374
|
_response?.statusText ||
|
|
374
375
|
'Unknown error';
|
|
375
376
|
if (msg.length > 255)
|
|
@@ -403,33 +404,41 @@ function createHttpApi(base, defaults, factoryErrorMessageExtractor) {
|
|
|
403
404
|
resolve({ ...(defaults || {}) });
|
|
404
405
|
}
|
|
405
406
|
});
|
|
407
|
+
const _buildPath = (path, base) => {
|
|
408
|
+
base = `${base || ''}`;
|
|
409
|
+
path = `${path || ''}`;
|
|
410
|
+
return /^https?:/.test(path) ? path : base + path;
|
|
411
|
+
};
|
|
406
412
|
return {
|
|
407
413
|
// GET
|
|
408
414
|
async get(path, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
409
|
-
path =
|
|
415
|
+
path = _buildPath(path, base);
|
|
410
416
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
411
417
|
},
|
|
412
418
|
// POST
|
|
413
419
|
async post(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
414
|
-
path =
|
|
420
|
+
path = _buildPath(path, base);
|
|
415
421
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
416
422
|
},
|
|
417
423
|
// PUT
|
|
418
424
|
async put(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
419
|
-
path =
|
|
425
|
+
path = _buildPath(path, base);
|
|
420
426
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
421
427
|
},
|
|
422
428
|
// PATCH
|
|
423
429
|
async patch(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
424
|
-
path =
|
|
430
|
+
path = _buildPath(path, base);
|
|
425
431
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
426
432
|
},
|
|
427
433
|
// DELETE
|
|
428
434
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
429
435
|
async del(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
430
|
-
path =
|
|
436
|
+
path = _buildPath(path, base);
|
|
431
437
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
432
438
|
},
|
|
439
|
+
// helper method to return api's resolved url
|
|
440
|
+
// note: cannot use URL(...) as relative would be invalid
|
|
441
|
+
url: (path) => _buildPath(path, base),
|
|
433
442
|
};
|
|
434
443
|
}
|
|
435
444
|
createHttpApi.defaultErrorMessageExtractor = null;
|
package/dist/index.js
CHANGED
|
@@ -307,7 +307,7 @@ const getErrorMessage = (e, stripErrorPrefix = true) => {
|
|
|
307
307
|
// ensure we're sending string
|
|
308
308
|
msg = `${msg}`;
|
|
309
309
|
if (stripErrorPrefix) {
|
|
310
|
-
msg = msg.replace(/^[^:]*Error:
|
|
310
|
+
msg = msg.replace(/^[^:]*Error: /i, '');
|
|
311
311
|
}
|
|
312
312
|
return msg;
|
|
313
313
|
};
|
|
@@ -368,6 +368,7 @@ const _fetch = async (params, respHeaders = null, errorMessageExtractor = null,
|
|
|
368
368
|
// try opinionated convention first
|
|
369
369
|
_body?.error?.message ||
|
|
370
370
|
_body?.message ||
|
|
371
|
+
_body?.error ||
|
|
371
372
|
_response?.statusText ||
|
|
372
373
|
'Unknown error';
|
|
373
374
|
if (msg.length > 255)
|
|
@@ -401,33 +402,41 @@ function createHttpApi(base, defaults, factoryErrorMessageExtractor) {
|
|
|
401
402
|
resolve({ ...(defaults || {}) });
|
|
402
403
|
}
|
|
403
404
|
});
|
|
405
|
+
const _buildPath = (path, base) => {
|
|
406
|
+
base = `${base || ''}`;
|
|
407
|
+
path = `${path || ''}`;
|
|
408
|
+
return /^https?:/.test(path) ? path : base + path;
|
|
409
|
+
};
|
|
404
410
|
return {
|
|
405
411
|
// GET
|
|
406
412
|
async get(path, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
407
|
-
path =
|
|
413
|
+
path = _buildPath(path, base);
|
|
408
414
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
409
415
|
},
|
|
410
416
|
// POST
|
|
411
417
|
async post(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
412
|
-
path =
|
|
418
|
+
path = _buildPath(path, base);
|
|
413
419
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
414
420
|
},
|
|
415
421
|
// PUT
|
|
416
422
|
async put(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
417
|
-
path =
|
|
423
|
+
path = _buildPath(path, base);
|
|
418
424
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
419
425
|
},
|
|
420
426
|
// PATCH
|
|
421
427
|
async patch(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
422
|
-
path =
|
|
428
|
+
path = _buildPath(path, base);
|
|
423
429
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
424
430
|
},
|
|
425
431
|
// DELETE
|
|
426
432
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
427
433
|
async del(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
428
|
-
path =
|
|
434
|
+
path = _buildPath(path, base);
|
|
429
435
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
430
436
|
},
|
|
437
|
+
// helper method to return api's resolved url
|
|
438
|
+
// note: cannot use URL(...) as relative would be invalid
|
|
439
|
+
url: (path) => _buildPath(path, base),
|
|
431
440
|
};
|
|
432
441
|
}
|
|
433
442
|
createHttpApi.defaultErrorMessageExtractor = null;
|