@marianmeres/http-utils 1.23.0 → 2.0.1

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/index.js DELETED
@@ -1,466 +0,0 @@
1
- import { dset } from 'dset/merge';
2
-
3
- // prettier-ignore
4
- class HTTP_STATUS {
5
- // full db
6
- // 1xx
7
- static INFO = {
8
- CONTINUE: { CODE: 100, TEXT: 'Continue' },
9
- SWITCHING_PROTOCOLS: { CODE: 101, TEXT: 'Switching Protocols' },
10
- PROCESSING: { CODE: 102, TEXT: 'Processing' },
11
- EARLY_HINTS: { CODE: 103, TEXT: 'Early Hints' },
12
- };
13
- // 2xx
14
- static SUCCESS = {
15
- OK: { CODE: 200, TEXT: 'OK' },
16
- CREATED: { CODE: 201, TEXT: 'Created' },
17
- ACCEPTED: { CODE: 202, TEXT: 'Accepted' },
18
- NON_AUTHORITATIVE_INFO: { CODE: 203, TEXT: 'Non-Authoritative Information' },
19
- NO_CONTENT: { CODE: 204, TEXT: 'No Content' },
20
- RESET_CONTENT: { CODE: 205, TEXT: 'Reset Content' },
21
- PARTIAL_CONTENT: { CODE: 206, TEXT: 'Partial Content' },
22
- MULTI_STATUS: { CODE: 207, TEXT: 'Multi-Status' },
23
- ALREADY_REPORTED: { CODE: 208, TEXT: 'Already Reported' },
24
- IM_USED: { CODE: 226, TEXT: 'IM Used' }, // ?
25
- };
26
- // 3xx
27
- static REDIRECT = {
28
- MUTLIPLE_CHOICES: { CODE: 300, TEXT: 'Multiple Choices' },
29
- MOVED_PERMANENTLY: { CODE: 301, TEXT: 'Moved Permanently' },
30
- FOUND: { CODE: 302, TEXT: 'Found' },
31
- SEE_OTHER: { CODE: 303, TEXT: 'See Other' },
32
- NOT_MODIFIED: { CODE: 304, TEXT: 'Not Modified' },
33
- TEMPORARY_REDIRECT: { CODE: 307, TEXT: 'Temporary Redirect' },
34
- PERMANENT_REDIRECT: { CODE: 308, TEXT: 'Permanent Redirect' },
35
- };
36
- // 4xx
37
- static ERROR_CLIENT = {
38
- BAD_REQUEST: { CODE: 400, TEXT: 'Bad Request' },
39
- UNAUTHORIZED: { CODE: 401, TEXT: 'Unauthorized' },
40
- PAYMENT_REQUIRED_EXPERIMENTAL: { CODE: 402, TEXT: 'Payment Required Experimental' },
41
- FORBIDDEN: { CODE: 403, TEXT: 'Forbidden' },
42
- NOT_FOUND: { CODE: 404, TEXT: 'Not Found' },
43
- METHOD_NOT_ALLOWED: { CODE: 405, TEXT: 'Method Not Allowed' },
44
- NOT_ACCEPTABLE: { CODE: 406, TEXT: 'Not Acceptable' },
45
- PROXY_AUTHENTICATION_REQUIRED: { CODE: 407, TEXT: 'Proxy Authentication Required' },
46
- REQUEST_TIMEOUT: { CODE: 408, TEXT: 'Request Timeout' },
47
- CONFLICT: { CODE: 409, TEXT: 'Conflict' },
48
- GONE: { CODE: 410, TEXT: 'Gone' },
49
- LENGTH_REQUIRED: { CODE: 411, TEXT: 'Length Required' },
50
- PRECONDITION_FAILED: { CODE: 412, TEXT: 'Precondition Failed' },
51
- PAYLOAD_TOO_LARGE: { CODE: 413, TEXT: 'Payload Too Large' },
52
- URI_TOO_LONG: { CODE: 414, TEXT: 'URI Too Long' },
53
- UNSUPPORTED_MEDIA_TYPE: { CODE: 415, TEXT: 'Unsupported Media Type' },
54
- RANGE_NOT_SATISFIABLE: { CODE: 416, TEXT: 'Range Not Satisfiable' },
55
- EXPECTATION_FAILED: { CODE: 417, TEXT: 'Expectation Failed' },
56
- IM_A_TEAPOT: { CODE: 418, TEXT: "I'm a teapot" },
57
- MISDIRECTED_REQUEST: { CODE: 421, TEXT: 'Misdirected Request' },
58
- UNPROCESSABLE_CONTENT: { CODE: 422, TEXT: 'Unprocessable Content' },
59
- LOCKED: { CODE: 423, TEXT: 'Locked' },
60
- FAILED_DEPENDENCY: { CODE: 424, TEXT: 'Failed Dependency' },
61
- TOO_EARLY_EXPERIMENTAL: { CODE: 425, TEXT: 'Too Early Experimental' },
62
- UPGRADE_REQUIRED: { CODE: 426, TEXT: 'Upgrade Required' },
63
- PRECONDITION_REQUIRED: { CODE: 428, TEXT: 'Precondition Required' },
64
- TOO_MANY_REQUESTS: { CODE: 429, TEXT: 'Too Many Requests' },
65
- REQUEST_HEADER_FIELDS_TOO_LARGE: { CODE: 431, TEXT: 'Request Header Fields Too Large' },
66
- UNAVAILABLE_FOR_LEGAL_REASONS: { CODE: 451, TEXT: 'Unavailable For Legal Reasons' },
67
- };
68
- // 5xx
69
- // prettier-ignore
70
- static ERROR_SERVER = {
71
- INTERNAL_SERVER_ERROR: { CODE: 500, TEXT: 'Internal Server Error' },
72
- NOT_IMPLEMENTED: { CODE: 501, TEXT: 'Not Implemented' },
73
- BAD_GATEWAY: { CODE: 502, TEXT: 'Bad Gateway' },
74
- SERVICE_UNAVAILABLE: { CODE: 503, TEXT: 'Service Unavailable' },
75
- GATEWAY_TIMEOUT: { CODE: 504, TEXT: 'Gateway Timeout' },
76
- HTTP_VERSION_NOT_SUPPORTED: { CODE: 505, TEXT: 'HTTP Version Not Supported' },
77
- VARIANT_ALSO_NEGOTIATES: { CODE: 506, TEXT: 'Variant Also Negotiates' },
78
- INSUFFICIENT_STORAGE: { CODE: 507, TEXT: 'Insufficient Storage' },
79
- LOOP_DETECTED: { CODE: 508, TEXT: 'Loop Detected' },
80
- NOT_EXTENDED: { CODE: 510, TEXT: 'Not Extended' },
81
- NETWORK_AUTH_REQUIRED: { CODE: 511, TEXT: 'Network Authentication Required' },
82
- };
83
- // few hand picked direct code shortcuts
84
- // 2xx
85
- static OK = HTTP_STATUS.SUCCESS.OK.CODE;
86
- static CREATED = HTTP_STATUS.SUCCESS.CREATED.CODE;
87
- static ACCEPTED = HTTP_STATUS.SUCCESS.ACCEPTED.CODE;
88
- static NO_CONTENT = HTTP_STATUS.SUCCESS.NO_CONTENT.CODE;
89
- // 3xx
90
- static MUTLIPLE_CHOICES = HTTP_STATUS.REDIRECT.MUTLIPLE_CHOICES.CODE;
91
- static FOUND = HTTP_STATUS.REDIRECT.FOUND.CODE;
92
- static NOT_MODIFIED = HTTP_STATUS.REDIRECT.NOT_MODIFIED.CODE;
93
- static MOVED_PERMANENTLY = HTTP_STATUS.REDIRECT.MOVED_PERMANENTLY.CODE;
94
- static TEMPORARY_REDIRECT = HTTP_STATUS.REDIRECT.TEMPORARY_REDIRECT.CODE;
95
- static PERMANENT_REDIRECT = HTTP_STATUS.REDIRECT.PERMANENT_REDIRECT.CODE;
96
- // 4xx
97
- static BAD_REQUEST = HTTP_STATUS.ERROR_CLIENT.BAD_REQUEST.CODE;
98
- static UNAUTHORIZED = HTTP_STATUS.ERROR_CLIENT.UNAUTHORIZED.CODE;
99
- static FORBIDDEN = HTTP_STATUS.ERROR_CLIENT.FORBIDDEN.CODE;
100
- static NOT_FOUND = HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.CODE;
101
- static METHOD_NOT_ALLOWED = HTTP_STATUS.ERROR_CLIENT.METHOD_NOT_ALLOWED.CODE;
102
- static CONFLICT = HTTP_STATUS.ERROR_CLIENT.CONFLICT.CODE;
103
- static GONE = HTTP_STATUS.ERROR_CLIENT.GONE.CODE;
104
- static UNPROCESSABLE_CONTENT = HTTP_STATUS.ERROR_CLIENT.UNPROCESSABLE_CONTENT.CODE;
105
- static TOO_MANY_REQUESTS = HTTP_STATUS.ERROR_CLIENT.TOO_MANY_REQUESTS.CODE;
106
- // 5xx
107
- static INTERNAL_SERVER_ERROR = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
108
- static NOT_IMPLEMENTED = HTTP_STATUS.ERROR_SERVER.NOT_IMPLEMENTED.CODE;
109
- static SERVICE_UNAVAILABLE = HTTP_STATUS.ERROR_SERVER.SERVICE_UNAVAILABLE.CODE;
110
- //
111
- static findByCode(code) {
112
- const keys = [
113
- 'INFO', 'SUCCESS', 'REDIRECT', 'ERROR_CLIENT', 'ERROR_SERVER',
114
- ];
115
- for (const _TYPE of keys) {
116
- for (const [_KEY, data] of Object.entries(HTTP_STATUS[_TYPE])) {
117
- if (data.CODE == code) {
118
- return { ...data, _TYPE, _KEY };
119
- }
120
- }
121
- }
122
- return null;
123
- }
124
- }
125
-
126
- // opinionated base for all
127
- class HttpError extends Error {
128
- name = 'HttpError';
129
- // props simulating fetch Response
130
- status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
131
- statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
132
- body = null;
133
- }
134
- // some more specific instances of the well known ones...
135
- // client
136
- class BadRequest extends HttpError {
137
- name = 'HttpBadRequestError';
138
- status = HTTP_STATUS.ERROR_CLIENT.BAD_REQUEST.CODE;
139
- statusText = HTTP_STATUS.ERROR_CLIENT.BAD_REQUEST.TEXT;
140
- }
141
- class Unauthorized extends HttpError {
142
- name = 'HttpUnauthorizedError';
143
- status = HTTP_STATUS.ERROR_CLIENT.UNAUTHORIZED.CODE;
144
- statusText = HTTP_STATUS.ERROR_CLIENT.UNAUTHORIZED.TEXT;
145
- }
146
- class Forbidden extends HttpError {
147
- name = 'HttpForbiddenError';
148
- status = HTTP_STATUS.ERROR_CLIENT.FORBIDDEN.CODE;
149
- statusText = HTTP_STATUS.ERROR_CLIENT.FORBIDDEN.TEXT;
150
- }
151
- class NotFound extends HttpError {
152
- name = 'HttpNotFoundError';
153
- status = HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.CODE;
154
- statusText = HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.TEXT;
155
- }
156
- class MethodNotAllowed extends HttpError {
157
- name = 'HttpMethodNotAllowedError';
158
- status = HTTP_STATUS.ERROR_CLIENT.METHOD_NOT_ALLOWED.CODE;
159
- statusText = HTTP_STATUS.ERROR_CLIENT.METHOD_NOT_ALLOWED.TEXT;
160
- }
161
- class RequestTimeout extends HttpError {
162
- name = 'HttpRequestTimeoutError';
163
- status = HTTP_STATUS.ERROR_CLIENT.REQUEST_TIMEOUT.CODE;
164
- statusText = HTTP_STATUS.ERROR_CLIENT.REQUEST_TIMEOUT.TEXT;
165
- }
166
- class Conflict extends HttpError {
167
- name = 'HttpConflictError';
168
- status = HTTP_STATUS.ERROR_CLIENT.CONFLICT.CODE;
169
- statusText = HTTP_STATUS.ERROR_CLIENT.CONFLICT.TEXT;
170
- }
171
- class Gone extends HttpError {
172
- name = 'HttpGoneError';
173
- status = HTTP_STATUS.ERROR_CLIENT.GONE.CODE;
174
- statusText = HTTP_STATUS.ERROR_CLIENT.GONE.TEXT;
175
- }
176
- class LengthRequired extends HttpError {
177
- name = 'HttpLengthRequiredError';
178
- status = HTTP_STATUS.ERROR_CLIENT.LENGTH_REQUIRED.CODE;
179
- statusText = HTTP_STATUS.ERROR_CLIENT.LENGTH_REQUIRED.TEXT;
180
- }
181
- class UnprocessableContent extends HttpError {
182
- name = 'HttpUnprocessableContentError';
183
- status = HTTP_STATUS.ERROR_CLIENT.UNPROCESSABLE_CONTENT.CODE;
184
- statusText = HTTP_STATUS.ERROR_CLIENT.UNPROCESSABLE_CONTENT.TEXT;
185
- }
186
- class TooManyRequests extends HttpError {
187
- name = 'HttpTooManyRequestsError';
188
- status = HTTP_STATUS.ERROR_CLIENT.TOO_MANY_REQUESTS.CODE;
189
- statusText = HTTP_STATUS.ERROR_CLIENT.TOO_MANY_REQUESTS.TEXT;
190
- }
191
- class ImATeapot extends HttpError {
192
- name = 'HttpImATeapotError';
193
- status = HTTP_STATUS.ERROR_CLIENT.IM_A_TEAPOT.CODE;
194
- statusText = HTTP_STATUS.ERROR_CLIENT.IM_A_TEAPOT.TEXT;
195
- }
196
- // server
197
- class InternalServerError extends HttpError {
198
- name = 'HttpInternalServerError';
199
- }
200
- class NotImplemented extends HttpError {
201
- name = 'HttpServiceUnavailableError';
202
- status = HTTP_STATUS.ERROR_SERVER.NOT_IMPLEMENTED.CODE;
203
- statusText = HTTP_STATUS.ERROR_SERVER.NOT_IMPLEMENTED.TEXT;
204
- }
205
- class BadGateway extends HttpError {
206
- name = 'HttpBadGatewayError';
207
- status = HTTP_STATUS.ERROR_SERVER.BAD_GATEWAY.CODE;
208
- statusText = HTTP_STATUS.ERROR_SERVER.BAD_GATEWAY.TEXT;
209
- }
210
- class ServiceUnavailable extends HttpError {
211
- name = 'HttpServiceUnavailableError';
212
- status = HTTP_STATUS.ERROR_SERVER.SERVICE_UNAVAILABLE.CODE;
213
- statusText = HTTP_STATUS.ERROR_SERVER.SERVICE_UNAVAILABLE.TEXT;
214
- }
215
- //
216
- const HTTP_ERROR = {
217
- // base
218
- HttpError,
219
- // client
220
- BadRequest,
221
- Unauthorized,
222
- Forbidden,
223
- NotFound,
224
- MethodNotAllowed,
225
- RequestTimeout,
226
- Conflict,
227
- Gone,
228
- LengthRequired,
229
- ImATeapot,
230
- UnprocessableContent,
231
- TooManyRequests,
232
- // server
233
- InternalServerError,
234
- NotImplemented,
235
- BadGateway,
236
- ServiceUnavailable,
237
- };
238
- const _wellKnownCtorMap = {
239
- '400': BadRequest,
240
- '401': Unauthorized,
241
- '403': Forbidden,
242
- '404': NotFound,
243
- '405': MethodNotAllowed,
244
- '408': RequestTimeout,
245
- '409': Conflict,
246
- '410': Gone,
247
- '411': LengthRequired,
248
- '418': ImATeapot,
249
- '422': UnprocessableContent,
250
- '429': TooManyRequests,
251
- //
252
- '500': InternalServerError,
253
- '501': NotImplemented,
254
- '502': BadGateway,
255
- '503': ServiceUnavailable,
256
- };
257
- const _maybeJsonParse = (v) => {
258
- if (typeof v === 'string') {
259
- try {
260
- v = JSON.parse(v);
261
- }
262
- catch (e) { }
263
- }
264
- return v;
265
- };
266
- const createHttpError = (code, message,
267
- // arbitrary content, typically http response body which threw this error
268
- // (will be JSON.parse-d if the content is a valid json string)
269
- body,
270
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
271
- // arbitrary details, typically response text (will be JSON.parse-d if the content is a valid json string)
272
- cause) => {
273
- const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
274
- code = Number(code);
275
- if (isNaN(code) || !(code >= 400 && code < 600))
276
- code = fallback.CODE;
277
- // opinionated conventions
278
- body = _maybeJsonParse(body);
279
- cause = _maybeJsonParse(cause);
280
- // try to find the well known one, otherwise fallback to generic
281
- const ctor = _wellKnownCtorMap[`${code}`] ?? HttpError;
282
- //
283
- const found = HTTP_STATUS.findByCode(code);
284
- const statusText = found?.TEXT ?? fallback.TEXT;
285
- //
286
- let e = new ctor(message || statusText, { cause });
287
- e.status = found?.CODE ?? fallback.CODE;
288
- e.statusText = statusText;
289
- e.body = body;
290
- return e;
291
- };
292
- const getErrorMessage = (e, stripErrorPrefix = true) => {
293
- if (!e)
294
- return '';
295
- // PROBLEM is that error may bubble from various sources which are not always under control
296
- // and even if they were it still may not be trivial to keep similar structure on each error boundary...
297
- // So, we'll just do what we can, it will not be perfect, but should handle most cases most of the time.
298
- // Also, I'm relying on some of my own opinionated conventions as well...
299
- const cause = _maybeJsonParse(e?.cause);
300
- const body = _maybeJsonParse(e?.body);
301
- let msg =
302
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
303
- // e.cause is the standard prop for error details, so should be considered as
304
- // the most authoritative (if available)
305
- // "code" and "message" are my own conventions
306
- cause?.message ||
307
- cause?.code ||
308
- (typeof cause === 'string' ? cause : null) ||
309
- // non-standard "body" is this package's HttpError prop
310
- body?.error?.message ||
311
- body?.message ||
312
- body?.error ||
313
- (typeof body === 'string' ? body : null) ||
314
- // the common message from Error ctor (e.g. "Foo" if new TypeError("Foo"))
315
- e?.message ||
316
- // the Error class name (e.g. TypeError)
317
- e?.name ||
318
- // this should handle (almost) everything else (mainly if e is not an Error instance)
319
- e?.toString() ||
320
- // very last fallback if `toString()` was not available (or returned empty)
321
- 'Unknown Error';
322
- // ensure we're sending string
323
- msg = `${msg}`;
324
- if (stripErrorPrefix) {
325
- msg = msg.replace(/^[^:]*Error: /i, '');
326
- }
327
- return msg;
328
- };
329
-
330
- const _fetchRaw = async ({ method, path, data = null, token = null, headers = null, signal = null, credentials, }) => {
331
- headers = Object.entries(headers || {}).reduce((m, [k, v]) => ({ ...m, [k.toLowerCase()]: v }), {});
332
- const opts = { method, credentials, headers, signal };
333
- if (data) {
334
- const isObj = typeof data === 'object';
335
- // multipart/form-data -- no explicit Content-Type
336
- if (data instanceof FormData) {
337
- opts.body = data;
338
- }
339
- // cover 99% use cases (may not fit all)
340
- else {
341
- // if not stated, assuming json
342
- if (isObj || !headers['content-type']) {
343
- opts.headers['content-type'] = 'application/json';
344
- }
345
- opts.body = JSON.stringify(data);
346
- }
347
- }
348
- // opinionated convention
349
- if (token) {
350
- opts.headers['authorization'] = `Bearer ${token}`;
351
- }
352
- return await fetch(path, opts);
353
- };
354
- const _fetch = async (params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) => {
355
- if (_dumpParams)
356
- return params;
357
- const r = await _fetchRaw(params);
358
- if (params.raw)
359
- return r;
360
- //
361
- const headers = [...r.headers.entries()].reduce((m, [k, v]) => ({ ...m, [k]: v }), {});
362
- // quick-n-dirty reference to headers (so it's still accessible over this api wrap)
363
- if (respHeaders) {
364
- Object.assign(respHeaders, { ...headers },
365
- // adding status/text under special keys
366
- { __http_status_code__: r.status, __http_status_text__: r.statusText });
367
- }
368
- let body = await r.text();
369
- // prettier-ignore
370
- try {
371
- body = JSON.parse(body);
372
- }
373
- catch (e) { }
374
- params.assert ??= true; // default is true
375
- if (!r.ok && params.assert) {
376
- // now we need to extract error message from an unknown response... this is obviously
377
- // impossible unless we know what to expect, but we'll do some educated tries...
378
- const extractor = errorMessageExtractor ?? // provided arg
379
- createHttpApi.defaultErrorMessageExtractor ?? // static default
380
- // educated guess fallback
381
- function (_body, _response) {
382
- let msg =
383
- // try opinionated convention first
384
- _body?.error?.message ||
385
- _body?.message ||
386
- _body?.error ||
387
- _response?.statusText ||
388
- 'Unknown error';
389
- if (msg.length > 255)
390
- msg = `[Shortened]: ${msg.slice(0, 255)}`;
391
- return msg;
392
- };
393
- // adding `cause` describing more details
394
- throw createHttpError(r.status, extractor(body, r), body, {
395
- method: params.method,
396
- path: params.path,
397
- response: {
398
- status: r.status,
399
- statusText: r.statusText,
400
- headers,
401
- },
402
- });
403
- }
404
- return body;
405
- };
406
- function createHttpApi(base, defaults, factoryErrorMessageExtractor) {
407
- const _merge = (a, b) => {
408
- const wrap = { result: a };
409
- dset(wrap, 'result', b);
410
- return wrap.result;
411
- };
412
- const _getDefs = async () => new Promise(async (resolve) => {
413
- if (typeof defaults === 'function') {
414
- resolve({ ...(await defaults()) });
415
- }
416
- else {
417
- resolve({ ...(defaults || {}) });
418
- }
419
- });
420
- const _buildPath = (path, base) => {
421
- base = `${base || ''}`;
422
- path = `${path || ''}`;
423
- return /^https?:/.test(path) ? path : base + path;
424
- };
425
- return {
426
- // GET
427
- async get(path, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
428
- path = _buildPath(path, base);
429
- return _fetch(_merge(await _getDefs(), { ...params, method: 'GET', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
430
- },
431
- // POST
432
- async post(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
433
- path = _buildPath(path, base);
434
- return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
435
- },
436
- // PUT
437
- async put(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
438
- path = _buildPath(path, base);
439
- return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
440
- },
441
- // PATCH
442
- async patch(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
443
- path = _buildPath(path, base);
444
- return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
445
- },
446
- // DELETE
447
- // https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
448
- async del(path, data = null, params, respHeaders = null, errorMessageExtractor = null, _dumpParams = false) {
449
- path = _buildPath(path, base);
450
- return _fetch(_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }), respHeaders, errorMessageExtractor ?? factoryErrorMessageExtractor, _dumpParams);
451
- },
452
- // helper method to return api's resolved url
453
- // note: cannot use URL(...) as relative would be invalid
454
- url: (path) => _buildPath(path, base),
455
- //
456
- get base() {
457
- return base;
458
- },
459
- set base(v) {
460
- base = v;
461
- },
462
- };
463
- }
464
- createHttpApi.defaultErrorMessageExtractor = null;
465
-
466
- export { HTTP_ERROR, HTTP_STATUS, createHttpApi, createHttpError, getErrorMessage };