@marianmeres/http-utils 1.18.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 +14 -6
- package/dist/index.js +14 -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
|
};
|
|
@@ -404,33 +404,41 @@ function createHttpApi(base, defaults, factoryErrorMessageExtractor) {
|
|
|
404
404
|
resolve({ ...(defaults || {}) });
|
|
405
405
|
}
|
|
406
406
|
});
|
|
407
|
+
const _buildPath = (path, base) => {
|
|
408
|
+
base = `${base || ''}`;
|
|
409
|
+
path = `${path || ''}`;
|
|
410
|
+
return /^https?:/.test(path) ? path : base + path;
|
|
411
|
+
};
|
|
407
412
|
return {
|
|
408
413
|
// GET
|
|
409
414
|
async get(path, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
410
|
-
path =
|
|
415
|
+
path = _buildPath(path, base);
|
|
411
416
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
412
417
|
},
|
|
413
418
|
// POST
|
|
414
419
|
async post(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
415
|
-
path =
|
|
420
|
+
path = _buildPath(path, base);
|
|
416
421
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
417
422
|
},
|
|
418
423
|
// PUT
|
|
419
424
|
async put(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
420
|
-
path =
|
|
425
|
+
path = _buildPath(path, base);
|
|
421
426
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
422
427
|
},
|
|
423
428
|
// PATCH
|
|
424
429
|
async patch(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
425
|
-
path =
|
|
430
|
+
path = _buildPath(path, base);
|
|
426
431
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
427
432
|
},
|
|
428
433
|
// DELETE
|
|
429
434
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
430
435
|
async del(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
431
|
-
path =
|
|
436
|
+
path = _buildPath(path, base);
|
|
432
437
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
433
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),
|
|
434
442
|
};
|
|
435
443
|
}
|
|
436
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
|
};
|
|
@@ -402,33 +402,41 @@ function createHttpApi(base, defaults, factoryErrorMessageExtractor) {
|
|
|
402
402
|
resolve({ ...(defaults || {}) });
|
|
403
403
|
}
|
|
404
404
|
});
|
|
405
|
+
const _buildPath = (path, base) => {
|
|
406
|
+
base = `${base || ''}`;
|
|
407
|
+
path = `${path || ''}`;
|
|
408
|
+
return /^https?:/.test(path) ? path : base + path;
|
|
409
|
+
};
|
|
405
410
|
return {
|
|
406
411
|
// GET
|
|
407
412
|
async get(path, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
408
|
-
path =
|
|
413
|
+
path = _buildPath(path, base);
|
|
409
414
|
return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
410
415
|
},
|
|
411
416
|
// POST
|
|
412
417
|
async post(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
413
|
-
path =
|
|
418
|
+
path = _buildPath(path, base);
|
|
414
419
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
415
420
|
},
|
|
416
421
|
// PUT
|
|
417
422
|
async put(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
418
|
-
path =
|
|
423
|
+
path = _buildPath(path, base);
|
|
419
424
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
420
425
|
},
|
|
421
426
|
// PATCH
|
|
422
427
|
async patch(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
423
|
-
path =
|
|
428
|
+
path = _buildPath(path, base);
|
|
424
429
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
425
430
|
},
|
|
426
431
|
// DELETE
|
|
427
432
|
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
428
433
|
async del(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
|
|
429
|
-
path =
|
|
434
|
+
path = _buildPath(path, base);
|
|
430
435
|
return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
|
|
431
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),
|
|
432
440
|
};
|
|
433
441
|
}
|
|
434
442
|
createHttpApi.defaultErrorMessageExtractor = null;
|