@d1g1tal/transportr 1.1.0 → 1.2.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.
@@ -101,34 +101,33 @@ var Transportr = (() => {
101
101
  };
102
102
 
103
103
  // node_modules/@d1g1tal/media-type/src/utils.js
104
- var removeLeadingAndTrailingHTTPWhitespace = (string) => string.replace(/^[ \t\n\r]+/u, "").replace(/[ \t\n\r]+$/u, "");
105
- var removeTrailingHTTPWhitespace = (string) => string.replace(/[ \t\n\r]+$/u, "");
106
- var isHTTPWhitespaceChar = (char) => char === " " || char === " " || char === "\n" || char === "\r";
107
- var solelyContainsHTTPTokenCodePoints = (string) => /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/u.test(string);
108
- var solelyContainsHTTPQuotedStringTokenCodePoints = (string) => /^[\t\u0020-\u007E\u0080-\u00FF]*$/u.test(string);
109
- var asciiLowercase = (string) => string.replace(/[A-Z]/ug, (l) => l.toLowerCase());
104
+ var whitespaceCharacters = [" ", " ", "\n", "\r"];
105
+ var leadingWhitespace = /^[ \t\n\r]+/u;
106
+ var trailingWhitespace = /[ \t\n\r]+$/u;
107
+ var httpTokenCodePoints = /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/u;
108
+ var httpQuotedTokenCodePoints = /^[\t\u0020-\u007E\u0080-\u00FF]*$/u;
109
+ var removeLeadingAndTrailingHTTPWhitespace = (string) => string.replace(leadingWhitespace, "").replace(trailingWhitespace, "");
110
+ var removeTrailingHTTPWhitespace = (string) => string.replace(trailingWhitespace, "");
111
+ var isHTTPWhitespaceChar = (char) => whitespaceCharacters.includes(char);
112
+ var solelyContainsHTTPTokenCodePoints = (string) => httpTokenCodePoints.test(string);
113
+ var solelyContainsHTTPQuotedStringTokenCodePoints = (string) => httpQuotedTokenCodePoints.test(string);
114
+ var asciiLowercase = (string) => {
115
+ let result = "";
116
+ for (const [char, charCode = char.charCodeAt(0)] of string) {
117
+ result += charCode >= 65 && charCode <= 90 ? String.fromCharCode(charCode + 32) : char;
118
+ }
119
+ return result;
120
+ };
110
121
  var collectAnHTTPQuotedString = (input, position) => {
111
122
  let value = "";
112
- position++;
113
- while (true) {
114
- while (position < input.length && input[position] !== '"' && input[position] !== "\\") {
115
- value += input[position];
116
- ++position;
117
- }
118
- if (position >= input.length) {
123
+ for (let length = input.length, char; ++position < length; ) {
124
+ char = input[position];
125
+ if (char == "\\") {
126
+ value += ++position < length ? input[position] : char;
127
+ } else if (char == '"') {
119
128
  break;
120
- }
121
- const quoteOrBackslash = input[position];
122
- ++position;
123
- if (quoteOrBackslash === "\\") {
124
- if (position >= input.length) {
125
- value += "\\";
126
- break;
127
- }
128
- value += input[position];
129
- ++position;
130
129
  } else {
131
- break;
130
+ value += char;
132
131
  }
133
132
  }
134
133
  return [value, position];
@@ -136,13 +135,15 @@ var Transportr = (() => {
136
135
 
137
136
  // node_modules/@d1g1tal/media-type/src/media-type-parameters.js
138
137
  var MediaTypeParameters = class {
138
+ /** @type {Map<string, string>} */
139
+ #map;
139
140
  /**
140
141
  * Create a new MediaTypeParameters instance.
141
142
  *
142
- * @param {Map.<string, string>} map The map of parameters for a media type.
143
+ * @param {Array<Array<string>>} entries An array of [name, value] tuples.
143
144
  */
144
- constructor(map) {
145
- this._map = map;
145
+ constructor(entries) {
146
+ this.#map = new Map(entries);
146
147
  }
147
148
  /**
148
149
  * Gets the number of media type parameters.
@@ -150,7 +151,7 @@ var Transportr = (() => {
150
151
  * @returns {number} The number of media type parameters
151
152
  */
152
153
  get size() {
153
- return this._map.size;
154
+ return this.#map.size;
154
155
  }
155
156
  /**
156
157
  * Gets the media type parameter value for the supplied name.
@@ -159,7 +160,7 @@ var Transportr = (() => {
159
160
  * @returns {string} The media type parameter value.
160
161
  */
161
162
  get(name) {
162
- return this._map.get(asciiLowercase(String(name)));
163
+ return this.#map.get(asciiLowercase(String(name)));
163
164
  }
164
165
  /**
165
166
  * Indicates whether the media type parameter with the specified name exists or not.
@@ -168,7 +169,7 @@ var Transportr = (() => {
168
169
  * @returns {boolean} true if the media type parameter exists, false otherwise.
169
170
  */
170
171
  has(name) {
171
- return this._map.has(asciiLowercase(String(name)));
172
+ return this.#map.has(asciiLowercase(String(name)));
172
173
  }
173
174
  /**
174
175
  * Adds a new media type parameter using the specified name and value to the MediaTypeParameters.
@@ -187,14 +188,14 @@ var Transportr = (() => {
187
188
  if (!solelyContainsHTTPQuotedStringTokenCodePoints(value)) {
188
189
  throw new Error(`Invalid media type parameter value "${value}": only HTTP quoted-string token code points are valid.`);
189
190
  }
190
- this._map.set(name, value);
191
+ this.#map.set(name, value);
191
192
  return this;
192
193
  }
193
194
  /**
194
195
  * Clears all the media type parameters.
195
196
  */
196
197
  clear() {
197
- this._map.clear();
198
+ this.#map.clear();
198
199
  }
199
200
  /**
200
201
  * Removes the media type parameter using the specified name.
@@ -204,7 +205,7 @@ var Transportr = (() => {
204
205
  */
205
206
  delete(name) {
206
207
  name = asciiLowercase(String(name));
207
- return this._map.delete(name);
208
+ return this.#map.delete(name);
208
209
  }
209
210
  /**
210
211
  * Executes a provided function once per each name/value pair in the MediaTypeParameters, in insertion order.
@@ -213,7 +214,7 @@ var Transportr = (() => {
213
214
  * @param {*} [thisArg] Optional object when binding 'this' to the callback.
214
215
  */
215
216
  forEach(callback, thisArg) {
216
- this._map.forEach(callback, thisArg);
217
+ this.#map.forEach(callback, thisArg);
217
218
  }
218
219
  /**
219
220
  * Returns an iterable of parameter names.
@@ -221,7 +222,7 @@ var Transportr = (() => {
221
222
  * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter names.
222
223
  */
223
224
  keys() {
224
- return this._map.keys();
225
+ return this.#map.keys();
225
226
  }
226
227
  /**
227
228
  * Returns an iterable of parameter values.
@@ -229,7 +230,7 @@ var Transportr = (() => {
229
230
  * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter values.
230
231
  */
231
232
  values() {
232
- return this._map.values();
233
+ return this.#map.values();
233
234
  }
234
235
  /**
235
236
  * Returns an iterable of name, value pairs for every parameter entry in the media type parameters.
@@ -237,7 +238,7 @@ var Transportr = (() => {
237
238
  * @returns {IterableIterator<Array<Array<string>>>} The media type parameter entries.
238
239
  */
239
240
  entries() {
240
- return this._map.entries();
241
+ return this.#map.entries();
241
242
  }
242
243
  /**
243
244
  * A method that returns the default iterator for the {@link MediaTypeParameters}. Called by the semantics of the for-of statement.
@@ -245,7 +246,26 @@ var Transportr = (() => {
245
246
  * @returns {Iterator<string, string, undefined>} The {@link Symbol.iterator} for the media type parameters.
246
247
  */
247
248
  [Symbol.iterator]() {
248
- return this._map[Symbol.iterator]();
249
+ return this.#map[Symbol.iterator]();
250
+ }
251
+ /**
252
+ * Returns a string representation of the media type parameters.
253
+ * This method is called by the `String()` function.
254
+ *
255
+ * @example
256
+ * const parameters = new MediaTypeParameters(new Map([['charset', 'utf-8']]));
257
+ * String(parameters); // 'charset=utf-8'
258
+ * parameters.toString(); // 'charset=utf-8'
259
+ * parameters + ''; // 'charset=utf-8'
260
+ * `${parameters}`; // 'charset=utf-8'
261
+ * parameters[Symbol.toStringTag]; // 'MediaTypeParameters'
262
+ * parameters[Symbol.toStringTag](); // 'MediaTypeParameters'
263
+ * Object.prototype.toString.call(parameters); // '[object MediaTypeParameters]'
264
+ * parameters + ''; // 'charset=utf-8'
265
+ * @returns {string} The string representation of the media type parameters.
266
+ */
267
+ [Symbol.toStringTag]() {
268
+ return "MediaTypeParameters";
249
269
  }
250
270
  };
251
271
 
@@ -254,7 +274,7 @@ var Transportr = (() => {
254
274
  input = removeLeadingAndTrailingHTTPWhitespace(input);
255
275
  let position = 0;
256
276
  let type = "";
257
- while (position < input.length && input[position] !== "/") {
277
+ while (position < input.length && input[position] != "/") {
258
278
  type += input[position];
259
279
  ++position;
260
280
  }
@@ -266,7 +286,7 @@ var Transportr = (() => {
266
286
  }
267
287
  ++position;
268
288
  let subtype = "";
269
- while (position < input.length && input[position] !== ";") {
289
+ while (position < input.length && input[position] != ";") {
270
290
  subtype += input[position];
271
291
  ++position;
272
292
  }
@@ -285,26 +305,26 @@ var Transportr = (() => {
285
305
  ++position;
286
306
  }
287
307
  let parameterName = "";
288
- while (position < input.length && input[position] !== ";" && input[position] !== "=") {
308
+ while (position < input.length && input[position] != ";" && input[position] != "=") {
289
309
  parameterName += input[position];
290
310
  ++position;
291
311
  }
292
312
  parameterName = asciiLowercase(parameterName);
293
313
  if (position < input.length) {
294
- if (input[position] === ";") {
314
+ if (input[position] == ";") {
295
315
  continue;
296
316
  }
297
317
  ++position;
298
318
  }
299
319
  let parameterValue = null;
300
- if (input[position] === '"') {
320
+ if (input[position] == '"') {
301
321
  [parameterValue, position] = collectAnHTTPQuotedString(input, position);
302
- while (position < input.length && input[position] !== ";") {
322
+ while (position < input.length && input[position] != ";") {
303
323
  ++position;
304
324
  }
305
325
  } else {
306
326
  parameterValue = "";
307
- while (position < input.length && input[position] !== ";") {
327
+ while (position < input.length && input[position] != ";") {
308
328
  parameterValue += input[position];
309
329
  ++position;
310
330
  }
@@ -328,12 +348,9 @@ var Transportr = (() => {
328
348
  return serialization;
329
349
  }
330
350
  for (let [name, value] of mediaType.parameters) {
331
- serialization += ";";
332
- serialization += name;
333
- serialization += "=";
351
+ serialization += `;${name}=`;
334
352
  if (!solelyContainsHTTPTokenCodePoints(value) || value.length === 0) {
335
- value = value.replace(/(["\\])/ug, "\\$1");
336
- value = `"${value}"`;
353
+ value = `"${value.replace(/(["\\])/ug, "\\$1")}"`;
337
354
  }
338
355
  serialization += value;
339
356
  }
@@ -343,32 +360,35 @@ var Transportr = (() => {
343
360
 
344
361
  // node_modules/@d1g1tal/media-type/src/media-type.js
345
362
  var MediaType = class {
363
+ /** @type {string} */
364
+ #type;
365
+ /** @type {string} */
366
+ #subtype;
367
+ /** @type {MediaTypeParameters} */
368
+ #parameters;
346
369
  /**
347
370
  * Create a new MediaType instance from a string representation.
348
371
  *
349
- * @param {string} string The media type to parse
372
+ * @param {string} mediaType The media type to parse.
373
+ * @param {Object} [parameters] Optional parameters.
350
374
  */
351
- constructor(string) {
352
- string = String(string);
353
- const result = parser_default(string);
354
- if (result === null) {
355
- throw new Error(`Could not parse media type string '${string}'`);
356
- }
357
- this._type = result.type;
358
- this._subtype = result.subtype;
359
- this._parameters = new MediaTypeParameters(result.parameters);
375
+ constructor(mediaType, parameters = {}) {
376
+ const { type, subtype, parameters: parsedParameters } = parser_default(mediaType);
377
+ this.#type = type;
378
+ this.#subtype = subtype;
379
+ this.#parameters = new MediaTypeParameters([...parsedParameters, ...Object.entries(parameters).map(([name, value]) => [asciiLowercase(name), asciiLowercase(value)])]);
360
380
  }
361
381
  /**
362
- * Static factor method for parsing a media type.
382
+ * Static factory method for parsing a media type.
363
383
  *
364
- * @param {string} string The media type to parse
365
- * @returns {MediaType} The parsed {@link MediaType} object
384
+ * @param {string} string The media type to parse.
385
+ * @returns {MediaType} The parsed {@link MediaType} object or null if the string could not be parsed.
366
386
  */
367
387
  static parse(string) {
368
388
  try {
369
- return new this(string);
389
+ return new MediaType(string);
370
390
  } catch (e) {
371
- return null;
391
+ throw new Error(`Could not parse media type string '${string}'`);
372
392
  }
373
393
  }
374
394
  /**
@@ -377,7 +397,7 @@ var Transportr = (() => {
377
397
  * @returns {string} The media type without any parameters
378
398
  */
379
399
  get essence() {
380
- return `${this.type}/${this.subtype}`;
400
+ return `${this.#type}/${this.#subtype}`;
381
401
  }
382
402
  /**
383
403
  * Gets the type.
@@ -385,7 +405,7 @@ var Transportr = (() => {
385
405
  * @returns {string} The type.
386
406
  */
387
407
  get type() {
388
- return this._type;
408
+ return this.#type;
389
409
  }
390
410
  /**
391
411
  * Sets the type.
@@ -398,7 +418,7 @@ var Transportr = (() => {
398
418
  if (!solelyContainsHTTPTokenCodePoints(value)) {
399
419
  throw new Error(`Invalid type ${value}: must contain only HTTP token code points`);
400
420
  }
401
- this._type = value;
421
+ this.#type = value;
402
422
  }
403
423
  /**
404
424
  * Gets the subtype.
@@ -406,7 +426,7 @@ var Transportr = (() => {
406
426
  * @returns {string} The subtype.
407
427
  */
408
428
  get subtype() {
409
- return this._subtype;
429
+ return this.#subtype;
410
430
  }
411
431
  /**
412
432
  * Sets the subtype.
@@ -419,7 +439,7 @@ var Transportr = (() => {
419
439
  if (!solelyContainsHTTPTokenCodePoints(value)) {
420
440
  throw new Error(`Invalid subtype ${value}: must contain only HTTP token code points`);
421
441
  }
422
- this._subtype = value;
442
+ this.#subtype = value;
423
443
  }
424
444
  /**
425
445
  * Gets the parameters.
@@ -427,7 +447,7 @@ var Transportr = (() => {
427
447
  * @returns {MediaTypeParameters} The media type parameters.
428
448
  */
429
449
  get parameters() {
430
- return this._parameters;
450
+ return this.#parameters;
431
451
  }
432
452
  /**
433
453
  * Gets the serialized version of the media type.
@@ -445,9 +465,9 @@ var Transportr = (() => {
445
465
  * @returns {boolean} true if this instance represents a JavaScript media type, false otherwise.
446
466
  */
447
467
  isJavaScript({ prohibitParameters = false } = {}) {
448
- switch (this._type) {
468
+ switch (this.#type) {
449
469
  case "text": {
450
- switch (this._subtype) {
470
+ switch (this.#subtype) {
451
471
  case "ecmascript":
452
472
  case "javascript":
453
473
  case "javascript1.0":
@@ -460,18 +480,18 @@ var Transportr = (() => {
460
480
  case "livescript":
461
481
  case "x-ecmascript":
462
482
  case "x-javascript":
463
- return !prohibitParameters || this._parameters.size === 0;
483
+ return !prohibitParameters || this.#parameters.size === 0;
464
484
  default:
465
485
  return false;
466
486
  }
467
487
  }
468
488
  case "application": {
469
- switch (this._subtype) {
489
+ switch (this.#subtype) {
470
490
  case "ecmascript":
471
491
  case "javascript":
472
492
  case "x-ecmascript":
473
493
  case "x-javascript":
474
- return !prohibitParameters || this._parameters.size === 0;
494
+ return !prohibitParameters || this.#parameters.size === 0;
475
495
  default:
476
496
  return false;
477
497
  }
@@ -486,7 +506,7 @@ var Transportr = (() => {
486
506
  * @returns {boolean} true if this instance represents an XML media type, false otherwise.
487
507
  */
488
508
  isXML() {
489
- return this._subtype === "xml" && (this._type === "text" || this._type === "application") || this._subtype.endsWith("+xml");
509
+ return this.#subtype === "xml" && (this.#type === "text" || this.#type === "application") || this.#subtype.endsWith("+xml");
490
510
  }
491
511
  /**
492
512
  * Determines if this instance is an HTML media type.
@@ -494,7 +514,7 @@ var Transportr = (() => {
494
514
  * @returns {boolean} true if this instance represents an HTML media type, false otherwise.
495
515
  */
496
516
  isHTML() {
497
- return this._subtype === "html" && this._type === "text";
517
+ return this.#subtype === "html" && this.#type === "text";
498
518
  }
499
519
  /**
500
520
  * Gets the name of the class.
@@ -1861,6 +1881,17 @@ var Transportr = (() => {
1861
1881
 
1862
1882
  // src/transportr.js
1863
1883
  var endsWithSlashRegEx = /\/$/;
1884
+ var charset = "utf-8";
1885
+ var _mediaTypes = /* @__PURE__ */ new Map([
1886
+ [http_media_type_default.PNG, new MediaType(http_media_type_default.PNG)],
1887
+ [http_media_type_default.TEXT, new MediaType(http_media_type_default.TEXT, { charset })],
1888
+ [http_media_type_default.JSON, new MediaType(http_media_type_default.JSON, { charset })],
1889
+ [http_media_type_default.HTML, new MediaType(http_media_type_default.HTML, { charset })],
1890
+ [http_media_type_default.JAVA_SCRIPT, new MediaType(http_media_type_default.JAVA_SCRIPT, { charset })],
1891
+ [http_media_type_default.CSS, new MediaType(http_media_type_default.CSS, { charset })],
1892
+ [http_media_type_default.XML, new MediaType(http_media_type_default.XML, { charset })],
1893
+ [http_media_type_default.BIN, new MediaType(http_media_type_default.BIN)]
1894
+ ]);
1864
1895
  var _handleText = async (response) => await response.text();
1865
1896
  var _handleScript = async (response) => {
1866
1897
  const objectURL = URL.createObjectURL(await response.blob());
@@ -1879,11 +1910,11 @@ var Transportr = (() => {
1879
1910
  var _handleImage = async (response) => URL.createObjectURL(await response.blob());
1880
1911
  var _handleBuffer = async (response) => await response.arrayBuffer();
1881
1912
  var _handleReadableStream = async (response) => response.body;
1882
- var _handleXml = async (response) => new DOMParser().parseFromString(await response.text(), http_media_type_default.XML.essence);
1883
- var _handleHtml = async (response) => new DOMParser().parseFromString(await response.text(), http_media_type_default.HTML.essence);
1913
+ var _handleXml = async (response) => new DOMParser().parseFromString(await response.text(), http_media_type_default.XML);
1914
+ var _handleHtml = async (response) => new DOMParser().parseFromString(await response.text(), http_media_type_default.HTML);
1884
1915
  var _handleHtmlFragment = async (response) => document.createRange().createContextualFragment(await response.text());
1885
1916
  var _typeConverter = (data) => Object.fromEntries(Array.from(data.keys()).map((key, index, keys, value = data.getAll(key)) => [key, value.length > 1 ? value : value[0]]));
1886
- var _baseUrl, _options, _subscribr, _globalSubscribr, _contentTypeHandlers, _propertyTypeConverters, _defaultRequestOptions, _eventResponseStatuses, _get, get_fn, _request, request_fn, _throwHttpError, throwHttpError_fn, _publish, publish_fn, _generateResponseStatusFromError, generateResponseStatusFromError_fn, _processResponse, processResponse_fn, _createUrl, createUrl_fn, _needsSerialization, needsSerialization_fn, _convertRequestOptions, convertRequestOptions_fn;
1917
+ var _baseUrl, _options, _subscribr, _globalSubscribr, _activeRequests, _contentTypeHandlers, _propertyTypeConverters, _defaultRequestOptions, _eventResponseStatuses, _get, get_fn, _request, request_fn, _handleError, handleError_fn, _publish, publish_fn, _generateResponseStatusFromError, generateResponseStatusFromError_fn, _processResponse, processResponse_fn, _createUrl, createUrl_fn, _needsSerialization, needsSerialization_fn, _convertRequestOptions, convertRequestOptions_fn;
1887
1918
  var _Transportr = class {
1888
1919
  /**
1889
1920
  * Create a new Transportr instance with the provided location or origin and context path.
@@ -1925,19 +1956,33 @@ var Transportr = (() => {
1925
1956
  * @private
1926
1957
  * @param {URL} url The path to the resource you want to access.
1927
1958
  * @param {import('./http-error.js').HttpErrorOptions} options The options for the HttpError.
1928
- * @returns {void}
1959
+ * @returns {HttpError} The HttpError.
1929
1960
  */
1930
- __privateAdd(this, _throwHttpError);
1961
+ __privateAdd(this, _handleError);
1931
1962
  /**
1932
1963
  * Publishes an event to the global and instance subscribers.
1933
1964
  *
1934
1965
  * @private
1935
1966
  * @param {string} eventName The name of the event.
1936
- * @param {Event} event The event object.
1937
- * @param {*} data The data to pass to the subscribers.
1967
+ * @param {boolean} global Whether or not to publish the event to the global subscribers.
1968
+ * @param {Event} [event] The event object.
1969
+ * @param {*} [data] The data to pass to the subscribers.
1938
1970
  * @returns {void}
1939
1971
  */
1940
1972
  __privateAdd(this, _publish);
1973
+ /**
1974
+ * It takes a response and a handler, and if the handler is not defined, it tries to find a handler
1975
+ * based on the response's content type
1976
+ *
1977
+ * @private
1978
+ * @static
1979
+ * @async
1980
+ * @param {Response} response - The response object returned by the fetch API.
1981
+ * @param {URL} url - The path to the resource you want to access. Used for error handling.
1982
+ * @param {ResponseHandler<ResponseBody>} [handler] - The handler to use for processing the response.
1983
+ * @returns {Promise<ResponseBody>} The response is being returned.
1984
+ */
1985
+ __privateAdd(this, _processResponse);
1941
1986
  /** @type {URL} */
1942
1987
  __privateAdd(this, _baseUrl, void 0);
1943
1988
  /** @type {RequestOptions} */
@@ -1960,10 +2005,11 @@ var Transportr = (() => {
1960
2005
  * Returns a {@link SignalController} used for aborting requests.
1961
2006
  *
1962
2007
  * @static
2008
+ * @param {AbortSignal} [signal] The optional {@link AbortSignal} to used for chaining.
1963
2009
  * @returns {SignalController} A new {@link SignalController} instance.
1964
2010
  */
1965
- static signalController() {
1966
- return new SignalController();
2011
+ static signalController(signal) {
2012
+ return new SignalController(signal);
1967
2013
  }
1968
2014
  /**
1969
2015
  * Returns a {@link EventRegistration} used for subscribing to global events.
@@ -1987,6 +2033,21 @@ var Transportr = (() => {
1987
2033
  static unregister(eventRegistration) {
1988
2034
  return __privateGet(_Transportr, _globalSubscribr).unsubscribe(eventRegistration);
1989
2035
  }
2036
+ /**
2037
+ * Aborts all active requests.
2038
+ * This is useful for when the user navigates away from the current page.
2039
+ * This will also clear the {@link Transportr#activeRequests} array.
2040
+ * This is called automatically when the {@link Transportr#abort} method is called.
2041
+ *
2042
+ * @static
2043
+ * @returns {void}
2044
+ */
2045
+ static abortAll() {
2046
+ for (const signalController of __privateGet(this, _activeRequests)) {
2047
+ signalController.abort();
2048
+ }
2049
+ __privateSet(this, _activeRequests, []);
2050
+ }
1990
2051
  /**
1991
2052
  * It returns the base {@link URL} for the API.
1992
2053
  *
@@ -2115,7 +2176,7 @@ var Transportr = (() => {
2115
2176
  * @returns {Promise<JsonObject>} A promise that resolves to the response body as a JSON object.
2116
2177
  */
2117
2178
  async getJson(path, options) {
2118
- return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: http_media_type_default.JSON } }, _handleJson);
2179
+ return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: _mediaTypes.get(http_media_type_default.JSON).toString() } }, _handleJson);
2119
2180
  }
2120
2181
  /**
2121
2182
  * It gets the XML representation of the resource at the given path.
@@ -2126,7 +2187,7 @@ var Transportr = (() => {
2126
2187
  * @returns {Promise<Document>} The result of the function call to #get.
2127
2188
  */
2128
2189
  async getXml(path, options) {
2129
- return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: http_media_type_default.XML } }, _handleXml);
2190
+ return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: _mediaTypes.get(http_media_type_default.XML).toString() } }, _handleXml);
2130
2191
  }
2131
2192
  /**
2132
2193
  * Get the HTML content of the specified path.
@@ -2139,7 +2200,7 @@ var Transportr = (() => {
2139
2200
  * method of the promise returned by the `#get` method.
2140
2201
  */
2141
2202
  async getHtml(path, options) {
2142
- return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: http_media_type_default.HTML } }, _handleHtml);
2203
+ return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: _mediaTypes.get(http_media_type_default.HTML).toString() } }, _handleHtml);
2143
2204
  }
2144
2205
  /**
2145
2206
  * It returns a promise that resolves to the HTML fragment at the given path.
@@ -2151,7 +2212,7 @@ var Transportr = (() => {
2151
2212
  * @returns {Promise<DocumentFragment>} A promise that resolves to an HTML fragment.
2152
2213
  */
2153
2214
  async getHtmlFragment(path, options) {
2154
- return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: http_media_type_default.HTML } }, _handleHtmlFragment);
2215
+ return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: _mediaTypes.get(http_media_type_default.HTML).toString() } }, _handleHtmlFragment);
2155
2216
  }
2156
2217
  /**
2157
2218
  * It gets a script from the server, and appends the script to the {@link Document} {@link HTMLHeadElement}
@@ -2163,7 +2224,7 @@ var Transportr = (() => {
2163
2224
  * @returns {Promise<void>} A promise that has been resolved.
2164
2225
  */
2165
2226
  async getScript(path, options) {
2166
- return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: http_media_type_default.JAVA_SCRIPT } }, _handleScript);
2227
+ return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: _mediaTypes.get(http_media_type_default.JAVA_SCRIPT).toString() } }, _handleScript);
2167
2228
  }
2168
2229
  /**
2169
2230
  * Gets a stylesheet from the server, and adds it as a {@link Blob} {@link URL}.
@@ -2174,7 +2235,7 @@ var Transportr = (() => {
2174
2235
  * @returns {Promise<void>} A promise that has been resolved.
2175
2236
  */
2176
2237
  async getStylesheet(path, options) {
2177
- return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: http_media_type_default.CSS } }, _handleCss);
2238
+ return __privateMethod(this, _get, get_fn).call(this, path, options, { headers: { [http_request_headers_default.ACCEPT]: _mediaTypes.get(http_media_type_default.CSS).toString() } }, _handleCss);
2178
2239
  }
2179
2240
  /**
2180
2241
  * It returns a blob from the specified path.
@@ -2235,6 +2296,7 @@ var Transportr = (() => {
2235
2296
  _options = new WeakMap();
2236
2297
  _subscribr = new WeakMap();
2237
2298
  _globalSubscribr = new WeakMap();
2299
+ _activeRequests = new WeakMap();
2238
2300
  _contentTypeHandlers = new WeakMap();
2239
2301
  _propertyTypeConverters = new WeakMap();
2240
2302
  _defaultRequestOptions = new WeakMap();
@@ -2245,58 +2307,66 @@ var Transportr = (() => {
2245
2307
  };
2246
2308
  _request = new WeakSet();
2247
2309
  request_fn = async function(path, userOptions = {}, options = {}, responseHandler) {
2248
- var _a, _b, _c, _d, _e, _f, _g;
2310
+ var _a, _b, _c, _d, _e;
2249
2311
  const requestOptions = object_merge_default(__privateGet(this, _options), __privateMethod(_a = _Transportr, _convertRequestOptions, convertRequestOptions_fn).call(_a, userOptions), options);
2250
2312
  const url = __privateMethod(_b = _Transportr, _createUrl, createUrl_fn).call(_b, __privateGet(this, _baseUrl), path, requestOptions.searchParams);
2251
2313
  const signalController = new SignalController(requestOptions.signal);
2314
+ __privateGet(_Transportr, _activeRequests).push(signalController);
2252
2315
  requestOptions.signal = signalController.signal;
2253
2316
  if (__privateMethod(_c = _Transportr, _needsSerialization, needsSerialization_fn).call(_c, requestOptions.method, requestOptions.headers[http_request_headers_default.CONTENT_TYPE])) {
2254
2317
  try {
2255
2318
  requestOptions.body = JSON.stringify(requestOptions.body);
2256
2319
  } catch (error) {
2257
- __privateMethod(this, _throwHttpError, throwHttpError_fn).call(this, url, { cause: error });
2320
+ return Promise.reject(new HttpError(url, { cause: error }));
2258
2321
  }
2259
2322
  } else if (requestOptions.method == http_request_methods_default.GET && requestOptions.headers[http_request_headers_default.CONTENT_TYPE] != "") {
2260
2323
  delete requestOptions.headers[http_request_headers_default.CONTENT_TYPE];
2261
2324
  delete requestOptions.body;
2262
2325
  }
2263
- requestOptions.signal.addEventListener("abort", (event) => __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.ABORTED, event));
2264
- requestOptions.signal.addEventListener("timeout", (event) => __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.TIMEOUT, event));
2265
- __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.CONFIGURED, requestOptions);
2326
+ requestOptions.signal.addEventListener("abort", (event) => __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.ABORTED, requestOptions.global, event));
2327
+ requestOptions.signal.addEventListener("timeout", (event) => __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.TIMEOUT, requestOptions.global, event));
2328
+ __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.CONFIGURED, requestOptions.global, requestOptions);
2266
2329
  let result, timeoutId, response;
2267
2330
  try {
2268
- try {
2269
- timeoutId = setTimeout(() => {
2270
- const cause = new DOMException(`The call to '${url}' timed-out after ${requestOptions.timeout / 1e3} seconds`, "TimeoutError");
2271
- signalController.abort(cause);
2272
- requestOptions.signal.dispatchEvent(new CustomEvent(_Transportr.Events.TIMEOUT, { detail: { url, options: requestOptions, cause } }));
2273
- }, requestOptions.timeout);
2274
- response = await fetch(url, requestOptions);
2275
- } catch (error) {
2276
- __privateMethod(this, _throwHttpError, throwHttpError_fn).call(this, url, { cause: error, status: __privateMethod(_d = _Transportr, _generateResponseStatusFromError, generateResponseStatusFromError_fn).call(_d, error.name, response) });
2277
- }
2331
+ timeoutId = setTimeout(() => {
2332
+ const cause = new DOMException(`The call to '${url}' timed-out after ${requestOptions.timeout / 1e3} seconds`, "TimeoutError");
2333
+ signalController.abort(cause);
2334
+ requestOptions.signal.dispatchEvent(new CustomEvent(_Transportr.Events.TIMEOUT, { detail: { url, options: requestOptions, cause } }));
2335
+ }, requestOptions.timeout);
2336
+ response = await fetch(url, requestOptions);
2278
2337
  if (!response.ok) {
2279
- __privateMethod(this, _throwHttpError, throwHttpError_fn).call(this, url, { status: __privateMethod(_e = _Transportr, _generateResponseStatusFromError, generateResponseStatusFromError_fn).call(_e, "ResponseError", response), entity: await __privateMethod(_f = _Transportr, _processResponse, processResponse_fn).call(_f, response) });
2338
+ return Promise.reject(__privateMethod(this, _handleError, handleError_fn).call(this, url, { status: __privateMethod(_d = _Transportr, _generateResponseStatusFromError, generateResponseStatusFromError_fn).call(_d, "ResponseError", response), entity: await __privateMethod(this, _processResponse, processResponse_fn).call(this, response, url) }));
2280
2339
  }
2281
- result = await __privateMethod(_g = _Transportr, _processResponse, processResponse_fn).call(_g, response, responseHandler);
2282
- __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.SUCCESS, result);
2340
+ result = await __privateMethod(this, _processResponse, processResponse_fn).call(this, response, url, responseHandler);
2341
+ __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.SUCCESS, requestOptions.global, result);
2342
+ } catch (error) {
2343
+ return Promise.reject(__privateMethod(this, _handleError, handleError_fn).call(this, url, { cause: error, status: __privateMethod(_e = _Transportr, _generateResponseStatusFromError, generateResponseStatusFromError_fn).call(_e, error.name, response) }));
2283
2344
  } finally {
2284
2345
  clearTimeout(timeoutId);
2285
2346
  if (!requestOptions.signal.aborted) {
2286
- __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.COMPLETE, response);
2347
+ __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.COMPLETE, requestOptions.global, response);
2348
+ const index = __privateGet(_Transportr, _activeRequests).indexOf(signalController);
2349
+ if (index > -1) {
2350
+ __privateGet(_Transportr, _activeRequests).splice(index, 1);
2351
+ }
2352
+ if (__privateGet(_Transportr, _activeRequests).length === 0) {
2353
+ __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.ALL_COMPLETE, requestOptions.global, response);
2354
+ }
2287
2355
  }
2288
2356
  }
2289
2357
  return result;
2290
2358
  };
2291
- _throwHttpError = new WeakSet();
2292
- throwHttpError_fn = function(url, options) {
2359
+ _handleError = new WeakSet();
2360
+ handleError_fn = function(url, options) {
2293
2361
  const error = new HttpError(`An error has occurred with your request to: '${url}'`, options);
2294
- __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.ERROR, error);
2295
- throw error;
2362
+ __privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.ERROR, true, error);
2363
+ return error;
2296
2364
  };
2297
2365
  _publish = new WeakSet();
2298
- publish_fn = function(eventName, event, data) {
2299
- __privateGet(_Transportr, _globalSubscribr).publish(eventName, event, data);
2366
+ publish_fn = function(eventName, global, event, data) {
2367
+ if (global) {
2368
+ __privateGet(_Transportr, _globalSubscribr).publish(eventName, event, data);
2369
+ }
2300
2370
  __privateGet(this, _subscribr).publish(eventName, event, data);
2301
2371
  };
2302
2372
  _generateResponseStatusFromError = new WeakSet();
@@ -2311,10 +2381,11 @@ var Transportr = (() => {
2311
2381
  }
2312
2382
  };
2313
2383
  _processResponse = new WeakSet();
2314
- processResponse_fn = async function(response, handler) {
2384
+ processResponse_fn = async function(response, url, handler) {
2315
2385
  try {
2386
+ let mediaType;
2316
2387
  if (!handler) {
2317
- const mediaType = new MediaType(response.headers.get(http_response_headers_default.CONTENT_TYPE));
2388
+ mediaType = MediaType.parse(response.headers.get(http_response_headers_default.CONTENT_TYPE));
2318
2389
  if (mediaType) {
2319
2390
  for (const [responseHandler, contentTypes] of __privateGet(_Transportr, _contentTypeHandlers)) {
2320
2391
  if (contentTypes.has(mediaType.type) || contentTypes.has(mediaType.subtype)) {
@@ -2326,20 +2397,21 @@ var Transportr = (() => {
2326
2397
  }
2327
2398
  return (handler ?? _handleText)(response);
2328
2399
  } catch (error) {
2329
- const errorMessage = "Unable to process response.";
2330
- console.error(errorMessage, error, response);
2331
- throw new HttpError(errorMessage, { cause: error });
2400
+ console.error("Unable to process response.", error, response);
2401
+ return Promise.reject(__privateMethod(this, _handleError, handleError_fn).call(this, url, { cause: error }));
2332
2402
  }
2333
2403
  };
2334
2404
  _createUrl = new WeakSet();
2335
2405
  createUrl_fn = function(url, path, searchParams = {}) {
2336
- url = path.startsWith("/") ? new URL(`${url.pathname.replace(endsWithSlashRegEx, "")}${path}`, url.origin) : new URL(path);
2406
+ if (path) {
2407
+ url = path.startsWith("/") ? new URL(`${url.pathname.replace(endsWithSlashRegEx, "")}${path}`, url.origin) : new URL(path);
2408
+ }
2337
2409
  Object.entries(searchParams).forEach(([key, value]) => url.searchParams.append(key, value));
2338
2410
  return url;
2339
2411
  };
2340
2412
  _needsSerialization = new WeakSet();
2341
2413
  needsSerialization_fn = function(method, contentType) {
2342
- return contentType == http_media_type_default.JSON && [http_request_methods_default.POST, http_request_methods_default.PUT, http_request_methods_default.PATCH].includes(method);
2414
+ return (_mediaTypes.get(contentType) ?? new MediaType(contentType)).essence == http_media_type_default.JSON && [http_request_methods_default.POST, http_request_methods_default.PUT, http_request_methods_default.PATCH].includes(method);
2343
2415
  };
2344
2416
  _convertRequestOptions = new WeakSet();
2345
2417
  convertRequestOptions_fn = function(options) {
@@ -2362,18 +2434,6 @@ var Transportr = (() => {
2362
2434
  * @returns {ResponseStatus} The response status object.
2363
2435
  */
2364
2436
  __privateAdd(Transportr, _generateResponseStatusFromError);
2365
- /**
2366
- * It takes a response and a handler, and if the handler is not defined, it tries to find a handler
2367
- * based on the response's content type
2368
- *
2369
- * @private
2370
- * @static
2371
- * @async
2372
- * @param {Response} response - The response object returned by the fetch API.
2373
- * @param {ResponseHandler<ResponseBody>} [handler] - The handler to use for processing the response.
2374
- * @returns {Promise<ResponseBody>} The response is being returned.
2375
- */
2376
- __privateAdd(Transportr, _processResponse);
2377
2437
  /**
2378
2438
  * It takes a URL, a path, and a set of search parameters, and returns a new URL with the path and
2379
2439
  * search parameters applied.
@@ -2406,20 +2466,22 @@ var Transportr = (() => {
2406
2466
  __privateAdd(Transportr, _convertRequestOptions);
2407
2467
  /** @type {Subscribr} */
2408
2468
  __privateAdd(Transportr, _globalSubscribr, new Subscribr());
2469
+ /** @type {Array<SignalController>} */
2470
+ __privateAdd(Transportr, _activeRequests, []);
2409
2471
  /**
2410
2472
  * @private
2411
2473
  * @static
2412
2474
  * @type {SetMultiMap<ResponseHandler<ResponseBody>, string>}
2413
2475
  */
2414
2476
  __privateAdd(Transportr, _contentTypeHandlers, new SetMultiMap([
2415
- [_handleImage, new MediaType(http_media_type_default.PNG).type],
2416
- [_handleText, new MediaType(http_media_type_default.TEXT).type],
2417
- [_handleJson, new MediaType(http_media_type_default.JSON).subtype],
2418
- [_handleHtml, new MediaType(http_media_type_default.HTML).subtype],
2419
- [_handleScript, new MediaType(http_media_type_default.JAVA_SCRIPT).subtype],
2420
- [_handleCss, new MediaType(http_media_type_default.CSS).subtype],
2421
- [_handleXml, new MediaType(http_media_type_default.XML).subtype],
2422
- [_handleReadableStream, new MediaType(http_media_type_default.BIN).subtype]
2477
+ [_handleImage, _mediaTypes.get(http_media_type_default.PNG).type],
2478
+ [_handleText, _mediaTypes.get(http_media_type_default.TEXT).type],
2479
+ [_handleJson, _mediaTypes.get(http_media_type_default.JSON).subtype],
2480
+ [_handleHtml, _mediaTypes.get(http_media_type_default.HTML).subtype],
2481
+ [_handleScript, _mediaTypes.get(http_media_type_default.JAVA_SCRIPT).subtype],
2482
+ [_handleCss, _mediaTypes.get(http_media_type_default.CSS).subtype],
2483
+ [_handleXml, _mediaTypes.get(http_media_type_default.XML).subtype],
2484
+ [_handleReadableStream, _mediaTypes.get(http_media_type_default.BIN).subtype]
2423
2485
  ]));
2424
2486
  /**
2425
2487
  * @private
@@ -2516,7 +2578,8 @@ var Transportr = (() => {
2516
2578
  ERROR: "error",
2517
2579
  ABORTED: "aborted",
2518
2580
  TIMEOUT: "timeout",
2519
- COMPLETE: "complete"
2581
+ COMPLETE: "complete",
2582
+ ALL_COMPLETE: "all-complete"
2520
2583
  }));
2521
2584
  /**
2522
2585
  * @private
@@ -2527,7 +2590,7 @@ var Transportr = (() => {
2527
2590
  body: null,
2528
2591
  cache: _Transportr.CachingPolicy.NO_STORE,
2529
2592
  credentials: _Transportr.CredentialsPolicy.SAME_ORIGIN,
2530
- headers: { [http_request_headers_default.CONTENT_TYPE]: _Transportr.MediaType.JSON, [http_request_headers_default.ACCEPT]: _Transportr.MediaType.JSON },
2593
+ headers: { [http_request_headers_default.CONTENT_TYPE]: _mediaTypes.get(http_media_type_default.JSON).toString(), [http_request_headers_default.ACCEPT]: _mediaTypes.get(http_media_type_default.JSON).toString() },
2531
2594
  searchParams: {},
2532
2595
  integrity: void 0,
2533
2596
  keepalive: void 0,
@@ -2538,6 +2601,7 @@ var Transportr = (() => {
2538
2601
  referrerPolicy: _Transportr.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
2539
2602
  signal: void 0,
2540
2603
  timeout: 1e4,
2604
+ global: true,
2541
2605
  window: null
2542
2606
  }));
2543
2607
  /**