@optionfactory/fml 8.0.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.
Files changed (47) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +22 -0
  3. package/dist/client-errors.iife.js +109 -0
  4. package/dist/client-errors.iife.js.map +1 -0
  5. package/dist/client-errors.iife.min.js +2 -0
  6. package/dist/client-errors.iife.min.js.map +1 -0
  7. package/dist/fml.css +2 -0
  8. package/dist/fml.css.map +1 -0
  9. package/dist/fml.d.mts +1241 -0
  10. package/dist/fml.iife.js +8340 -0
  11. package/dist/fml.iife.js.map +1 -0
  12. package/dist/fml.iife.min.js +2 -0
  13. package/dist/fml.iife.min.js.map +1 -0
  14. package/dist/fml.min.mjs +2 -0
  15. package/dist/fml.min.mjs.map +1 -0
  16. package/dist/fml.mjs +8284 -0
  17. package/dist/fml.mjs.map +1 -0
  18. package/dist/ftl.d.mts +361 -0
  19. package/dist/ftl.iife.js +4719 -0
  20. package/dist/ftl.iife.js.map +1 -0
  21. package/dist/ftl.iife.min.js +2 -0
  22. package/dist/ftl.iife.min.js.map +1 -0
  23. package/dist/ftl.min.mjs +2 -0
  24. package/dist/ftl.min.mjs.map +1 -0
  25. package/dist/ftl.mjs +4700 -0
  26. package/dist/ftl.mjs.map +1 -0
  27. package/dist/ful.css +2 -0
  28. package/dist/ful.css.map +1 -0
  29. package/dist/ful.d.mts +581 -0
  30. package/dist/ful.iife.js +2814 -0
  31. package/dist/ful.iife.js.map +1 -0
  32. package/dist/ful.iife.min.js +2 -0
  33. package/dist/ful.iife.min.js.map +1 -0
  34. package/dist/ful.min.mjs +2 -0
  35. package/dist/ful.min.mjs.map +1 -0
  36. package/dist/ful.mjs +2780 -0
  37. package/dist/ful.mjs.map +1 -0
  38. package/dist/httpc.d.mts +306 -0
  39. package/dist/httpc.iife.js +755 -0
  40. package/dist/httpc.iife.js.map +1 -0
  41. package/dist/httpc.iife.min.js +2 -0
  42. package/dist/httpc.iife.min.js.map +1 -0
  43. package/dist/httpc.min.mjs +2 -0
  44. package/dist/httpc.min.mjs.map +1 -0
  45. package/dist/httpc.mjs +743 -0
  46. package/dist/httpc.mjs.map +1 -0
  47. package/package.json +72 -0
