@d1g1tal/transportr 0.1.6 → 1.1.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/transportr.js +382 -61
- package/dist/transportr.min.js +2 -2
- package/dist/transportr.min.js.map +4 -4
- package/package.json +10 -9
- package/src/http-error.js +1 -1
- package/src/response-status.js +1 -1
- package/src/signal-controller.js +50 -0
- package/src/transportr.js +237 -34
package/dist/transportr.js
CHANGED
|
@@ -54,45 +54,33 @@ var Transportr = (() => {
|
|
|
54
54
|
var _type = (object) => object?.constructor ?? object?.prototype?.constructor ?? globalThis[Object.prototype.toString.call(object).slice(8, -1)] ?? object;
|
|
55
55
|
var object_type_default = _type;
|
|
56
56
|
|
|
57
|
-
// node_modules/@d1g1tal/chrysalis/src/esm/object-is-
|
|
58
|
-
var
|
|
59
|
-
var
|
|
57
|
+
// node_modules/@d1g1tal/chrysalis/src/esm/object-is-empty.js
|
|
58
|
+
var _objectIsEmpty = (object) => !!object && Object.keys(object).length === 0 && object.constructor === Object;
|
|
59
|
+
var object_is_empty_default = _objectIsEmpty;
|
|
60
60
|
|
|
61
61
|
// node_modules/@d1g1tal/chrysalis/src/esm/object-merge.js
|
|
62
62
|
var _objectMerge = (...objects) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
const target = {};
|
|
64
|
+
for (const source of objects) {
|
|
65
|
+
if (object_type_default(source) != Object)
|
|
66
|
+
return void 0;
|
|
67
|
+
let descriptor, sourceType;
|
|
68
|
+
for (const property of Object.getOwnPropertyNames(source)) {
|
|
69
|
+
descriptor = Object.getOwnPropertyDescriptor(source, property);
|
|
70
|
+
if (descriptor.enumerable) {
|
|
71
|
+
sourceType = object_type_default(source[property]);
|
|
72
|
+
if (sourceType == Object) {
|
|
73
|
+
descriptor.value = object_type_default(target[property]) == Object ? _objectMerge(target[property], source[property]) : { ...source[property] };
|
|
74
|
+
} else if (sourceType == Array) {
|
|
75
|
+
descriptor.value = Array.isArray(target[property]) ? [.../* @__PURE__ */ new Set([...source[property], ...target[property]])] : [...source[property]];
|
|
69
76
|
}
|
|
70
|
-
|
|
71
|
-
previousValue[key] = _objectMerge(previousValue[key], value);
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
default:
|
|
75
|
-
previousValue[key] = value;
|
|
77
|
+
target[property] = descriptor.value;
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
|
-
return previousValue;
|
|
79
|
-
}, {});
|
|
80
|
-
};
|
|
81
|
-
var object_merge_default = _objectMerge;
|
|
82
|
-
|
|
83
|
-
// node_modules/@d1g1tal/chrysalis/src/esm/is-iterable.js
|
|
84
|
-
var _isIterable = (input) => !!input?.[Symbol.iterator];
|
|
85
|
-
var is_iterable_default = _isIterable;
|
|
86
|
-
|
|
87
|
-
// node_modules/@d1g1tal/chrysalis/src/esm/object-construct.js
|
|
88
|
-
var _construct = (value, args = []) => {
|
|
89
|
-
try {
|
|
90
|
-
return Reflect.construct(value, args);
|
|
91
|
-
} catch (error) {
|
|
92
|
-
return value(...args);
|
|
93
80
|
}
|
|
81
|
+
return target;
|
|
94
82
|
};
|
|
95
|
-
var
|
|
83
|
+
var object_merge_default = _objectMerge;
|
|
96
84
|
|
|
97
85
|
// node_modules/@d1g1tal/collections/src/set-multi-map.js
|
|
98
86
|
var SetMultiMap = class extends Map {
|
|
@@ -518,6 +506,130 @@ var Transportr = (() => {
|
|
|
518
506
|
}
|
|
519
507
|
};
|
|
520
508
|
|
|
509
|
+
// node_modules/@d1g1tal/subscribr/src/context-event-handler.js
|
|
510
|
+
var ContextEventHandler = class {
|
|
511
|
+
#context;
|
|
512
|
+
#eventHandler;
|
|
513
|
+
/**
|
|
514
|
+
* @param {*} context The context to bind to the event handler.
|
|
515
|
+
* @param {function(*): void} eventHandler The event handler to call when the event is published.
|
|
516
|
+
*/
|
|
517
|
+
constructor(context, eventHandler) {
|
|
518
|
+
this.#context = context;
|
|
519
|
+
this.#eventHandler = eventHandler;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Call the event handler for the provided event.
|
|
523
|
+
*
|
|
524
|
+
* @param {Event} event The event to handle
|
|
525
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
526
|
+
*/
|
|
527
|
+
handle(event, data) {
|
|
528
|
+
this.#eventHandler.call(this.#context, event, data);
|
|
529
|
+
}
|
|
530
|
+
get [Symbol.toStringTag]() {
|
|
531
|
+
return "ContextEventHandler";
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// node_modules/@d1g1tal/subscribr/src/subscription.js
|
|
536
|
+
var Subscription = class {
|
|
537
|
+
#eventName;
|
|
538
|
+
#contextEventHandler;
|
|
539
|
+
/**
|
|
540
|
+
* @param {string} eventName The event name.
|
|
541
|
+
* @param {ContextEventHandler} contextEventHandler Then context event handler.
|
|
542
|
+
*/
|
|
543
|
+
constructor(eventName, contextEventHandler) {
|
|
544
|
+
this.#eventName = eventName;
|
|
545
|
+
this.#contextEventHandler = contextEventHandler;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Gets the event name for the subscription.
|
|
549
|
+
*
|
|
550
|
+
* @returns {string} The event name.
|
|
551
|
+
*/
|
|
552
|
+
get eventName() {
|
|
553
|
+
return this.#eventName;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Gets the context event handler.
|
|
557
|
+
*
|
|
558
|
+
* @returns {ContextEventHandler} The context event handler
|
|
559
|
+
*/
|
|
560
|
+
get contextEventHandler() {
|
|
561
|
+
return this.#contextEventHandler;
|
|
562
|
+
}
|
|
563
|
+
get [Symbol.toStringTag]() {
|
|
564
|
+
return "Subscription";
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// node_modules/@d1g1tal/subscribr/src/subscribr.js
|
|
569
|
+
var Subscribr = class {
|
|
570
|
+
/** @type {SetMultiMap<string, ContextEventHandler>} */
|
|
571
|
+
#subscribers;
|
|
572
|
+
constructor() {
|
|
573
|
+
this.#subscribers = new SetMultiMap();
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Subscribe to an event
|
|
577
|
+
*
|
|
578
|
+
* @param {string} eventName The event name to subscribe to.
|
|
579
|
+
* @param {function(Event, *): void} eventHandler The event handler to call when the event is published.
|
|
580
|
+
* @param {*} [context] The context to bind to the event handler.
|
|
581
|
+
* @returns {Subscription} An object used to check if the subscription still exists and to unsubscribe from the event.
|
|
582
|
+
*/
|
|
583
|
+
subscribe(eventName, eventHandler, context = eventHandler) {
|
|
584
|
+
const contextEventHandler = new ContextEventHandler(context, eventHandler);
|
|
585
|
+
this.#subscribers.set(eventName, contextEventHandler);
|
|
586
|
+
return new Subscription(eventName, contextEventHandler);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Unsubscribe from the event
|
|
590
|
+
*
|
|
591
|
+
* @param {Subscription} subscription The subscription to unsubscribe.
|
|
592
|
+
* @param {string} subscription.eventName The event name to subscribe to.
|
|
593
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to call when the event is published.
|
|
594
|
+
* @returns {boolean} true if eventListener has been removed successfully. false if the value is not found or if the value is not an object.
|
|
595
|
+
*/
|
|
596
|
+
unsubscribe({ eventName, contextEventHandler }) {
|
|
597
|
+
const contextEventHandlers = this.#subscribers.get(eventName);
|
|
598
|
+
const removed = contextEventHandlers?.delete(contextEventHandler);
|
|
599
|
+
if (removed && contextEventHandlers.size == 0) {
|
|
600
|
+
this.#subscribers.delete(eventName);
|
|
601
|
+
}
|
|
602
|
+
return removed;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Publish an event
|
|
606
|
+
*
|
|
607
|
+
* @param {string} eventName The name of the event.
|
|
608
|
+
* @param {Event} event The event to be handled.
|
|
609
|
+
* @param {*} [data] The value to be passed to the event handler as a parameter.
|
|
610
|
+
*/
|
|
611
|
+
publish(eventName, event = new CustomEvent(eventName), data) {
|
|
612
|
+
if (data == null && !(event instanceof Event)) {
|
|
613
|
+
[data, event] = [event, new CustomEvent(eventName)];
|
|
614
|
+
}
|
|
615
|
+
this.#subscribers.get(eventName)?.forEach((contextEventHandler) => contextEventHandler.handle(event, data));
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Check if the event and handler are subscribed.
|
|
619
|
+
*
|
|
620
|
+
* @param {Subscription} subscription The subscription object.
|
|
621
|
+
* @param {string} subscription.eventName The name of the event to check.
|
|
622
|
+
* @param {ContextEventHandler} subscription.contextEventHandler The event handler to check.
|
|
623
|
+
* @returns {boolean} true if the event name and handler are subscribed, false otherwise.
|
|
624
|
+
*/
|
|
625
|
+
isSubscribed({ eventName, contextEventHandler }) {
|
|
626
|
+
return this.#subscribers.get(eventName)?.has(contextEventHandler);
|
|
627
|
+
}
|
|
628
|
+
get [Symbol.toStringTag]() {
|
|
629
|
+
return "Subscribr";
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
|
|
521
633
|
// src/http-error.js
|
|
522
634
|
var HttpError = class extends Error {
|
|
523
635
|
/** @type {ResponseBody} */
|
|
@@ -1706,6 +1818,47 @@ var Transportr = (() => {
|
|
|
1706
1818
|
}
|
|
1707
1819
|
};
|
|
1708
1820
|
|
|
1821
|
+
// src/signal-controller.js
|
|
1822
|
+
var SignalController = class {
|
|
1823
|
+
/** @type {AbortController} */
|
|
1824
|
+
#abortController;
|
|
1825
|
+
/**
|
|
1826
|
+
* @param {AbortSignal} [signal] The signal to be used to abort the request.
|
|
1827
|
+
*/
|
|
1828
|
+
constructor(signal) {
|
|
1829
|
+
this.#abortController = new AbortController();
|
|
1830
|
+
signal?.addEventListener("abort", () => this.#abortController.abort());
|
|
1831
|
+
}
|
|
1832
|
+
/**
|
|
1833
|
+
* Returns the {@link AbortSignal} object associated with this object.
|
|
1834
|
+
*
|
|
1835
|
+
* @returns {AbortSignal} The {@link AbortSignal} object associated with this object.
|
|
1836
|
+
*/
|
|
1837
|
+
get signal() {
|
|
1838
|
+
return this.#abortController.signal;
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* Aborts a DOM request before it has completed.
|
|
1842
|
+
* This is able to abort fetch requests, data sent using the XMLHttpRequest API, and Web Sockets.
|
|
1843
|
+
*
|
|
1844
|
+
* @param {DOMException} [reason] The reason for aborting the request.
|
|
1845
|
+
* @returns {void}
|
|
1846
|
+
*/
|
|
1847
|
+
abort(reason) {
|
|
1848
|
+
this.#abortController.abort(reason);
|
|
1849
|
+
}
|
|
1850
|
+
/**
|
|
1851
|
+
* A String value that is used in the creation of the default string
|
|
1852
|
+
* description of an object. Called by the built-in method {@link Object.prototype.toString}.
|
|
1853
|
+
*
|
|
1854
|
+
* @override
|
|
1855
|
+
* @returns {string} The default string description of this object.
|
|
1856
|
+
*/
|
|
1857
|
+
get [Symbol.toStringTag]() {
|
|
1858
|
+
return "SignalController";
|
|
1859
|
+
}
|
|
1860
|
+
};
|
|
1861
|
+
|
|
1709
1862
|
// src/transportr.js
|
|
1710
1863
|
var endsWithSlashRegEx = /\/$/;
|
|
1711
1864
|
var _handleText = async (response) => await response.text();
|
|
@@ -1729,13 +1882,14 @@ var Transportr = (() => {
|
|
|
1729
1882
|
var _handleXml = async (response) => new DOMParser().parseFromString(await response.text(), http_media_type_default.XML.essence);
|
|
1730
1883
|
var _handleHtml = async (response) => new DOMParser().parseFromString(await response.text(), http_media_type_default.HTML.essence);
|
|
1731
1884
|
var _handleHtmlFragment = async (response) => document.createRange().createContextualFragment(await response.text());
|
|
1732
|
-
var
|
|
1885
|
+
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;
|
|
1733
1887
|
var _Transportr = class {
|
|
1734
1888
|
/**
|
|
1735
1889
|
* Create a new Transportr instance with the provided location or origin and context path.
|
|
1736
1890
|
*
|
|
1737
|
-
* @param {URL
|
|
1738
|
-
* @param {RequestOptions} [options=
|
|
1891
|
+
* @param {URL|string|RequestOptions} [url=location.origin] The URL for {@link fetch} requests.
|
|
1892
|
+
* @param {RequestOptions} [options={}] The default {@link RequestOptions} for this instance.
|
|
1739
1893
|
*/
|
|
1740
1894
|
constructor(url = location.origin, options = {}) {
|
|
1741
1895
|
/**
|
|
@@ -1765,10 +1919,31 @@ var Transportr = (() => {
|
|
|
1765
1919
|
* @returns {Promise<ResponseBody>} The response from the API call.
|
|
1766
1920
|
*/
|
|
1767
1921
|
__privateAdd(this, _request);
|
|
1922
|
+
/**
|
|
1923
|
+
* Handles an error by logging it and throwing it.
|
|
1924
|
+
*
|
|
1925
|
+
* @private
|
|
1926
|
+
* @param {URL} url The path to the resource you want to access.
|
|
1927
|
+
* @param {import('./http-error.js').HttpErrorOptions} options The options for the HttpError.
|
|
1928
|
+
* @returns {void}
|
|
1929
|
+
*/
|
|
1930
|
+
__privateAdd(this, _throwHttpError);
|
|
1931
|
+
/**
|
|
1932
|
+
* Publishes an event to the global and instance subscribers.
|
|
1933
|
+
*
|
|
1934
|
+
* @private
|
|
1935
|
+
* @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.
|
|
1938
|
+
* @returns {void}
|
|
1939
|
+
*/
|
|
1940
|
+
__privateAdd(this, _publish);
|
|
1768
1941
|
/** @type {URL} */
|
|
1769
1942
|
__privateAdd(this, _baseUrl, void 0);
|
|
1770
1943
|
/** @type {RequestOptions} */
|
|
1771
1944
|
__privateAdd(this, _options, void 0);
|
|
1945
|
+
/** @type {Subscribr} */
|
|
1946
|
+
__privateAdd(this, _subscribr, void 0);
|
|
1772
1947
|
var _a;
|
|
1773
1948
|
const type = object_type_default(url);
|
|
1774
1949
|
if (type == Object) {
|
|
@@ -1778,7 +1953,39 @@ var Transportr = (() => {
|
|
|
1778
1953
|
url = url.startsWith("/") ? new URL(url, location.origin) : new URL(url);
|
|
1779
1954
|
}
|
|
1780
1955
|
__privateSet(this, _baseUrl, url);
|
|
1781
|
-
__privateSet(this, _options, object_merge_default(__privateGet(_Transportr, _defaultRequestOptions), __privateMethod(_a = _Transportr, _convertRequestOptions, convertRequestOptions_fn).call(_a, options
|
|
1956
|
+
__privateSet(this, _options, object_merge_default(__privateGet(_Transportr, _defaultRequestOptions), __privateMethod(_a = _Transportr, _convertRequestOptions, convertRequestOptions_fn).call(_a, options)));
|
|
1957
|
+
__privateSet(this, _subscribr, new Subscribr());
|
|
1958
|
+
}
|
|
1959
|
+
/**
|
|
1960
|
+
* Returns a {@link SignalController} used for aborting requests.
|
|
1961
|
+
*
|
|
1962
|
+
* @static
|
|
1963
|
+
* @returns {SignalController} A new {@link SignalController} instance.
|
|
1964
|
+
*/
|
|
1965
|
+
static signalController() {
|
|
1966
|
+
return new SignalController();
|
|
1967
|
+
}
|
|
1968
|
+
/**
|
|
1969
|
+
* Returns a {@link EventRegistration} used for subscribing to global events.
|
|
1970
|
+
*
|
|
1971
|
+
* @static
|
|
1972
|
+
* @param {TransportrEvent} event The event to subscribe to.
|
|
1973
|
+
* @param {function(Event, *): void} handler The event handler.
|
|
1974
|
+
* @param {*} context The context to bind the handler to.
|
|
1975
|
+
* @returns {EventRegistration} A new {@link EventRegistration} instance.
|
|
1976
|
+
*/
|
|
1977
|
+
static register(event, handler, context) {
|
|
1978
|
+
return __privateGet(_Transportr, _globalSubscribr).subscribe(event, handler, context);
|
|
1979
|
+
}
|
|
1980
|
+
/**
|
|
1981
|
+
* Removes a {@link EventRegistration} from the global event handler.
|
|
1982
|
+
*
|
|
1983
|
+
* @static
|
|
1984
|
+
* @param {EventRegistration} eventRegistration The {@link EventRegistration} to remove.
|
|
1985
|
+
* @returns {boolean} True if the {@link EventRegistration} was removed, false otherwise.
|
|
1986
|
+
*/
|
|
1987
|
+
static unregister(eventRegistration) {
|
|
1988
|
+
return __privateGet(_Transportr, _globalSubscribr).unsubscribe(eventRegistration);
|
|
1782
1989
|
}
|
|
1783
1990
|
/**
|
|
1784
1991
|
* It returns the base {@link URL} for the API.
|
|
@@ -1788,6 +1995,26 @@ var Transportr = (() => {
|
|
|
1788
1995
|
get baseUrl() {
|
|
1789
1996
|
return __privateGet(this, _baseUrl);
|
|
1790
1997
|
}
|
|
1998
|
+
/**
|
|
1999
|
+
* Registers an event handler with a {@link Transportr} instance.
|
|
2000
|
+
*
|
|
2001
|
+
* @param {TransportrEvent} event The name of the event to listen for.
|
|
2002
|
+
* @param {function(Event, *): void} handler The function to call when the event is triggered.
|
|
2003
|
+
* @param {*} [context] The context to bind to the handler.
|
|
2004
|
+
* @returns {EventRegistration} An object that can be used to remove the event handler.
|
|
2005
|
+
*/
|
|
2006
|
+
register(event, handler, context) {
|
|
2007
|
+
return __privateGet(this, _subscribr).subscribe(event, handler, context);
|
|
2008
|
+
}
|
|
2009
|
+
/**
|
|
2010
|
+
* Unregisters an event handler from a {@link Transportr} instance.
|
|
2011
|
+
*
|
|
2012
|
+
* @param {EventRegistration} eventRegistration The event registration to remove.
|
|
2013
|
+
* @returns {void}
|
|
2014
|
+
*/
|
|
2015
|
+
unregister(eventRegistration) {
|
|
2016
|
+
__privateGet(this, _subscribr).unsubscribe(eventRegistration);
|
|
2017
|
+
}
|
|
1791
2018
|
/**
|
|
1792
2019
|
* This function returns a promise that resolves to the result of a request to the specified path with
|
|
1793
2020
|
* the specified options, where the method is GET.
|
|
@@ -1810,7 +2037,7 @@ var Transportr = (() => {
|
|
|
1810
2037
|
* @returns {Promise<ResponseBody>} A promise that resolves to the response body.
|
|
1811
2038
|
*/
|
|
1812
2039
|
async post(path, body, options) {
|
|
1813
|
-
return __privateMethod(this, _request, request_fn).call(this, path, options,
|
|
2040
|
+
return __privateMethod(this, _request, request_fn).call(this, path, { ...options, body }, { method: http_request_methods_default.POST });
|
|
1814
2041
|
}
|
|
1815
2042
|
/**
|
|
1816
2043
|
* This function returns a promise that resolves to the result of a request to the specified path with
|
|
@@ -2006,31 +2233,82 @@ var Transportr = (() => {
|
|
|
2006
2233
|
var Transportr = _Transportr;
|
|
2007
2234
|
_baseUrl = new WeakMap();
|
|
2008
2235
|
_options = new WeakMap();
|
|
2236
|
+
_subscribr = new WeakMap();
|
|
2237
|
+
_globalSubscribr = new WeakMap();
|
|
2009
2238
|
_contentTypeHandlers = new WeakMap();
|
|
2239
|
+
_propertyTypeConverters = new WeakMap();
|
|
2010
2240
|
_defaultRequestOptions = new WeakMap();
|
|
2241
|
+
_eventResponseStatuses = new WeakMap();
|
|
2011
2242
|
_get = new WeakSet();
|
|
2012
2243
|
get_fn = async function(path, userOptions, options, responseHandler) {
|
|
2013
2244
|
return __privateMethod(this, _request, request_fn).call(this, path, userOptions, options, responseHandler);
|
|
2014
2245
|
};
|
|
2015
2246
|
_request = new WeakSet();
|
|
2016
2247
|
request_fn = async function(path, userOptions = {}, options = {}, responseHandler) {
|
|
2017
|
-
var _a, _b, _c, _d, _e;
|
|
2018
|
-
const requestOptions = __privateMethod(_a = _Transportr, _convertRequestOptions, convertRequestOptions_fn).call(_a,
|
|
2019
|
-
const
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2248
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
2249
|
+
const requestOptions = object_merge_default(__privateGet(this, _options), __privateMethod(_a = _Transportr, _convertRequestOptions, convertRequestOptions_fn).call(_a, userOptions), options);
|
|
2250
|
+
const url = __privateMethod(_b = _Transportr, _createUrl, createUrl_fn).call(_b, __privateGet(this, _baseUrl), path, requestOptions.searchParams);
|
|
2251
|
+
const signalController = new SignalController(requestOptions.signal);
|
|
2252
|
+
requestOptions.signal = signalController.signal;
|
|
2253
|
+
if (__privateMethod(_c = _Transportr, _needsSerialization, needsSerialization_fn).call(_c, requestOptions.method, requestOptions.headers[http_request_headers_default.CONTENT_TYPE])) {
|
|
2254
|
+
try {
|
|
2255
|
+
requestOptions.body = JSON.stringify(requestOptions.body);
|
|
2256
|
+
} catch (error) {
|
|
2257
|
+
__privateMethod(this, _throwHttpError, throwHttpError_fn).call(this, url, { cause: error });
|
|
2258
|
+
}
|
|
2259
|
+
} else if (requestOptions.method == http_request_methods_default.GET && requestOptions.headers[http_request_headers_default.CONTENT_TYPE] != "") {
|
|
2260
|
+
delete requestOptions.headers[http_request_headers_default.CONTENT_TYPE];
|
|
2261
|
+
delete requestOptions.body;
|
|
2262
|
+
}
|
|
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);
|
|
2266
|
+
let result, timeoutId, response;
|
|
2024
2267
|
try {
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
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
|
+
}
|
|
2278
|
+
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) });
|
|
2280
|
+
}
|
|
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);
|
|
2283
|
+
} finally {
|
|
2284
|
+
clearTimeout(timeoutId);
|
|
2285
|
+
if (!requestOptions.signal.aborted) {
|
|
2286
|
+
__privateMethod(this, _publish, publish_fn).call(this, _Transportr.Events.COMPLETE, response);
|
|
2287
|
+
}
|
|
2029
2288
|
}
|
|
2030
|
-
|
|
2031
|
-
|
|
2289
|
+
return result;
|
|
2290
|
+
};
|
|
2291
|
+
_throwHttpError = new WeakSet();
|
|
2292
|
+
throwHttpError_fn = function(url, options) {
|
|
2293
|
+
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;
|
|
2296
|
+
};
|
|
2297
|
+
_publish = new WeakSet();
|
|
2298
|
+
publish_fn = function(eventName, event, data) {
|
|
2299
|
+
__privateGet(_Transportr, _globalSubscribr).publish(eventName, event, data);
|
|
2300
|
+
__privateGet(this, _subscribr).publish(eventName, event, data);
|
|
2301
|
+
};
|
|
2302
|
+
_generateResponseStatusFromError = new WeakSet();
|
|
2303
|
+
generateResponseStatusFromError_fn = function(errorName, response) {
|
|
2304
|
+
switch (errorName) {
|
|
2305
|
+
case "AbortError":
|
|
2306
|
+
return __privateGet(_Transportr, _eventResponseStatuses).get(_Transportr.Events.ABORTED);
|
|
2307
|
+
case "TimeoutError":
|
|
2308
|
+
return __privateGet(_Transportr, _eventResponseStatuses).get(_Transportr.Events.TIMEOUT);
|
|
2309
|
+
default:
|
|
2310
|
+
return response ? new ResponseStatus(response.status, response.statusText) : new ResponseStatus(500, "Internal Server Error");
|
|
2032
2311
|
}
|
|
2033
|
-
return await __privateMethod(_e = _Transportr, _processResponse, processResponse_fn).call(_e, response, responseHandler);
|
|
2034
2312
|
};
|
|
2035
2313
|
_processResponse = new WeakSet();
|
|
2036
2314
|
processResponse_fn = async function(response, handler) {
|
|
@@ -2054,9 +2332,9 @@ var Transportr = (() => {
|
|
|
2054
2332
|
}
|
|
2055
2333
|
};
|
|
2056
2334
|
_createUrl = new WeakSet();
|
|
2057
|
-
createUrl_fn = function(url, path, searchParams =
|
|
2335
|
+
createUrl_fn = function(url, path, searchParams = {}) {
|
|
2058
2336
|
url = path.startsWith("/") ? new URL(`${url.pathname.replace(endsWithSlashRegEx, "")}${path}`, url.origin) : new URL(path);
|
|
2059
|
-
searchParams.forEach((
|
|
2337
|
+
Object.entries(searchParams).forEach(([key, value]) => url.searchParams.append(key, value));
|
|
2060
2338
|
return url;
|
|
2061
2339
|
};
|
|
2062
2340
|
_needsSerialization = new WeakSet();
|
|
@@ -2064,16 +2342,26 @@ var Transportr = (() => {
|
|
|
2064
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);
|
|
2065
2343
|
};
|
|
2066
2344
|
_convertRequestOptions = new WeakSet();
|
|
2067
|
-
convertRequestOptions_fn = function(options
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2345
|
+
convertRequestOptions_fn = function(options) {
|
|
2346
|
+
if (!object_is_empty_default(options)) {
|
|
2347
|
+
for (const [{ property, type, converter }, option = options[property]] of __privateGet(_Transportr, _propertyTypeConverters)) {
|
|
2348
|
+
if (option instanceof type) {
|
|
2349
|
+
options[property] = converter(option);
|
|
2350
|
+
}
|
|
2073
2351
|
}
|
|
2074
2352
|
}
|
|
2075
2353
|
return options;
|
|
2076
2354
|
};
|
|
2355
|
+
/**
|
|
2356
|
+
* Generates a ResponseStatus object based on the error name and the response.
|
|
2357
|
+
*
|
|
2358
|
+
* @private
|
|
2359
|
+
* @static
|
|
2360
|
+
* @param {string} errorName The name of the error.
|
|
2361
|
+
* @param {Response} response The response object returned by the fetch API.
|
|
2362
|
+
* @returns {ResponseStatus} The response status object.
|
|
2363
|
+
*/
|
|
2364
|
+
__privateAdd(Transportr, _generateResponseStatusFromError);
|
|
2077
2365
|
/**
|
|
2078
2366
|
* It takes a response and a handler, and if the handler is not defined, it tries to find a handler
|
|
2079
2367
|
* based on the response's content type
|
|
@@ -2094,7 +2382,7 @@ var Transportr = (() => {
|
|
|
2094
2382
|
* @static
|
|
2095
2383
|
* @param {URL} url - The URL to use as a base.
|
|
2096
2384
|
* @param {string} path - The path to the resource. This can be a relative path or a full URL.
|
|
2097
|
-
* @param {
|
|
2385
|
+
* @param {Object<string, string>} [searchParams={}] - An object containing the query parameters to be added to the URL.
|
|
2098
2386
|
* @returns {URL} A new URL object with the pathname and origin of the url parameter, and the path parameter
|
|
2099
2387
|
* appended to the end of the pathname.
|
|
2100
2388
|
*/
|
|
@@ -2113,10 +2401,11 @@ var Transportr = (() => {
|
|
|
2113
2401
|
/**
|
|
2114
2402
|
*
|
|
2115
2403
|
* @param {RequestOptions} options - The options passed to the public function to use for the request.
|
|
2116
|
-
* @param {Object<string, Type>} typeConversionMap - A map of properties to convert to the specified type.
|
|
2117
2404
|
* @returns {RequestOptions} The options to use for the request.
|
|
2118
2405
|
*/
|
|
2119
2406
|
__privateAdd(Transportr, _convertRequestOptions);
|
|
2407
|
+
/** @type {Subscribr} */
|
|
2408
|
+
__privateAdd(Transportr, _globalSubscribr, new Subscribr());
|
|
2120
2409
|
/**
|
|
2121
2410
|
* @private
|
|
2122
2411
|
* @static
|
|
@@ -2132,6 +2421,16 @@ var Transportr = (() => {
|
|
|
2132
2421
|
[_handleXml, new MediaType(http_media_type_default.XML).subtype],
|
|
2133
2422
|
[_handleReadableStream, new MediaType(http_media_type_default.BIN).subtype]
|
|
2134
2423
|
]));
|
|
2424
|
+
/**
|
|
2425
|
+
* @private
|
|
2426
|
+
* @static
|
|
2427
|
+
* @type {Set<PropertyTypeConverter>}
|
|
2428
|
+
*/
|
|
2429
|
+
__privateAdd(Transportr, _propertyTypeConverters, /* @__PURE__ */ new Set([
|
|
2430
|
+
[{ property: "body", type: FormData, converter: _typeConverter }],
|
|
2431
|
+
[{ property: "searchParams", type: URLSearchParams, converter: _typeConverter }],
|
|
2432
|
+
[{ property: "headers", type: Headers, converter: Object.fromEntries }]
|
|
2433
|
+
]));
|
|
2135
2434
|
/**
|
|
2136
2435
|
* @static
|
|
2137
2436
|
* @constant {Object<string, HttpRequestMethod>}
|
|
@@ -2207,6 +2506,18 @@ var Transportr = (() => {
|
|
|
2207
2506
|
STRICT_ORIGIN_WHEN_CROSS_ORIGIN: "strict-origin-when-cross-origin",
|
|
2208
2507
|
UNSAFE_URL: "unsafe-url"
|
|
2209
2508
|
}));
|
|
2509
|
+
/**
|
|
2510
|
+
* @static
|
|
2511
|
+
* @constant {Object<string, TransportrEvent>}
|
|
2512
|
+
*/
|
|
2513
|
+
__publicField(Transportr, "Events", Object.freeze({
|
|
2514
|
+
CONFIGURED: "configured",
|
|
2515
|
+
SUCCESS: "success",
|
|
2516
|
+
ERROR: "error",
|
|
2517
|
+
ABORTED: "aborted",
|
|
2518
|
+
TIMEOUT: "timeout",
|
|
2519
|
+
COMPLETE: "complete"
|
|
2520
|
+
}));
|
|
2210
2521
|
/**
|
|
2211
2522
|
* @private
|
|
2212
2523
|
* @static
|
|
@@ -2225,9 +2536,19 @@ var Transportr = (() => {
|
|
|
2225
2536
|
redirect: _Transportr.RedirectPolicy.FOLLOW,
|
|
2226
2537
|
referrer: "about:client",
|
|
2227
2538
|
referrerPolicy: _Transportr.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
|
|
2228
|
-
signal:
|
|
2539
|
+
signal: void 0,
|
|
2540
|
+
timeout: 1e4,
|
|
2229
2541
|
window: null
|
|
2230
2542
|
}));
|
|
2543
|
+
/**
|
|
2544
|
+
* @private
|
|
2545
|
+
* @static
|
|
2546
|
+
* @type {Map<TransportrEvent, ResponseStatus>}
|
|
2547
|
+
*/
|
|
2548
|
+
__privateAdd(Transportr, _eventResponseStatuses, /* @__PURE__ */ new Map([
|
|
2549
|
+
[_Transportr.Events.ABORTED, new ResponseStatus(499, "Aborted")],
|
|
2550
|
+
[_Transportr.Events.TIMEOUT, new ResponseStatus(504, "Gateway Timeout")]
|
|
2551
|
+
]));
|
|
2231
2552
|
return __toCommonJS(transportr_exports);
|
|
2232
2553
|
})();
|
|
2233
2554
|
window.Transportr = Transportr.default;
|