package/dist/httpc.mjs ADDED
@@ -0,0 +1,743 @@
1
+ /**
2
+ * @typedef {{ type: string; context: string?; reason: string; details: any?; }} Problem
3
+ */
4
+ class Failure extends Error {
5
+ /**
6
+ *
7
+ * @param {string} message
8
+ * @param {Problem[]} problems
9
+ * @param {*} cause
10
+ */
11
+ constructor(message, problems, cause) {
12
+ super(message, { cause });
13
+ this.name = 'Failure';
14
+ this.problems = problems;
15
+ }
16
+ dropping(prefix) {
17
+ return new Failure(this.message, Failure.dropProblemsContext(this.problems, prefix), this);
18
+ }
19
+ static dropProblemsContext(problems, prefix) {
20
+ return problems.map(({ type, context, reason, details }) => {
21
+ const nctx = context?.startsWith(prefix) ? context.substring(prefix.length) : context;
22
+ return { type, context: nctx, reason, details };
23
+ });
24
+ }
25
+ }
26
+
27
+ class Base64 {
28
+ static encode(arrayBuffer, dialect) {
29
+ const d = dialect || Base64.URL_SAFE;
30
+ const len = arrayBuffer.byteLength;
31
+ const view = new Uint8Array(arrayBuffer);
32
+ let res = '';
33
+ for (let i = 0; i < len; i += 3) {
34
+ const v1 = d[view[i] >> 2];
35
+ const v2 = d[((view[i] & 3) << 4) | (view[i + 1] >> 4)];
36
+ const v3 = d[((view[i + 1] & 15) << 2) | (view[i + 2] >> 6)];
37
+ const v4 = d[view[i + 2] & 63];
38
+ res += v1 + v2 + v3 + v4;
39
+ }
40
+ if (len % 3 === 2) {
41
+ res = res.substring(0, res.length - 1);
42
+ } else if (len % 3 === 1) {
43
+ res = res.substring(0, res.length - 2);
44
+ }
45
+ return res;
46
+ }
47
+ static decode(str, dialect) {
48
+ const d = dialect || Base64.URL_SAFE;
49
+ let nbytes = Math.floor(str.length * 0.75);
50
+ for (let i = 0; i !== str.length; ++i) {
51
+ if (str[str.length - i - 1] !== '=') {
52
+ break;
53
+ }
54
+ --nbytes;
55
+ }
56
+ const view = new Uint8Array(nbytes);
57
+
58
+ let vi = 0;
59
+ let si = 0;
60
+ while (vi < str.length * 0.75) {
61
+ const v1 = d.indexOf(str.charAt(si++));
62
+ const v2 = d.indexOf(str.charAt(si++));
63
+ const v3 = d.indexOf(str.charAt(si++));
64
+ const v4 = d.indexOf(str.charAt(si++));
65
+ view[vi++] = (v1 << 2) | (v2 >> 4);
66
+ view[vi++] = ((v2 & 15) << 4) | (v3 >> 2);
67
+ view[vi++] = ((v3 & 3) << 6) | v4;
68
+ }
69
+
70
+ return view.buffer;
71
+ }
72
+ }
73
+
74
+ Base64.STANDARD = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
75
+ Base64.URL_SAFE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
76
+
77
+ class Hex {
78
+ static decode(hex) {
79
+ if (hex.length % 2 !== 0) {
80
+ throw new Error('invalid length');
81
+ }
82
+ const lenInBytes = hex.length / 2;
83
+ return new Uint8Array(lenInBytes).map((e, i) => {
84
+ const offset = i * 2;
85
+ const octet = hex.substring(offset, offset + 2);
86
+ return parseInt(octet, 16);
87
+ });
88
+ }
89
+ static encode(bytes, upper) {
90
+ return Array.from(bytes)
91
+ .map((b) => b.toString(16))
92
+ .map((b) => (upper ? b.toUpperCase() : b))
93
+ .map((o) => o.padStart(2, 0))
94
+ .join('');
95
+ }
96
+ }
97
+
98
+ class MediaType {
99
+ #type;
100
+ #subtype;
101
+ constructor(type, subtype) {
102
+ this.#type = type;
103
+ this.#subtype = subtype;
104
+ }
105
+ get normalized() {
106
+ return `${this.#type}/${this.#subtype}`;
107
+ }
108
+ get type() {
109
+ return this.#type;
110
+ }
111
+ get subtype() {
112
+ return this.#subtype;
113
+ }
114
+ /**
115
+ *
116
+ * @param {string|null|undefined} v
117
+ * @returns
118
+ */
119
+ static parse(v) {
120
+ if (!v) {
121
+ return new MediaType('unknown', 'unknown');
122
+ }
123
+ const [prefix, _] = v.split(';');
124
+ const [ptype, psubtype] = prefix.trim().split('/');
125
+ return new MediaType(ptype.toLowerCase(), psubtype?.toLowerCase());
126
+ }
127
+ }
128
+
129
+ /**
130
+ * @typedef {Int8Array| Uint8Array| Uint8ClampedArray| Int16Array| Uint16Array| Int32Array| Uint32Array| Float32Array| Float64Array| BigInt64Array| BigUint64Array} TypedArray
131
+ */
132
+ /**
133
+ * @typedef {object} HttpInterceptor
134
+ * @property {(url: URL, init: RequestInit|undefined, chain: HttpInterceptorChain) => Promise<Response>} intercept
135
+ */
136
+
137
+ class HttpClientError extends Failure {
138
+ /**
139
+ * @param {string} message
140
+ * @param {number} status
141
+ * @param {{ type: string; context: string?; reason: string; details: any?; }[]} problems
142
+ * @param {Error|undefined} [cause]
143
+ */
144
+ constructor(message, status, problems, cause) {
145
+ super(message, problems, cause);
146
+ this.name = 'HttpClientError';
147
+ this.status = status;
148
+ }
149
+ dropping(prefix) {
150
+ return new HttpClientError(this.message, this.status, Failure.dropProblemsContext(this.problems, prefix), this);
151
+ }
152
+ /**
153
+ *
154
+ * @param {string} type
155
+ * @param {any} cause
156
+ * @returns
157
+ */
158
+ static of(type, cause) {
159
+ return new HttpClientError(
160
+ cause.message,
161
+ 0,
162
+ [
163
+ {
164
+ type,
165
+ context: null,
166
+ reason: cause.message,
167
+ details: null,
168
+ },
169
+ ],
170
+ cause,
171
+ );
172
+ }
173
+ /**
174
+ * Creates an HttpClientError from a Response.
175
+ * @param {Response} response
176
+ * @returns an HttpClientError
177
+ */
178
+ static async fromResponse(response) {
179
+ switch (MediaType.parse(response.headers.get('Content-Type')).normalized) {
180
+ case 'application/failures+json': {
181
+ const data = await response.json();
182
+ const message = `${response.status} ${response.statusText}: ${data.length} failures`;
183
+ return new HttpClientError(message, response.status, data);
184
+ }
185
+ case 'application/problem+json': {
186
+ const data = await response.json();
187
+ const message = `${response.status} ${response.statusText}: ${data.title} ${data.detail}`;
188
+ return new HttpClientError(
189
+ message,
190
+ response.status,
191
+ data.problems || [
192
+ {
193
+ type: 'GENERIC_PROBLEM',
194
+ context: null,
195
+ reason: message,
196
+ details: null,
197
+ },
198
+ ],
199
+ );
200
+ }
201
+ default: {
202
+ const text = await response.text();
203
+ const message = `${response.status} ${response.statusText}: ${text}`;
204
+ return new HttpClientError(message, response.status, [
205
+ {
206
+ type: 'GENERIC_PROBLEM',
207
+ context: null,
208
+ reason: message,
209
+ details: null,
210
+ },
211
+ ]);
212
+ }
213
+ }
214
+ }
215
+ }
216
+
217
+ /**
218
+ * @implements {HttpInterceptor}
219
+ */
220
+ class CsrfTokenInterceptor {
221
+ #k;
222
+ #v;
223
+ constructor() {
224
+ this.#k = document.querySelector("meta[name='_csrf_header']")?.getAttribute('content');
225
+ this.#v = document.querySelector("meta[name='_csrf']")?.getAttribute('content');
226
+ }
227
+ async intercept(url, request, chain) {
228
+ if (this.#k && this.#v) {
229
+ request.headers.set(this.#k, this.#v);
230
+ }
231
+ return await chain.proceed(url, request);
232
+ }
233
+ }
234
+ /**
235
+ * @implements {HttpInterceptor}
236
+ */
237
+ class RedirectOnUnauthorizedInterceptor {
238
+ #redirectUri;
239
+ /**
240
+ * @param {string} redirectUri
241
+ */
242
+ constructor(redirectUri) {
243
+ this.#redirectUri = redirectUri;
244
+ }
245
+ async intercept(url, request, chain) {
246
+ const response = await chain.proceed(url, request);
247
+ if (response.status === 401) {
248
+ window.location.href = this.#redirectUri;
249
+ return new Promise(() => {});
250
+ }
251
+ return response;
252
+ }
253
+ }
254
+
255
+ class HttpClientBuilder {
256
+ /**
257
+ * @type {HttpInterceptor[]}
258
+ */
259
+ #interceptors;
260
+ constructor() {
261
+ this.#interceptors = [];
262
+ }
263
+ withCsrfToken() {
264
+ this.#interceptors.push(new CsrfTokenInterceptor());
265
+ return this;
266
+ }
267
+ withRedirectOnUnauthorized(redirectUri) {
268
+ this.#interceptors.push(new RedirectOnUnauthorizedInterceptor(redirectUri));
269
+ return this;
270
+ }
271
+ /**
272
+ * @param {...HttpInterceptor} interceptors
273
+ */
274
+ withInterceptors(...interceptors) {
275
+ this.#interceptors.push(...interceptors);
276
+ return this;
277
+ }
278
+ build() {
279
+ return new HttpClient(this.#interceptors);
280
+ }
281
+ }
282
+
283
+ /**
284
+ * @implements {HttpInterceptor}
285
+ */
286
+ class HttpCall {
287
+ async intercept(url, request, chain) {
288
+ return await fetch(url, request);
289
+ }
290
+ }
291
+
292
+ class HttpInterceptorChain {
293
+ #interceptors;
294
+ #current;
295
+ /**
296
+ *
297
+ * @param {HttpInterceptor[]} interceptors
298
+ * @param {number} current
299
+ */
300
+ constructor(interceptors, current) {
301
+ this.#interceptors = interceptors;
302
+ this.#current = current;
303
+ }
304
+ /**
305
+ *
306
+ * @param {URL} url
307
+ * @param {RequestInit} request
308
+ * @returns {Promise<Response>} the response
309
+ */
310
+ async proceed(url, request) {
311
+ const interceptor = this.#interceptors[this.#current];
312
+ return await interceptor.intercept(
313
+ url,
314
+ request,
315
+ new HttpInterceptorChain(this.#interceptors, this.#current + 1),
316
+ );
317
+ }
318
+ }
319
+
320
+ class HttpClient {
321
+ #interceptors;
322
+ /**
323
+ * Creates a builder for an HttpClient.
324
+ * @returns {HttpClientBuilder} the client builder
325
+ */
326
+ static builder() {
327
+ return new HttpClientBuilder();
328
+ }
329
+ /**
330
+ * Creates an HttpClient.
331
+ * @param {HttpInterceptor[]|undefined} interceptors - a list of interceptors to be registered for every request performed by the created client.
332
+ */
333
+ constructor(interceptors) {
334
+ this.#interceptors = interceptors || [];
335
+ }
336
+ /**
337
+ * Performs an HTTP exchange.
338
+ * @async
339
+ * @param {string} uri - the (possibly relative) request url
340
+ * @param {RequestInit|undefined} options - fetch options
341
+ * @param {HttpInterceptor[]|undefined} interceptors - the HttpInterceptors to be registered for this exchange.
342
+ * @returns {Promise<Response>} the response
343
+ */
344
+ async exchange(uri, options, interceptors) {
345
+ const is = [...this.#interceptors, ...(interceptors || []), new HttpCall()];
346
+ const chain = new HttpInterceptorChain(is, 0);
347
+ const url = new URL(new Request(uri).url);
348
+ return await chain.proceed(url, options ?? {});
349
+ }
350
+ /**
351
+ * Creates a request builder.
352
+ * @param {string} method - the HTTP method to be used
353
+ * @param {string} uri - the (possibly relative) request url
354
+ * @returns {HttpRequestBuilder} the request builder
355
+ */
356
+ request(method, uri) {
357
+ return HttpRequestBuilder.create(this, method, uri);
358
+ }
359
+ /**
360
+ * Creates a request builder.
361
+ * @param {string} uri - the (possibly relative) request url
362
+ * @returns {HttpRequestBuilder} the request builder
363
+ */
364
+ get(uri) {
365
+ return HttpRequestBuilder.create(this, 'GET', uri);
366
+ }
367
+ /**
368
+ * Creates a request builder.
369
+ * @param {string} uri - the (possibly relative) request url
370
+ * @returns {HttpRequestBuilder} the request builder
371
+ */
372
+ head(uri) {
373
+ return HttpRequestBuilder.create(this, 'HEAD', uri);
374
+ }
375
+ /**
376
+ * Creates a request builder.
377
+ * @param {string} uri - the (possibly relative) request url
378
+ * @returns {HttpRequestBuilder} the request builder
379
+ */
380
+ post(uri) {
381
+ return HttpRequestBuilder.create(this, 'POST', uri);
382
+ }
383
+ /**
384
+ * Creates a request builder.
385
+ * @param {string} uri - the (possibly relative) request url
386
+ * @returns {HttpRequestBuilder} the request builder
387
+ */
388
+ put(uri) {
389
+ return HttpRequestBuilder.create(this, 'PUT', uri);
390
+ }
391
+ /**
392
+ * Creates a request builder.
393
+ * @param {string} uri - the (possibly relative) request url
394
+ * @returns {HttpRequestBuilder} the request builder
395
+ */
396
+ patch(uri) {
397
+ return HttpRequestBuilder.create(this, 'PATCH', uri);
398
+ }
399
+ /**
400
+ * Creates a request builder.
401
+ * @param {string} uri - the (possibly relative) request url
402
+ * @returns {HttpRequestBuilder} the request builder
403
+ */
404
+ delete(uri) {
405
+ return HttpRequestBuilder.create(this, 'DELETE', uri);
406
+ }
407
+ }
408
+
409
+ /**
410
+ *
411
+ * @param {Response} response
412
+ * @param {'text'|'json'|'blob'|'arrayBuffer'} type
413
+ * @returns
414
+ */
415
+ const unmarshal = async (response, type) => {
416
+ try {
417
+ return await response[type]();
418
+ } catch (ex) {
419
+ throw HttpClientError.of('UNMARSHALING_PROBLEM', ex);
420
+ }
421
+ };
422
+
423
+ class HttpRequestBuilder {
424
+ #client;
425
+ #method;
426
+ #uri;
427
+ #params;
428
+ #headers;
429
+ #body;
430
+ #options;
431
+ #interceptors;
432
+ /**
433
+ * Creates an HttpRequestBuilder.
434
+ * @param {HttpClient} client
435
+ * @param {string} method - the HTTP method to be used
436
+ * @param {string} uri - the (possibly relative) request url
437
+ * @returns {HttpRequestBuilder} the builder
438
+ */
439
+ static create(client, method, uri) {
440
+ const [baseUri, queryString = ''] = uri.split('?');
441
+ return new HttpRequestBuilder(
442
+ client,
443
+ method,
444
+ baseUri,
445
+ new URLSearchParams(queryString),
446
+ new Headers(),
447
+ undefined,
448
+ {},
449
+ [],
450
+ );
451
+ }
452
+ /**
453
+ * Creates an HttpRequestBuilder.
454
+ * @param {HttpClient} client
455
+ * @param {string} method - the HTTP method to be used
456
+ * @param {string} uri - the (possibly relative) request url
457
+ * @param {URLSearchParams} params
458
+ * @param {Headers} headers
459
+ * @param {any} body
460
+ * @param {Omit<RequestInit,"headers"|"method"|"body">} options
461
+ * @param {HttpInterceptor[]} interceptors
462
+ */
463
+ constructor(client, method, uri, params, headers, body, options, interceptors) {
464
+ this.#client = client;
465
+ this.#method = method;
466
+ this.#uri = uri;
467
+ this.#params = params;
468
+ this.#body = body;
469
+ this.#headers = headers;
470
+ this.#options = options;
471
+ this.#interceptors = interceptors;
472
+ }
473
+ /**
474
+ * Add all passed headers to the request, overriding existing ones if that key already exists. Null and undefined values cause the key to be removed.
475
+ * @param {HeadersInit} hs
476
+ * @returns {HttpRequestBuilder} this builder
477
+ */
478
+ headers(hs) {
479
+ for (const [k, v] of new Headers(hs).entries()) {
480
+ if (v == null) {
481
+ this.#headers.delete(k);
482
+ } else {
483
+ this.#headers.set(k, v);
484
+ }
485
+ }
486
+ return this;
487
+ }
488
+ /**
489
+ * Adds an header to the request, overriding it if it already exists. Null and undefined values cause the key to be removed
490
+ * @param {string} k
491
+ * @param {string} v
492
+ * @returns {HttpRequestBuilder} this builder
493
+ */
494
+ header(k, v) {
495
+ if (v == null) {
496
+ this.#headers.delete(k);
497
+ } else {
498
+ this.#headers.set(k, v);
499
+ }
500
+ return this;
501
+ }
502
+ /**
503
+ * Add all query parameters to the request, overriding existing ones if that key already exists. Null and undefined values cause the key to be removed
504
+ * @param {URLSearchParams|Record<string,string>|string[][]|string} ps
505
+ * @returns {HttpRequestBuilder} this builder
506
+ */
507
+ params(ps) {
508
+ for (const [k, v] of new URLSearchParams(ps).entries()) {
509
+ if (v == null) {
510
+ this.#params.delete(k);
511
+ } else {
512
+ this.#params.set(k, v);
513
+ }
514
+ }
515
+ return this;
516
+ }
517
+ /**
518
+ * Adds a query parameter to the request, overriding it if it already exists. Empty vs, or a single null or undefined value cause the key to be removed.
519
+ * @param {string} k
520
+ * @param {...string} vs
521
+ * @returns {HttpRequestBuilder} this builder
522
+ */
523
+ param(k, ...vs) {
524
+ if (vs.length === 0 || vs[0] == null) {
525
+ this.#params.delete(k);
526
+ return this;
527
+ }
528
+ for (const v of vs) {
529
+ this.#params.append(k, v);
530
+ }
531
+ return this;
532
+ }
533
+ /**
534
+ * Sets the request body.
535
+ * `Content-Type: multipart/form-data` header is automatically added by fetch when data is a FormData instance if not explicitly set.
536
+ * `Content-Type: application/x-www-form-urlencoded` header is automatically added by fetch when data is an URLSearchParams instance if not explicitly set.
537
+ * `Content-Type: text/plain` header is automatically added by fetch when data is a string instance if not explicitly set.
538
+ * @param {string|ArrayBuffer|Blob|DataView|File|FormData|TypedArray|URLSearchParams|ReadableStream} data
539
+ * @returns {HttpRequestBuilder} this builder
540
+ */
541
+ body(data) {
542
+ this.#body = data;
543
+ return this;
544
+ }
545
+ /**
546
+ * Sets the request body that will be serialized as json. Calling this method adds the `Content-Type application/json` header for the request.
547
+ * @param {any} body - the body to be serialized as json
548
+ * @returns {HttpRequestBuilder} this builder
549
+ */
550
+ json(body) {
551
+ this.#headers.set('Content-Type', 'application/json');
552
+ this.#body = JSON.stringify(body);
553
+ return this;
554
+ }
555
+ /**
556
+ * Sets the request body as a FormData configured using the callback.
557
+ * `Content-Type: multipart/form-data` header is automatically added by fetch if not explicitly set.
558
+ * @param {function(HttpMultipartRequestCustomizer):void} callback
559
+ */
560
+ multipart(callback) {
561
+ const formData = new FormData();
562
+ const builder = new HttpMultipartRequestCustomizer(formData);
563
+ callback(builder);
564
+ this.#body = formData;
565
+ return this;
566
+ }
567
+ /**
568
+ * Sets a fetch options for the request.
569
+ * @param {Omit<RequestInit,"headers"|"method"|"body">} kvs
570
+ * @returns {HttpRequestBuilder} this builder
571
+ */
572
+ options(kvs) {
573
+ for (const [k, v] of Object.entries(kvs)) {
574
+ this.#options[k] = v;
575
+ }
576
+ return this;
577
+ }
578
+ /**
579
+ * Sets a fetch option for the request.
580
+ * @param {keyof Omit<RequestInit,"headers"|"method"|"body">} k
581
+ * @param {*} v
582
+ * @returns {HttpRequestBuilder} this builder
583
+ */
584
+ option(k, v) {
585
+ this.#options[k] = v;
586
+ return this;
587
+ }
588
+ /**
589
+ * Adds interceptors to the request.
590
+ * @param {[HttpInterceptor]} is - the interceptor to be regisered
591
+ * @returns {HttpRequestBuilder} this builder
592
+ */
593
+ interceptors(is) {
594
+ for (const i of is) {
595
+ this.#interceptors.push(i);
596
+ }
597
+ return this;
598
+ }
599
+ /**
600
+ * Adds an interceptor to the request.
601
+ * @param {HttpInterceptor} i - the interceptor to be regisered
602
+ * @returns {HttpRequestBuilder} this builder
603
+ */
604
+ interceptor(i) {
605
+ this.#interceptors.push(i);
606
+ return this;
607
+ }
608
+ /**
609
+ * Performs an HTTP exchange using the configured client, request and interceptors.
610
+ * @returns {Promise<Response>} the response
611
+ */
612
+ async exchange() {
613
+ const uri = this.#params.size ? `${this.#uri}?${this.#params}` : this.#uri;
614
+ const opts = {
615
+ ...this.#options,
616
+ headers: this.#headers,
617
+ method: this.#method,
618
+ body: this.#body,
619
+ };
620
+ return await this.#client.exchange(uri, opts, this.#interceptors);
621
+ }
622
+ /**
623
+ * Performs an HTTP exchange using the configured client request, and interceptos throwing a failure when response status is not in the 200-299 range.
624
+ * @returns {Promise<Response>} the response
625
+ */
626
+ async fetch() {
627
+ const uri = this.#params.size ? `${this.#uri}?${this.#params}` : this.#uri;
628
+ const opts = {
629
+ ...this.#options,
630
+ headers: this.#headers,
631
+ method: this.#method,
632
+ body: this.#body,
633
+ };
634
+ try {
635
+ const response = await this.#client.exchange(uri, opts, this.#interceptors);
636
+ if (!response.ok) {
637
+ throw await HttpClientError.fromResponse(response);
638
+ }
639
+ return response;
640
+ } catch (ex) {
641
+ if (ex instanceof Failure) {
642
+ throw ex;
643
+ }
644
+ throw HttpClientError.of('CONNECTION_PROBLEM', ex);
645
+ }
646
+ }
647
+ /**
648
+ * Performs an HTTP exchange using the configured client request, and interceptos throwing a failure when response status is not in the 200-299 range.
649
+ * @returns {Promise<string>} the response body, as text
650
+ */
651
+ async fetchText() {
652
+ const response = await this.fetch();
653
+ return await unmarshal(response, 'text');
654
+ }
655
+ /**
656
+ * Performs an HTTP exchange using the configured client request, and interceptos throwing a failure when response status is not in the 200-299 range.
657
+ * @returns {Promise<any>} the response body, deserialized as JSON
658
+ */
659
+ async fetchJson() {
660
+ const response = await this.fetch();
661
+ return await unmarshal(response, 'json');
662
+ }
663
+ /**
664
+ * Performs an HTTP exchange using the configured client request, and interceptos throwing a failure when response status is not in the 200-299 range.
665
+ * @returns {Promise<Blob>} the response body, as a Blob
666
+ */
667
+ async fetchBlob() {
668
+ const response = await this.fetch();
669
+ return await unmarshal(response, 'blob');
670
+ }
671
+ /**
672
+ * Performs an HTTP exchange using the configured client request, and interceptos throwing a failure when response status is not in the 200-299 range.
673
+ * @returns {Promise<ArrayBuffer>} the response body, as an ArrayBuffer
674
+ */
675
+ async fetchArrayBuffer() {
676
+ const response = await this.fetch();
677
+ return await unmarshal(response, 'arrayBuffer');
678
+ }
679
+ }
680
+
681
+ class HttpMultipartRequestCustomizer {
682
+ #formData;
683
+ /**
684
+ *
685
+ * @param {FormData} formData
686
+ */
687
+ constructor(formData) {
688
+ this.#formData = formData;
689
+ }
690
+ /**
691
+ * Appends a value to the FormData.
692
+ * @param {string} name
693
+ * @param {*} value
694
+ * @returns this builder
695
+ */
696
+ field(name, value) {
697
+ this.#formData.append(name, value);
698
+ return this;
699
+ }
700
+ /**
701
+ * Appends a Blob to the FormData.
702
+ * If `filename` is omitted, FormData defaults are applied:
703
+ * The default filename for Blob objects is "blob";
704
+ * The default filename for File objects is the file's filename.
705
+ * @param {string} name
706
+ * @param {Blob} value
707
+ * @param {string|undefined} filename
708
+ * @returns this builder
709
+ */
710
+ blob(name, value, filename) {
711
+ this.#formData.append(name, value, filename);
712
+ return this;
713
+ }
714
+ /**
715
+ * Appends multiple Blobs to the FormData with the same name.
716
+ * The default filename for Blob objects is "blob";
717
+ * The default filename for File objects is the file's filename.
718
+ * @param {string} name
719
+ * @param {Blob[]} values
720
+ * @returns this builder
721
+ */
722
+ blobs(name, values) {
723
+ for (let v of values) {
724
+ this.#formData.append(name, v);
725
+ }
726
+ return this;
727
+ }
728
+ /**
729
+ * Appends a JSON serialized blob to the FormData.
730
+ * @param {string} name
731
+ * @param {any} value
732
+ * @param {string|undefined} filename
733
+ * @returns this builder
734
+ */
735
+ json(name, value, filename) {
736
+ const blob = new Blob([JSON.stringify(value)], { type: 'application/json' });
737
+ this.#formData.append(name, blob, filename);
738
+ return this;
739
+ }
740
+ }
741
+
742
+ export { Base64, Failure, Hex, HttpClient, HttpClientError, MediaType };
743
+ //# sourceMappingURL=httpc.mjs.map