@hybridly/core 0.10.0-beta.14 → 0.10.0-beta.15

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 (3) hide show
  1. package/dist/index.d.mts +269 -140
  2. package/dist/index.mjs +1014 -559
  3. package/package.json +49 -51
package/dist/index.mjs CHANGED
@@ -1,59 +1,355 @@
1
1
  import { t as __exportAll } from "./_chunks/chunk.mjs";
2
- import { debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, showResponseErrorModal, when } from "@hybridly/utils";
3
- import axios from "axios";
4
- import { parse, stringify } from "superjson";
5
- import qs from "qs";
6
- //#region src/constants.ts
7
- var constants_exports = /* @__PURE__ */ __exportAll({
8
- DIALOG_KEY_HEADER: () => DIALOG_KEY_HEADER,
9
- DIALOG_REDIRECT_HEADER: () => DIALOG_REDIRECT_HEADER,
10
- ERROR_BAG_HEADER: () => ERROR_BAG_HEADER,
11
- EXCEPT_DATA_HEADER: () => EXCEPT_DATA_HEADER,
12
- EXTERNAL_NAVIGATION_HEADER: () => EXTERNAL_NAVIGATION_HEADER,
13
- EXTERNAL_NAVIGATION_TARGET_HEADER: () => EXTERNAL_NAVIGATION_TARGET_HEADER,
14
- HYBRIDLY_HEADER: () => HYBRIDLY_HEADER,
15
- ONLY_DATA_HEADER: () => ONLY_DATA_HEADER,
16
- PARTIAL_COMPONENT_HEADER: () => PARTIAL_COMPONENT_HEADER,
17
- SCROLL_REGION_ATTRIBUTE: () => SCROLL_REGION_ATTRIBUTE,
18
- STORAGE_EXTERNAL_KEY: () => STORAGE_EXTERNAL_KEY,
19
- VERSION_HEADER: () => VERSION_HEADER
20
- });
21
- const STORAGE_EXTERNAL_KEY = "hybridly:external";
22
- const HYBRIDLY_HEADER = "x-hybrid";
23
- const EXTERNAL_NAVIGATION_HEADER = `${HYBRIDLY_HEADER}-external`;
24
- const EXTERNAL_NAVIGATION_TARGET_HEADER = `${HYBRIDLY_HEADER}-external-target`;
25
- const PARTIAL_COMPONENT_HEADER = `${HYBRIDLY_HEADER}-partial-component`;
26
- const ONLY_DATA_HEADER = `${HYBRIDLY_HEADER}-only-data`;
27
- const DIALOG_KEY_HEADER = `${HYBRIDLY_HEADER}-dialog-key`;
28
- const DIALOG_REDIRECT_HEADER = `${HYBRIDLY_HEADER}-dialog-redirect`;
29
- const EXCEPT_DATA_HEADER = `${HYBRIDLY_HEADER}-except-data`;
30
- const VERSION_HEADER = `${HYBRIDLY_HEADER}-version`;
31
- const ERROR_BAG_HEADER = `${HYBRIDLY_HEADER}-error-bag`;
32
- const SCROLL_REGION_ATTRIBUTE = "scroll-region";
2
+ import { debug, getByPath, hasFiles, merge, mergeObject, objectToFormData, random, showResponseErrorModal, wrap } from "@hybridly/utils";
3
+ import { trimEnd } from "es-toolkit/string";
4
+ import { parse, stringify } from "picoquery";
5
+ import { debounce } from "es-toolkit/function";
6
+ import { parse as parse$1, stringify as stringify$1 } from "superjson";
7
+ import { get, set, uniqBy } from "es-toolkit/compat";
8
+ //#region src/query.ts
9
+ function parseQueryString(query) {
10
+ const source = query.startsWith("?") ? query.slice(1) : query;
11
+ if (!source) return {};
12
+ return parse(source, {
13
+ nesting: true,
14
+ nestingSyntax: "index",
15
+ arrayRepeat: true,
16
+ arrayRepeatSyntax: "bracket"
17
+ });
18
+ }
19
+ function stringifyQueryString(value, options = {}) {
20
+ const normalizedQuery = unescapeBracketSyntaxInKeys(stringify(normalizeQueryValue(value), {
21
+ nesting: true,
22
+ nestingSyntax: "index",
23
+ arrayRepeat: (options.arrayFormat ?? "brackets") === "brackets",
24
+ arrayRepeatSyntax: "bracket"
25
+ }));
26
+ if (!normalizedQuery) return "";
27
+ return options.addQueryPrefix ? `?${normalizedQuery}` : normalizedQuery;
28
+ }
29
+ function unescapeBracketSyntaxInKeys(query) {
30
+ if (!query) return query;
31
+ return query.split("&").map((entry) => {
32
+ const separator = entry.indexOf("=");
33
+ if (separator < 0) return decodeBracketSyntax(entry);
34
+ const key = entry.slice(0, separator);
35
+ const value = entry.slice(separator);
36
+ return `${decodeBracketSyntax(key)}${value}`;
37
+ }).join("&");
38
+ }
39
+ function decodeBracketSyntax(value) {
40
+ return value.replace(/%5B/gi, "[").replace(/%5D/gi, "]");
41
+ }
42
+ function normalizeQueryValue(value) {
43
+ if (value instanceof Set) return [...value].map((entry) => normalizeQueryValue(entry));
44
+ if (Array.isArray(value)) return value.map((entry) => normalizeQueryValue(entry));
45
+ if (isPlainObject(value)) return Object.entries(value).reduce((result, [key, entry]) => ({
46
+ ...result,
47
+ [key]: normalizeQueryValue(entry)
48
+ }), {});
49
+ return value;
50
+ }
51
+ function isPlainObject(value) {
52
+ if (typeof value !== "object" || value === null) return false;
53
+ const prototype = Object.getPrototypeOf(value);
54
+ return prototype === null || prototype === Object.prototype;
55
+ }
33
56
  //#endregion
34
- //#region src/errors.ts
35
- var NotAHybridResponseError = class extends Error {
36
- constructor(response) {
37
- super();
38
- this.response = response;
57
+ //#region src/url.ts
58
+ /** Normalizes the given input to an URL. */
59
+ function normalizeUrl(href, trailingSlash) {
60
+ return makeUrl(href, { trailingSlash }).toString();
61
+ }
62
+ /**
63
+ * Converts an input to an URL, optionally changing its properties after initialization.
64
+ */
65
+ function makeUrl(href, transformations = {}) {
66
+ try {
67
+ const base = document?.location?.href === "//" ? void 0 : document.location.href;
68
+ const url = new URL(String(href), base);
69
+ transformations = typeof transformations === "function" ? transformations(url) ?? {} : transformations ?? {};
70
+ Object.entries(transformations).forEach(([key, value]) => {
71
+ if (key === "query") {
72
+ const currentQueryParameters = merge(parseQueryString(url.search), value, { mergePlainObjects: true });
73
+ key = "search";
74
+ value = stringifyQueryString(currentQueryParameters, { arrayFormat: "brackets" });
75
+ }
76
+ Reflect.set(url, key, value);
77
+ });
78
+ if (transformations.trailingSlash === false) {
79
+ const _url = trimEnd(url.toString().replace(/\/\?/, "?"), "/");
80
+ url.toString = () => _url;
81
+ }
82
+ return url;
83
+ } catch (error) {
84
+ throw new TypeError(`${href} is not resolvable to a valid URL.`);
39
85
  }
40
- };
41
- var NavigationCancelledError = class extends Error {};
42
- var RoutingNotInitialized = class extends Error {
43
- constructor() {
44
- super("Routing is not initialized. Make sure the Vite plugin is enabled and that `php artisan route:list` returns no error.");
86
+ }
87
+ /**
88
+ * Checks if the given URLs have the same origin and path.
89
+ */
90
+ function sameUrls(...hrefs) {
91
+ if (hrefs.length < 2) return true;
92
+ try {
93
+ return hrefs.every((href) => {
94
+ return makeUrl(href, { hash: "" }).toJSON() === makeUrl(hrefs.at(0), { hash: "" }).toJSON();
95
+ });
96
+ } catch {}
97
+ return false;
98
+ }
99
+ /**
100
+ * Checks if the given URLs have the same origin, path, and hash.
101
+ */
102
+ function sameHashes(...hrefs) {
103
+ if (hrefs.length < 2) return true;
104
+ try {
105
+ return hrefs.every((href) => {
106
+ return makeUrl(href).toJSON() === makeUrl(hrefs.at(0)).toJSON();
107
+ });
108
+ } catch {}
109
+ return false;
110
+ }
111
+ /**
112
+ * If the back-end did not specify a hash, if the navigation specified one,
113
+ * and both URLs lead to the same endpoint, we update the target URL
114
+ * to use the hash of the initially-requested URL.
115
+ */
116
+ function fillHash(currentUrl, targetUrl) {
117
+ currentUrl = makeUrl(currentUrl);
118
+ targetUrl = makeUrl(targetUrl);
119
+ if (currentUrl.hash && !targetUrl.hash && sameUrls(targetUrl, currentUrl)) targetUrl.hash = currentUrl.hash;
120
+ return targetUrl.toString();
121
+ }
122
+ //#endregion
123
+ //#region src/http/client.ts
124
+ var HttpError = class extends Error {
125
+ constructor(message, options) {
126
+ super(message);
127
+ this.isHttpError = true;
128
+ this.name = "HttpError";
129
+ this.config = options.config;
130
+ this.request = options.request;
131
+ this.response = options.response;
132
+ this.code = options.code ?? "ERR_NETWORK";
133
+ this.kind = options.kind ?? "network";
134
+ this.reason = options.reason;
45
135
  }
46
136
  };
47
- var RouteNotFound = class extends Error {
48
- constructor(name) {
49
- super(`Route [${name}] does not exist.`);
137
+ var HttpAbortError = class extends HttpError {
138
+ constructor(options) {
139
+ super("The request was aborted.", {
140
+ ...options,
141
+ code: options.code ?? "ERR_ABORTED",
142
+ kind: options.kind ?? "abort"
143
+ });
144
+ this.name = "AbortError";
50
145
  }
51
146
  };
52
- var MissingRouteParameter = class extends Error {
53
- constructor(parameter, routeName) {
54
- super(`Parameter [${parameter}] is required for route [${routeName}].`);
147
+ function isHttpError(error) {
148
+ return error instanceof Error && error.isHttpError === true;
149
+ }
150
+ function isHttpAbortError(error) {
151
+ return isHttpError(error) && error.kind === "abort";
152
+ }
153
+ function createXhrHttpClient() {
154
+ return { request: async (config) => await requestWithXhr(config) };
155
+ }
156
+ function requestWithXhr(config) {
157
+ return new Promise((resolve, reject) => {
158
+ let settled = false;
159
+ const resolveOnce = (response) => {
160
+ if (settled) return;
161
+ settled = true;
162
+ cleanupAbort?.();
163
+ resolve(response);
164
+ };
165
+ const rejectOnce = (error) => {
166
+ if (settled) return;
167
+ settled = true;
168
+ cleanupAbort?.();
169
+ reject(error);
170
+ };
171
+ const xhr = new XMLHttpRequest();
172
+ const method = config.method ?? "GET";
173
+ const url = buildUrl(config.url, config.params);
174
+ const headers = normalizeHeaders(config.headers);
175
+ const body = toRequestBody(config.data, headers);
176
+ const cleanupAbort = registerAbortSignal(config.signal, xhr, (reason) => {
177
+ rejectOnce(new HttpAbortError({
178
+ config,
179
+ request: xhr,
180
+ reason
181
+ }));
182
+ });
183
+ if (config.signal?.aborted) {
184
+ rejectOnce(new HttpAbortError({
185
+ config,
186
+ request: xhr,
187
+ reason: config.signal.reason
188
+ }));
189
+ return;
190
+ }
191
+ xhr.open(method, url, true);
192
+ xhr.responseType = "arraybuffer";
193
+ for (const [key, value] of Object.entries(headers)) xhr.setRequestHeader(key, value);
194
+ if (config.onUploadProgress) xhr.upload.onprogress = (event) => {
195
+ const total = event.lengthComputable ? event.total : void 0;
196
+ const percentage = total && total > 0 ? Math.round(event.loaded / total * 100) : 0;
197
+ config.onUploadProgress?.({
198
+ loaded: event.loaded,
199
+ total,
200
+ lengthComputable: event.lengthComputable,
201
+ percentage
202
+ });
203
+ };
204
+ xhr.onload = () => {
205
+ resolveOnce(toResponse(xhr, config));
206
+ };
207
+ xhr.onerror = () => {
208
+ rejectOnce(new HttpError("Network Error", {
209
+ config,
210
+ request: xhr,
211
+ code: "ERR_NETWORK",
212
+ kind: "network"
213
+ }));
214
+ };
215
+ xhr.onabort = () => {
216
+ rejectOnce(new HttpAbortError({
217
+ config,
218
+ request: xhr,
219
+ reason: config.signal?.reason
220
+ }));
221
+ };
222
+ xhr.ontimeout = () => {
223
+ rejectOnce(new HttpError("The request timed out.", {
224
+ config,
225
+ request: xhr,
226
+ code: "ECONNABORTED",
227
+ kind: "timeout"
228
+ }));
229
+ };
230
+ try {
231
+ xhr.send(body);
232
+ } catch (error) {
233
+ rejectOnce(new HttpError("Network Error", {
234
+ config,
235
+ request: xhr,
236
+ code: "ERR_NETWORK",
237
+ kind: "network",
238
+ reason: error
239
+ }));
240
+ }
241
+ });
242
+ }
243
+ function registerAbortSignal(signal, xhr, onAbort) {
244
+ if (!signal) return;
245
+ if (signal.aborted) {
246
+ onAbort(signal.reason);
247
+ return;
55
248
  }
56
- };
249
+ const abort = () => {
250
+ onAbort(signal.reason);
251
+ xhr.abort();
252
+ };
253
+ signal.addEventListener("abort", abort, { once: true });
254
+ return () => signal.removeEventListener("abort", abort);
255
+ }
256
+ function buildUrl(url, params) {
257
+ if (!params || Object.keys(params).length === 0) return url;
258
+ return makeUrl(url, { query: params }).toString();
259
+ }
260
+ function normalizeHeaders(headers) {
261
+ if (!headers) return {};
262
+ return Object.entries(headers).reduce((result, [key, value]) => ({
263
+ ...result,
264
+ [key]: String(value)
265
+ }), {});
266
+ }
267
+ function toRequestBody(data, headers) {
268
+ if (data === void 0 || data === null) return null;
269
+ if (typeof FormData !== "undefined" && data instanceof FormData) return data;
270
+ if (typeof data === "string" || data instanceof Blob || data instanceof URLSearchParams || data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return data;
271
+ if (typeof data === "object") {
272
+ if (!hasHeader(headers, "content-type")) headers["Content-Type"] = "application/json";
273
+ return JSON.stringify(data);
274
+ }
275
+ return String(data);
276
+ }
277
+ function hasHeader(headers, headerName) {
278
+ const expected = headerName.toLowerCase();
279
+ return Object.keys(headers).some((header) => header.toLowerCase() === expected);
280
+ }
281
+ function toResponse(xhr, config) {
282
+ const headers = createHttpHeaders(parseResponseHeaders(xhr.getAllResponseHeaders()));
283
+ const rawData = toArrayBuffer(xhr.response);
284
+ return {
285
+ data: decodeResponseBody(rawData, headers),
286
+ rawData,
287
+ status: xhr.status,
288
+ statusText: xhr.statusText,
289
+ headers,
290
+ request: xhr,
291
+ config,
292
+ toBlob: (type) => {
293
+ const resolvedType = type ?? headers.get("content-type");
294
+ return new Blob([rawData], { ...resolvedType ? { type: resolvedType } : {} });
295
+ }
296
+ };
297
+ }
298
+ function createHttpHeaders(values) {
299
+ const all = Object.freeze({ ...values });
300
+ const get = (name) => all[name.toLowerCase()];
301
+ const has = (name) => get(name) !== void 0;
302
+ const isContentType = (matcher) => {
303
+ const contentType = get("content-type");
304
+ if (!contentType) return false;
305
+ if (typeof matcher === "string") return contentType.toLowerCase().includes(matcher.toLowerCase());
306
+ return matcher.test(contentType);
307
+ };
308
+ return {
309
+ all,
310
+ get,
311
+ has,
312
+ isContentType
313
+ };
314
+ }
315
+ function toArrayBuffer(data) {
316
+ if (data instanceof ArrayBuffer) return data;
317
+ if (ArrayBuffer.isView(data)) return new Uint8Array(data.buffer, data.byteOffset, data.byteLength).slice().buffer;
318
+ if (typeof data === "string") return new TextEncoder().encode(data).buffer;
319
+ return /* @__PURE__ */ new ArrayBuffer(0);
320
+ }
321
+ function decodeResponseBody(rawData, headers) {
322
+ if (headers.isContentType(/(^|\b|\+)json(\b|;|$)/i)) {
323
+ const text = decodeText(rawData, headers);
324
+ try {
325
+ return JSON.parse(text);
326
+ } catch {
327
+ return text;
328
+ }
329
+ }
330
+ if (headers.isContentType(/^text\//i) || headers.isContentType(/xml|javascript|x-www-form-urlencoded/i)) return decodeText(rawData, headers);
331
+ return rawData;
332
+ }
333
+ function decodeText(rawData, headers) {
334
+ const charset = headers.get("content-type")?.match(/charset=([^;]+)/i)?.[1]?.trim();
335
+ try {
336
+ return new TextDecoder(charset).decode(rawData);
337
+ } catch {
338
+ return new TextDecoder().decode(rawData);
339
+ }
340
+ }
341
+ function parseResponseHeaders(rawHeaders) {
342
+ const lines = rawHeaders.split("\r\n").filter(Boolean);
343
+ const headers = {};
344
+ for (const line of lines) {
345
+ const separator = line.indexOf(":");
346
+ if (separator < 0) continue;
347
+ const name = line.slice(0, separator).trim().toLowerCase();
348
+ const value = line.slice(separator + 1).trim();
349
+ headers[name] = name in headers ? `${headers[name]}, ${value}` : value;
350
+ }
351
+ return headers;
352
+ }
57
353
  //#endregion
58
354
  //#region src/plugins/plugin.ts
59
355
  function definePlugin(plugin) {
@@ -119,6 +415,36 @@ function registerHook(hook, fn, options) {
119
415
  return appendCallbackToHooks(hook, fn);
120
416
  }
121
417
  //#endregion
418
+ //#region src/constants.ts
419
+ var constants_exports = /* @__PURE__ */ __exportAll({
420
+ DIALOG_KEY_HEADER: () => DIALOG_KEY_HEADER,
421
+ DIALOG_REDIRECT_HEADER: () => DIALOG_REDIRECT_HEADER,
422
+ ERROR_BAG_HEADER: () => ERROR_BAG_HEADER,
423
+ EXCEPT_DATA_HEADER: () => EXCEPT_DATA_HEADER,
424
+ EXTERNAL_NAVIGATION_HEADER: () => EXTERNAL_NAVIGATION_HEADER,
425
+ EXTERNAL_NAVIGATION_TARGET_HEADER: () => EXTERNAL_NAVIGATION_TARGET_HEADER,
426
+ HYBRIDLY_HEADER: () => HYBRIDLY_HEADER,
427
+ ONLY_DATA_HEADER: () => ONLY_DATA_HEADER,
428
+ PARTIAL_COMPONENT_HEADER: () => PARTIAL_COMPONENT_HEADER,
429
+ RESET_HEADER: () => RESET_HEADER,
430
+ SCROLL_REGION_ATTRIBUTE: () => SCROLL_REGION_ATTRIBUTE,
431
+ STORAGE_EXTERNAL_KEY: () => STORAGE_EXTERNAL_KEY,
432
+ VERSION_HEADER: () => VERSION_HEADER
433
+ });
434
+ const STORAGE_EXTERNAL_KEY = "hybridly:external";
435
+ const HYBRIDLY_HEADER = "x-hybrid";
436
+ const EXTERNAL_NAVIGATION_HEADER = `${HYBRIDLY_HEADER}-external`;
437
+ const EXTERNAL_NAVIGATION_TARGET_HEADER = `${HYBRIDLY_HEADER}-external-target`;
438
+ const PARTIAL_COMPONENT_HEADER = `${HYBRIDLY_HEADER}-partial-component`;
439
+ const RESET_HEADER = `${HYBRIDLY_HEADER}-reset`;
440
+ const ONLY_DATA_HEADER = `${HYBRIDLY_HEADER}-only-data`;
441
+ const DIALOG_KEY_HEADER = `${HYBRIDLY_HEADER}-dialog-key`;
442
+ const DIALOG_REDIRECT_HEADER = `${HYBRIDLY_HEADER}-dialog-redirect`;
443
+ const EXCEPT_DATA_HEADER = `${HYBRIDLY_HEADER}-except-data`;
444
+ const VERSION_HEADER = `${HYBRIDLY_HEADER}-version`;
445
+ const ERROR_BAG_HEADER = `${HYBRIDLY_HEADER}-error-bag`;
446
+ const SCROLL_REGION_ATTRIBUTE = "scroll-region";
447
+ //#endregion
122
448
  //#region src/scroll.ts
123
449
  /** Saves the current view's scrollbar positions into the history state. */
124
450
  function saveScrollPositions() {
@@ -158,6 +484,7 @@ function resetScrollPositions() {
158
484
  }
159
485
  /** Restores the scroll positions stored in the context. */
160
486
  async function restoreScrollPositions() {
487
+ debug.scroll("Restoring scroll positions.");
161
488
  const context = getRouterContext();
162
489
  const regions = getScrollRegions();
163
490
  if (!context.scrollRegions) {
@@ -173,74 +500,651 @@ async function restoreScrollPositions() {
173
500
  });
174
501
  }
175
502
  //#endregion
176
- //#region src/url.ts
177
- /** Normalizes the given input to an URL. */
178
- function normalizeUrl(href, trailingSlash) {
179
- return makeUrl(href, { trailingSlash }).toString();
503
+ //#region src/errors.ts
504
+ var HybridlyError = class extends Error {
505
+ constructor(message, options) {
506
+ super(message);
507
+ this.isHybridlyError = true;
508
+ this.name = "HybridlyError";
509
+ this.kind = options.kind;
510
+ this.code = options.code;
511
+ this.details = options.details;
512
+ this.cause = options.cause;
513
+ }
514
+ };
515
+ var InvalidResponseError = class extends HybridlyError {
516
+ constructor(options = {}) {
517
+ super("The response was not a valid hybrid response.", {
518
+ ...options,
519
+ kind: "response.invalid",
520
+ code: "HYB_INVALID_RESPONSE"
521
+ });
522
+ this.name = "InvalidResponseError";
523
+ }
524
+ };
525
+ var NavigationCancelledError = class extends HybridlyError {
526
+ constructor(message = "The navigation was cancelled.", options = {}) {
527
+ super(message, {
528
+ ...options,
529
+ kind: "navigation.cancelled",
530
+ code: "HYB_NAVIGATION_CANCELLED"
531
+ });
532
+ this.name = "NavigationCancelledError";
533
+ }
534
+ };
535
+ var RoutingNotInitialized = class extends HybridlyError {
536
+ constructor(options = {}) {
537
+ super("Routing is not initialized. Make sure the Vite plugin is enabled and that `php artisan route:list` returns no error.", {
538
+ ...options,
539
+ kind: "routing.not-initialized",
540
+ code: "HYB_ROUTING_NOT_INITIALIZED"
541
+ });
542
+ this.name = "RoutingNotInitialized";
543
+ }
544
+ };
545
+ var RouteNotFound = class extends HybridlyError {
546
+ constructor(name, options = {}) {
547
+ super(`Route [${name}] does not exist.`, {
548
+ ...options,
549
+ kind: "routing.route-not-found",
550
+ code: "HYB_ROUTE_NOT_FOUND",
551
+ details: { name }
552
+ });
553
+ this.name = "RouteNotFound";
554
+ }
555
+ };
556
+ var MissingRouteParameter = class extends HybridlyError {
557
+ constructor(parameter, routeName, options = {}) {
558
+ super(`Parameter [${parameter}] is required for route [${routeName}].`, {
559
+ ...options,
560
+ kind: "routing.missing-parameter",
561
+ code: "HYB_MISSING_ROUTE_PARAMETER",
562
+ details: {
563
+ parameter,
564
+ routeName
565
+ }
566
+ });
567
+ this.name = "MissingRouteParameter";
568
+ }
569
+ };
570
+ function isHybridlyError(error) {
571
+ return error instanceof Error && error.isHybridlyError === true;
572
+ }
573
+ function isNavigationCancelledError(error) {
574
+ return isHybridlyError(error) && error.kind === "navigation.cancelled";
575
+ }
576
+ //#endregion
577
+ //#region src/download.ts
578
+ /** Checks if the response wants to redirect to an external URL. */
579
+ function isDownloadResponse(response) {
580
+ return response.status === 200 && response.headers.has("content-disposition");
581
+ }
582
+ /** Handles a download. */
583
+ async function handleDownloadResponse(response) {
584
+ const blob = response.toBlob();
585
+ const urlObject = window.webkitURL || window.URL;
586
+ const link = document.createElement("a");
587
+ link.style.display = "none";
588
+ link.href = urlObject.createObjectURL(blob);
589
+ link.download = getFileNameFromContentDispositionHeader(response.headers.get("content-disposition") ?? "");
590
+ link.click();
591
+ setTimeout(() => {
592
+ urlObject.revokeObjectURL(link.href);
593
+ link.remove();
594
+ }, 0);
595
+ }
596
+ function getFileNameFromContentDispositionHeader(header) {
597
+ return (header.split(";")[1]?.trim().split("=")[1])?.replace(/^"(.*)"$/, "$1") ?? "";
598
+ }
599
+ //#endregion
600
+ //#region src/utils.ts
601
+ function createPromiseWithResolvers() {
602
+ let resolve;
603
+ let reject;
604
+ return {
605
+ promise: new Promise((_resolve, _reject) => {
606
+ resolve = _resolve;
607
+ reject = _reject;
608
+ }),
609
+ resolve,
610
+ reject
611
+ };
612
+ }
613
+ //#endregion
614
+ //#region src/router/request/request.ts
615
+ function createPendingHybridRequest(options) {
616
+ const context = getRouterContext();
617
+ const url = makeUrl(options.url ?? context.url, options.transformUrl);
618
+ const { promise, resolve } = createPromiseWithResolvers();
619
+ return {
620
+ url,
621
+ options,
622
+ promise,
623
+ resolve,
624
+ id: random(),
625
+ controller: options.abortController ?? new AbortController(),
626
+ cancelled: false,
627
+ completed: false,
628
+ interrupted: false,
629
+ view: context.view
630
+ };
631
+ }
632
+ async function sendHybridRequest(request) {
633
+ const context = getInternalRouterContext();
634
+ return await context.http.request({
635
+ url: request.url.toString(),
636
+ method: request.options.method,
637
+ data: request.options.method === "GET" ? {} : request.options.data,
638
+ params: request.options.method === "GET" ? request.options.data : {},
639
+ signal: request.controller?.signal,
640
+ headers: {
641
+ ...request.options.headers,
642
+ ...context.dialog ? { [DIALOG_KEY_HEADER]: context.dialog.key } : {},
643
+ ...context.dialog ? { [DIALOG_REDIRECT_HEADER]: context.dialog.redirectUrl ?? "" } : {},
644
+ ...mergeObject(request.options.only || request.options.except || request.options.reset, {
645
+ [PARTIAL_COMPONENT_HEADER]: context.view.component,
646
+ ...mergeObject(request.options.reset, { [RESET_HEADER]: JSON.stringify(wrap(request.options.reset)) }),
647
+ ...mergeObject(request.options.only, { [ONLY_DATA_HEADER]: JSON.stringify(wrap(request.options.only)) }),
648
+ ...mergeObject(request.options.except, { [EXCEPT_DATA_HEADER]: JSON.stringify(wrap(request.options.except)) })
649
+ }),
650
+ ...mergeObject(request.options.errorBag, { [ERROR_BAG_HEADER]: request.options.errorBag }),
651
+ ...mergeObject(context.version, { [VERSION_HEADER]: context.version }),
652
+ [HYBRIDLY_HEADER]: "true",
653
+ "X-Requested-With": "XMLHttpRequest",
654
+ "Accept": "text/html, application/xhtml+xml"
655
+ },
656
+ onUploadProgress: async (progress) => {
657
+ await runHooks("progress", request.options.hooks, progress, request, context);
658
+ }
659
+ });
660
+ }
661
+ async function performHybridRequest(request) {
662
+ enqueueRequest(request);
663
+ return request.promise;
664
+ }
665
+ /** Performs every action necessary to make a hybrid navigation. */
666
+ async function performHybridNavigation(options) {
667
+ const context = getRouterContext();
668
+ debug.router("Making a hybrid navigation:", {
669
+ context,
670
+ options
671
+ });
672
+ await transformOptions(options);
673
+ const request = createPendingHybridRequest(options);
674
+ if (!await runHooks("before", options.hooks, request, context)) {
675
+ debug.router("\"before\" event returned false, aborting the navigation.");
676
+ return { error: new NavigationCancelledError("The navigation was cancelled by the \"before\" event.") };
677
+ }
678
+ await runHooks("start", options.hooks, request, context);
679
+ debug.router("Making request with the configured HTTP client.");
680
+ return await performHybridRequest(request);
180
681
  }
181
682
  /**
182
- * Converts an input to an URL, optionally changing its properties after initialization.
683
+ * Transform the options object with convenience changes.
183
684
  */
184
- function makeUrl(href, transformations = {}) {
185
- try {
186
- const base = document?.location?.href === "//" ? void 0 : document.location.href;
187
- const url = new URL(String(href), base);
188
- transformations = typeof transformations === "function" ? transformations(url) ?? {} : transformations ?? {};
189
- Object.entries(transformations).forEach(([key, value]) => {
190
- if (key === "query") {
191
- const currentQueryParameters = merge(qs.parse(url.search, { ignoreQueryPrefix: true }), value, { mergePlainObjects: true });
192
- key = "search";
193
- value = qs.stringify(currentQueryParameters, {
194
- encodeValuesOnly: true,
195
- arrayFormat: "brackets",
196
- filter: (_, object) => object instanceof Set ? [...object] : object
685
+ async function transformOptions(options) {
686
+ const context = getRouterContext();
687
+ if (!options.method) {
688
+ debug.router("Setting method to GET because none was provided.");
689
+ options.method = "GET";
690
+ }
691
+ options.method = options.method.toUpperCase();
692
+ options.mode ??= "navigation";
693
+ options.cancelOnNavigation ??= false;
694
+ options.interruptAsyncOnStart ??= "none";
695
+ if (options.mode === "async" && options.progress === void 0) options.progress = false;
696
+ if (options.mode === "async" && options.replace === void 0) options.replace = true;
697
+ if ((hasFiles(options.data) || options.useFormData) && !(options.data instanceof FormData)) {
698
+ options.data = objectToFormData(options.data);
699
+ debug.router("Converted data to FormData.", options.data);
700
+ }
701
+ if (!(options.data instanceof FormData) && options.method === "GET" && Object.keys(options.data ?? {}).length) {
702
+ debug.router("Transforming data to query parameters.", options.data);
703
+ options.url = makeUrl(options.url ?? context.url, { query: options.data });
704
+ options.data = {};
705
+ }
706
+ if ([
707
+ "PUT",
708
+ "PATCH",
709
+ "DELETE"
710
+ ].includes(options.method) && options.spoof !== false) {
711
+ debug.router(`Automatically spoofing method ${options.method}.`);
712
+ if (options.data instanceof FormData) options.data.append("_method", options.method);
713
+ else if (typeof options.data === "undefined") options.data = { _method: options.method };
714
+ else if (options.data instanceof Object && Object.keys(options.data).length >= 0) Object.assign(options.data, { _method: options.method });
715
+ else debug.router("Could not spoof method because body type is not supported.", options.data);
716
+ options.method = "POST";
717
+ }
718
+ return options;
719
+ }
720
+ //#endregion
721
+ //#region src/router/view.ts
722
+ /**
723
+ * Makes an internal navigation that swaps the view and updates the context.
724
+ * @internal
725
+ */
726
+ async function navigate(options) {
727
+ const context = getRouterContext();
728
+ options.hasDialog ??= !!options.payload?.dialog;
729
+ debug.router("Making an internal navigation:", {
730
+ context,
731
+ options
732
+ });
733
+ await runHooks("navigating", {}, options, context);
734
+ options.payload ??= payloadFromContext();
735
+ options.payload.view ??= payloadFromContext().view;
736
+ options.payload.view.properties = options.properties ?? options.payload.view.properties;
737
+ function evaluateConditionalOption(option) {
738
+ return typeof option === "function" ? option(options) : option;
739
+ }
740
+ const shouldPreserveState = evaluateConditionalOption(options.preserveState);
741
+ const shouldPreserveScroll = evaluateConditionalOption(options.preserveScroll);
742
+ const shouldReplaceHistory = evaluateConditionalOption(options.replace);
743
+ const shouldReplaceUrl = evaluateConditionalOption(options.preserveUrl);
744
+ const shouldPreserveView = !options.payload.view.component;
745
+ if (shouldPreserveState && getHistoryMemo() && options.payload.view.component === context.view.component) {
746
+ debug.history("Setting the memo from this history entry into the current context.");
747
+ setContext({ memo: getHistoryMemo() });
748
+ }
749
+ if (shouldReplaceUrl) {
750
+ debug.router(`Preserving the current URL (${context.url}) instead of navigating to ${options.payload.url}`);
751
+ options.payload.url = context.url;
752
+ }
753
+ setContext({
754
+ ...shouldPreserveView ? {
755
+ view: {
756
+ component: context.view.component,
757
+ properties: merge(context.view.properties, options.payload.view.properties),
758
+ deferred: context.view.deferred,
759
+ mergeable: context.view.mergeable
760
+ },
761
+ url: context.url,
762
+ version: options.payload.version,
763
+ validation: options.payload.validation,
764
+ dialog: context.dialog
765
+ } : options.payload,
766
+ memo: {}
767
+ });
768
+ if (options.updateHistoryState !== false) {
769
+ debug.router(`Target URL is ${context.url}, current window URL is ${window.location.href}.`, { shouldReplaceHistory });
770
+ setHistoryState({ replace: shouldReplaceHistory });
771
+ }
772
+ if (Object.entries(context.view.deferred ?? {}).length) {
773
+ debug.router("Request has deferred properties, queueing a partial reload:", context.view.deferred);
774
+ context.adapter.executeOnMounted(async () => {
775
+ return Promise.all(Object.entries(context.view.deferred).map(async ([_, properties]) => {
776
+ await performHybridNavigation({
777
+ preserveScroll: true,
778
+ preserveState: true,
779
+ replace: true,
780
+ mode: "async",
781
+ only: properties
197
782
  });
198
- }
199
- Reflect.set(url, key, value);
783
+ }));
200
784
  });
201
- if (transformations.trailingSlash === false) {
202
- const _url = removeTrailingSlash(url.toString().replace(/\/\?/, "?"));
203
- url.toString = () => _url;
785
+ }
786
+ const viewComponent = !shouldPreserveView ? await context.adapter.resolveComponent(context.view.component) : void 0;
787
+ if (viewComponent) debug.router(`Component [${context.view.component}] resolved to:`, viewComponent);
788
+ await context.adapter.onViewSwap({
789
+ component: viewComponent,
790
+ dialog: context.dialog,
791
+ properties: options.payload?.view?.properties,
792
+ preserveState: shouldPreserveState,
793
+ onMounted: (hookOptions) => runHooks("mounted", {}, {
794
+ ...options,
795
+ ...hookOptions
796
+ }, context)
797
+ });
798
+ if (options.type === "back-forward" || shouldPreserveScroll) restoreScrollPositions();
799
+ else if (!shouldPreserveScroll) resetScrollPositions();
800
+ await runHooks("navigated", {}, options, context);
801
+ }
802
+ /** Performs a local navigation to the given component without a round-trip. */
803
+ async function performLocalNavigation(targetUrl, options) {
804
+ const context = getRouterContext();
805
+ const url = normalizeUrl(targetUrl);
806
+ return await navigate({
807
+ ...options,
808
+ type: "local",
809
+ payload: {
810
+ version: context.version,
811
+ validation: context.validation,
812
+ dialog: options?.dialog === false ? void 0 : options?.dialog ?? context.dialog,
813
+ url,
814
+ view: {
815
+ component: options?.component ?? context.view.component,
816
+ properties: options?.properties ?? {},
817
+ deferred: {},
818
+ mergeable: []
819
+ }
204
820
  }
205
- return url;
206
- } catch (error) {
207
- throw new TypeError(`${href} is not resolvable to a valid URL.`);
821
+ });
822
+ }
823
+ //#endregion
824
+ //#region src/router/response/external.ts
825
+ /**
826
+ * Performs an external navigation by saving options to the storage and
827
+ * making a full page reload. Upon loading, the navigation options
828
+ * will be pulled and a hybrid navigation will be made.
829
+ */
830
+ async function performExternalNavigation(options) {
831
+ debug.external("Navigating to an external URL:", options);
832
+ if (options.target === "new-tab") {
833
+ const link = document.createElement("a");
834
+ link.style.display = "none";
835
+ link.target = "_blank";
836
+ link.href = options.url;
837
+ link.click();
838
+ setTimeout(() => link.remove(), 0);
839
+ return;
840
+ }
841
+ window.sessionStorage.setItem(STORAGE_EXTERNAL_KEY, JSON.stringify(options));
842
+ window.location.href = options.url;
843
+ if (sameUrls(window.location, options.url)) {
844
+ debug.external("Manually reloading due to the external URL being the same.");
845
+ window.location.reload();
208
846
  }
209
847
  }
848
+ /** Navigates to the given URL without the hybrid protocol. */
849
+ function navigateToExternalUrl(url, data) {
850
+ document.location.href = makeUrl(url, { search: stringifyQueryString(data, { arrayFormat: "brackets" }) }).toString();
851
+ }
852
+ /** Checks if the response wants to redirect to an external URL. */
853
+ function isExternalResponse(response) {
854
+ return response.status === 409 && response.headers.has(EXTERNAL_NAVIGATION_HEADER);
855
+ }
210
856
  /**
211
- * Checks if the given URLs have the same origin and path.
857
+ * Performs the internal navigation when an external navigation to a hybrid view has been made.
858
+ * This method is meant to be called on router creation.
212
859
  */
213
- function sameUrls(...hrefs) {
214
- if (hrefs.length < 2) return true;
860
+ async function handleExternalNavigation() {
861
+ debug.external("Handling an external navigation.");
862
+ const options = JSON.parse(window.sessionStorage.getItem("hybridly:external") || "{}");
863
+ window.sessionStorage.removeItem(STORAGE_EXTERNAL_KEY);
864
+ debug.external("Options from the session storage:", options);
865
+ setContext({ url: makeUrl(getRouterContext().url, { hash: window.location.hash }).toString() });
866
+ await navigate({
867
+ type: "initial",
868
+ preserveState: true,
869
+ preserveScroll: options.preserveScroll
870
+ });
871
+ }
872
+ /** Checks if the navigation being initialized points to an external location. */
873
+ function isExternalNavigation() {
215
874
  try {
216
- return hrefs.every((href) => {
217
- return makeUrl(href, { hash: "" }).toJSON() === makeUrl(hrefs.at(0), { hash: "" }).toJSON();
218
- });
875
+ return window.sessionStorage.getItem(STORAGE_EXTERNAL_KEY) !== null;
219
876
  } catch {}
220
877
  return false;
221
878
  }
879
+ //#endregion
880
+ //#region src/router/response/response.ts
881
+ async function handleHybridRequestResponse({ request, response }) {
882
+ debug.router("Handling response", response);
883
+ const context = getInternalRouterContext();
884
+ const options = request.options;
885
+ saveScrollPositions();
886
+ if (await runHooks("data", options.hooks, request, response, context) === false) return { response };
887
+ if (isExternalResponse(response)) {
888
+ debug.router("The response is explicitely external.");
889
+ await performExternalNavigation({
890
+ url: fillHash(request.url, response.headers.get(EXTERNAL_NAVIGATION_HEADER)),
891
+ preserveScroll: options.preserveScroll === true,
892
+ target: "current"
893
+ });
894
+ return { response };
895
+ }
896
+ if (isDownloadResponse(response)) {
897
+ debug.router("The response returns a file to download.");
898
+ await handleDownloadResponse(response);
899
+ return { response };
900
+ }
901
+ if (!isHybridResponse(response)) {
902
+ debug.router("The response was not hybrid.");
903
+ console.warn("Hybridly received an invalid response.", response);
904
+ await runHooks("fail", request.options.hooks, new InvalidResponseError(), request, context);
905
+ const prevented = !await runHooks("invalid", request.options.hooks, request, response, context);
906
+ if (context.responseErrorModals && !prevented) showResponseErrorModal(typeof response.data === "string" ? response.data : JSON.stringify(response.data, null, 2));
907
+ return { response };
908
+ }
909
+ debug.router("The response respects the Hybridly protocol.");
910
+ const payload = response.data;
911
+ const mergedValidation = mergeValidation(context.validation, payload.validation, options.errorBag);
912
+ const incomingErrors = resolveErrors(payload.validation, options.errorBag);
913
+ if (options.mode !== "async" || context.view.component === request.view.component) {
914
+ const properties = (() => {
915
+ if (!payload.view && !isPartial(options)) return;
916
+ if (!payload.view.component || payload.view.component === context.view.component) return resolveProperties(context.view.properties, payload.view);
917
+ })();
918
+ if (properties) debug.router("Merged properties:", properties);
919
+ await navigate({
920
+ type: "server",
921
+ properties,
922
+ payload: {
923
+ ...payload,
924
+ validation: mergedValidation,
925
+ url: fillHash(request.url, payload.url)
926
+ },
927
+ preserveScroll: options.preserveScroll,
928
+ preserveState: options.preserveState,
929
+ preserveUrl: options.preserveUrl,
930
+ replace: options.replace === true || options.preserveUrl || sameUrls(payload.url, window.location.href) && !sameHashes(payload.url, window.location.href),
931
+ viewTransition: options.viewTransition
932
+ });
933
+ } else debug.router("Discarding navigation from an asynchronous request initiated on a previous page.");
934
+ if (Object.keys(incomingErrors).length > 0) {
935
+ debug.router("The request returned validation errors.", incomingErrors);
936
+ const errors = resolveErrors(context.validation, options.errorBag);
937
+ const resolvedErrors = Object.keys(errors).length > 0 ? errors : incomingErrors;
938
+ await runHooks("validation-error", options.hooks, resolvedErrors, request, context);
939
+ } else await runHooks("success", options.hooks, payload, request, response, context);
940
+ return { response };
941
+ }
942
+ /** Checks if the response contains a hybrid header. */
943
+ function isHybridResponse(response) {
944
+ return response.headers.has(HYBRIDLY_HEADER);
945
+ }
946
+ function isPartial(options) {
947
+ return options.only !== void 0 || options.except !== void 0;
948
+ }
949
+ function resolveProperties(original, payload) {
950
+ const mergedPayloadProperties = merge(original, payload.properties);
951
+ (payload.mergeable ?? []).forEach(([mergeableProperty, prepends, uniqueBy]) => {
952
+ const originalValue = getByPath(original, mergeableProperty);
953
+ const newValue = getByPath(payload.properties, mergeableProperty);
954
+ const mergeArrays = (current, incoming) => {
955
+ const merged = prepends === true ? [...incoming, ...current] : [...current, ...incoming];
956
+ if (typeof uniqueBy !== "string") return merged;
957
+ const getUniqueKey = (entry) => {
958
+ const key = get(entry, uniqueBy);
959
+ return key === void 0 ? Symbol() : key;
960
+ };
961
+ if (prepends === true) return uniqBy(merged, getUniqueKey);
962
+ const orderedKeys = [];
963
+ const valuesByKey = /* @__PURE__ */ new Map();
964
+ for (const entry of merged) {
965
+ const key = getUniqueKey(entry);
966
+ if (!valuesByKey.has(key)) orderedKeys.push(key);
967
+ valuesByKey.set(key, entry);
968
+ }
969
+ return orderedKeys.map((key) => valuesByKey.get(key));
970
+ };
971
+ let value = newValue;
972
+ if (Array.isArray(originalValue)) value = mergeArrays(originalValue, Array.isArray(newValue) ? newValue : newValue === void 0 ? [] : [newValue]);
973
+ else if (originalValue instanceof Object && newValue instanceof Object) value = merge(originalValue, newValue, {
974
+ overwriteArray: false,
975
+ arrayMerge: (current, incoming) => mergeArrays(current, incoming)
976
+ });
977
+ set(mergedPayloadProperties, mergeableProperty, value);
978
+ });
979
+ return mergedPayloadProperties;
980
+ }
981
+ function mergeValidation(current, next, errorBag) {
982
+ if (!errorBag) return next;
983
+ return {
984
+ ...current,
985
+ [errorBag]: next[errorBag] ?? {}
986
+ };
987
+ }
988
+ function resolveErrors(validation, errorBag) {
989
+ if (errorBag) return validation[errorBag] ?? {};
990
+ return validation.default ?? {};
991
+ }
992
+ //#endregion
993
+ //#region src/router/response/response-manager.ts
994
+ const queue = [];
995
+ let processing = false;
222
996
  /**
223
- * Checks if the given URLs have the same origin, path, and hash.
997
+ * Adds a response to the queue and starts processing it if not already doing so.
224
998
  */
225
- function sameHashes(...hrefs) {
226
- if (hrefs.length < 2) return true;
999
+ function enqueueResponse(response) {
1000
+ debug.queue("Enqueuing response", response);
1001
+ queue.push(response);
1002
+ processResponseQueue();
1003
+ }
1004
+ async function processResponseQueue() {
1005
+ if (processing) return;
1006
+ processing = true;
1007
+ await processNextResponse();
1008
+ processing = false;
1009
+ }
1010
+ async function processNextResponse() {
1011
+ const response = queue.shift();
1012
+ if (!response) {
1013
+ debug.queue("End of response queue.");
1014
+ return;
1015
+ }
1016
+ debug.queue("Processing response", response);
1017
+ response.request.resolve(await handleHybridRequestResponse(response));
1018
+ return await processNextResponse();
1019
+ }
1020
+ //#endregion
1021
+ //#region src/router/request/request-manager.ts
1022
+ const activeRequests = {
1023
+ navigation: void 0,
1024
+ async: /* @__PURE__ */ new Map(),
1025
+ asyncGroups: /* @__PURE__ */ new Map()
1026
+ };
1027
+ /**
1028
+ * Registers and starts a request.
1029
+ */
1030
+ function enqueueRequest(request) {
1031
+ debug.queue("Enqueuing request", request);
1032
+ interruptRequestIfNeeded(request);
1033
+ if (isAsyncRequest(request)) {
1034
+ registerAsyncRequest(request);
1035
+ processRequest(request, () => unregisterAsyncRequest(request));
1036
+ return;
1037
+ }
1038
+ activeRequests.navigation = request;
1039
+ processRequest(request, () => {
1040
+ if (activeRequests.navigation?.id === request.id) activeRequests.navigation = void 0;
1041
+ });
1042
+ }
1043
+ function isAsyncRequest(request) {
1044
+ return request.options.mode === "async";
1045
+ }
1046
+ function registerAsyncRequest(request) {
1047
+ activeRequests.async.set(request.id, request);
1048
+ const group = request.options.group;
1049
+ if (!group) return;
1050
+ if (!activeRequests.asyncGroups.has(group)) activeRequests.asyncGroups.set(group, /* @__PURE__ */ new Set());
1051
+ activeRequests.asyncGroups.get(group)?.add(request.id);
1052
+ }
1053
+ function unregisterAsyncRequest(request) {
1054
+ activeRequests.async.delete(request.id);
1055
+ const group = request.options.group;
1056
+ if (!group) return;
1057
+ const requests = activeRequests.asyncGroups.get(group);
1058
+ if (!requests) return;
1059
+ requests.delete(request.id);
1060
+ if (requests.size === 0) activeRequests.asyncGroups.delete(group);
1061
+ }
1062
+ async function processRequest(request, onFinally) {
1063
+ debug.queue("Processing request", request);
227
1064
  try {
228
- return hrefs.every((href) => {
229
- return makeUrl(href).toJSON() === makeUrl(hrefs.at(0)).toJSON();
1065
+ enqueueResponse({
1066
+ request,
1067
+ response: await sendHybridRequest(request)
230
1068
  });
231
- } catch {}
232
- return false;
1069
+ } catch (error) {
1070
+ if (!(error instanceof Error)) error = /* @__PURE__ */ new Error("Unknown error during request processing.");
1071
+ await handleTransportError(request, error);
1072
+ } finally {
1073
+ request.completed = true;
1074
+ debug.router("Ended navigation.", request);
1075
+ await runHooks("after", request.options.hooks, request, getRouterContext());
1076
+ onFinally();
1077
+ }
1078
+ }
1079
+ async function handleTransportError(request, error) {
1080
+ const context = getRouterContext();
1081
+ if (isHttpAbortError(error)) {
1082
+ debug.router("The request was aborted.", error);
1083
+ await runHooks("abort", request.options.hooks, request, context);
1084
+ await runHooks("fail", request.options.hooks, error, request, context);
1085
+ request.resolve({ error });
1086
+ return;
1087
+ }
1088
+ if (isNavigationCancelledError(error)) {
1089
+ debug.router("The request was cancelled through the \"before\" hook.", error);
1090
+ await runHooks("abort", request.options.hooks, request, context);
1091
+ } else {
1092
+ debug.router("An unknown error occured.", error);
1093
+ console.error(error);
1094
+ await runHooks("exception", request.options.hooks, error, request, context);
1095
+ }
1096
+ await runHooks("fail", request.options.hooks, error, request, context);
1097
+ request.resolve({ error });
1098
+ }
1099
+ /**
1100
+ * Interrupts active requests according to the starting request's policy.
1101
+ */
1102
+ function interruptRequestIfNeeded(request) {
1103
+ if (isAsyncRequest(request)) {
1104
+ interruptAsyncRequestsOnStart(request);
1105
+ return;
1106
+ }
1107
+ interruptNavigationRequest({ interrupted: true });
1108
+ interruptAsyncRequestsCancelledByNavigation();
1109
+ interruptAsyncRequestsOnStart(request);
1110
+ }
1111
+ function interruptNavigationRequest(options) {
1112
+ const request = activeRequests.navigation;
1113
+ if (!request) return;
1114
+ activeRequests.navigation = void 0;
1115
+ cancelRequest(request, options);
1116
+ }
1117
+ function interruptAsyncRequestsCancelledByNavigation() {
1118
+ for (const request of activeRequests.async.values()) if (request.options.cancelOnNavigation) interruptAsyncRequest(request, { interrupted: true });
1119
+ }
1120
+ function interruptAsyncRequestsOnStart(request) {
1121
+ debug.queue("Interrupting async requests", request);
1122
+ if (request.options.interruptAsyncOnStart === "none") return;
1123
+ if (request.options.interruptAsyncOnStart === "all") {
1124
+ for (const current of activeRequests.async.values()) interruptAsyncRequest(current, { interrupted: true });
1125
+ return;
1126
+ }
1127
+ if (!request.options.group) return;
1128
+ for (const requestId of activeRequests.asyncGroups.get(request.options.group) ?? []) {
1129
+ const current = activeRequests.async.get(requestId);
1130
+ if (current) interruptAsyncRequest(current, { interrupted: true });
1131
+ }
1132
+ }
1133
+ function interruptAsyncRequest(request, options) {
1134
+ unregisterAsyncRequest(request);
1135
+ cancelRequest(request, options);
233
1136
  }
234
1137
  /**
235
- * If the back-end did not specify a hash, if the navigation specified one,
236
- * and both URLs lead to the same endpoint, we update the target URL
237
- * to use the hash of the initially-requested URL.
1138
+ * Cancels the current navigation request.
238
1139
  */
239
- function fillHash(currentUrl, targetUrl) {
240
- currentUrl = makeUrl(currentUrl);
241
- targetUrl = makeUrl(targetUrl);
242
- if (currentUrl.hash && !targetUrl.hash && sameUrls(targetUrl, currentUrl)) targetUrl.hash = currentUrl.hash;
243
- return targetUrl.toString();
1140
+ function cancelNavigationRequest() {
1141
+ interruptNavigationRequest({ cancelled: true });
1142
+ }
1143
+ function cancelRequest(request, options) {
1144
+ request.completed = false;
1145
+ request.cancelled = options.cancelled ?? false;
1146
+ request.interrupted = options.interrupted ?? false;
1147
+ request.controller.abort();
244
1148
  }
245
1149
  //#endregion
246
1150
  //#region src/router/history.ts
@@ -276,10 +1180,7 @@ async function registerEventListeners() {
276
1180
  debug.history("Registering [popstate] and [scroll] event listeners.");
277
1181
  window?.addEventListener("popstate", async (event) => {
278
1182
  debug.history("Navigation detected (popstate event). State:", { state: event.state });
279
- if (context.pendingNavigation) {
280
- debug.router("Aborting current navigation.", context.pendingNavigation);
281
- context.pendingNavigation?.controller?.abort();
282
- }
1183
+ cancelNavigationRequest();
283
1184
  const state = context.serializer.unserialize(event.state);
284
1185
  await runHooks("backForward", {}, state, context);
285
1186
  if (!state) {
@@ -303,9 +1204,10 @@ async function registerEventListeners() {
303
1204
  updateHistoryState: false
304
1205
  });
305
1206
  });
306
- window?.addEventListener("scroll", (event) => debounce(100, () => {
1207
+ const onScroll = debounce((event) => {
307
1208
  if ((event?.target)?.hasAttribute?.("scroll-region")) saveScrollPositions();
308
- }), true);
1209
+ }, 100);
1210
+ window?.addEventListener("scroll", (event) => onScroll(event), true);
309
1211
  }
310
1212
  /** Checks if the current navigation was made by going back or forward. */
311
1213
  function isBackForwardNavigation() {
@@ -343,6 +1245,7 @@ function serializeContext(context) {
343
1245
  return context.serializer.serialize({
344
1246
  url: context.url,
345
1247
  version: context.version,
1248
+ validation: context.validation,
346
1249
  view: context.view,
347
1250
  dialog: context.dialog,
348
1251
  scrollRegions: context.scrollRegions,
@@ -354,14 +1257,14 @@ function createSerializer(options) {
354
1257
  return {
355
1258
  serialize: (data) => {
356
1259
  debug.history("Serializing data.", data);
357
- return stringify(data);
1260
+ return stringify$1(data);
358
1261
  },
359
1262
  unserialize: (data) => {
360
1263
  if (!data) {
361
1264
  debug.history("No data to unserialize.");
362
1265
  return;
363
1266
  }
364
- return parse(data);
1267
+ return parse$1(data);
365
1268
  }
366
1269
  };
367
1270
  }
@@ -471,8 +1374,7 @@ function getRouteTransformable(routeName, routeParameters, shouldThrow) {
471
1374
  return {
472
1375
  ...domain && { hostname: domain },
473
1376
  pathname: path,
474
- search: qs.stringify(remaining, {
475
- encodeValuesOnly: true,
1377
+ search: stringifyQueryString(remaining, {
476
1378
  arrayFormat: "indices",
477
1379
  addQueryPrefix: true
478
1380
  })
@@ -534,29 +1436,6 @@ function updateRoutingConfiguration(routing) {
534
1436
  setContext({ routing });
535
1437
  }
536
1438
  //#endregion
537
- //#region src/download.ts
538
- /** Checks if the response wants to redirect to an external URL. */
539
- function isDownloadResponse(response) {
540
- return response.status === 200 && !!response.headers["content-disposition"];
541
- }
542
- /** Handles a download. */
543
- async function handleDownloadResponse(response) {
544
- const blob = new Blob([response.data], { type: response.headers["content-type"] });
545
- const urlObject = window.webkitURL || window.URL;
546
- const link = document.createElement("a");
547
- link.style.display = "none";
548
- link.href = urlObject.createObjectURL(blob);
549
- link.download = getFileNameFromContentDispositionHeader(response.headers["content-disposition"]);
550
- link.click();
551
- setTimeout(() => {
552
- urlObject.revokeObjectURL(link.href);
553
- link.remove();
554
- }, 0);
555
- }
556
- function getFileNameFromContentDispositionHeader(header) {
557
- return (header.split(";")[1]?.trim().split("=")[1])?.replace(/^"(.*)"$/, "$1") ?? "";
558
- }
559
- //#endregion
560
1439
  //#region src/context/context.ts
561
1440
  const state = {
562
1441
  initialized: false,
@@ -576,6 +1455,7 @@ async function initializeContext(options) {
576
1455
  state.initialized = true;
577
1456
  state.context = {
578
1457
  ...options.payload,
1458
+ validation: options.payload.validation ?? {},
579
1459
  responseErrorModals: options.responseErrorModals,
580
1460
  serializer: createSerializer(options),
581
1461
  url: makeUrl(options.payload.url).toString(),
@@ -585,9 +1465,8 @@ async function initializeContext(options) {
585
1465
  },
586
1466
  scrollRegions: [],
587
1467
  plugins: options.plugins ?? [],
588
- axios: registerAxios(options.axios ?? axios.create()),
1468
+ http: options.http ?? createXhrHttpClient(),
589
1469
  routing: options.routing,
590
- preloadCache: /* @__PURE__ */ new Map(),
591
1470
  hooks: {},
592
1471
  memo: {}
593
1472
  };
@@ -595,24 +1474,6 @@ async function initializeContext(options) {
595
1474
  return getInternalRouterContext();
596
1475
  }
597
1476
  /**
598
- * Registers an interceptor that assumes `arraybuffer`
599
- * responses and converts responses to JSON or text.
600
- */
601
- function registerAxios(axios) {
602
- axios.interceptors.response.use((response) => {
603
- if (!isDownloadResponse(response)) {
604
- const text = new TextDecoder().decode(response.data);
605
- try {
606
- response.data = JSON.parse(text);
607
- } catch {
608
- response.data = text;
609
- }
610
- }
611
- return response;
612
- }, (error) => Promise.reject(error));
613
- return axios;
614
- }
615
- /**
616
1477
  * Mutates properties at the top-level of the context.
617
1478
  */
618
1479
  function setContext(merge = {}, options = {}) {
@@ -630,70 +1491,12 @@ function payloadFromContext() {
630
1491
  return {
631
1492
  url: getRouterContext().url,
632
1493
  version: getRouterContext().version,
1494
+ validation: getRouterContext().validation,
633
1495
  view: getRouterContext().view,
634
1496
  dialog: getRouterContext().dialog
635
1497
  };
636
1498
  }
637
1499
  //#endregion
638
- //#region src/external.ts
639
- /**
640
- * Performs an external navigation by saving options to the storage and
641
- * making a full page reload. Upon loading, the navigation options
642
- * will be pulled and a hybrid navigation will be made.
643
- */
644
- async function performExternalNavigation(options) {
645
- debug.external("Navigating to an external URL:", options);
646
- if (options.target === "new-tab") {
647
- const link = document.createElement("a");
648
- link.style.display = "none";
649
- link.target = "_blank";
650
- link.href = options.url;
651
- link.click();
652
- setTimeout(() => link.remove(), 0);
653
- return;
654
- }
655
- window.sessionStorage.setItem(STORAGE_EXTERNAL_KEY, JSON.stringify(options));
656
- window.location.href = options.url;
657
- if (sameUrls(window.location, options.url)) {
658
- debug.external("Manually reloading due to the external URL being the same.");
659
- window.location.reload();
660
- }
661
- }
662
- /** Navigates to the given URL without the hybrid protocol. */
663
- function navigateToExternalUrl(url, data) {
664
- document.location.href = makeUrl(url, { search: qs.stringify(data, {
665
- encodeValuesOnly: true,
666
- arrayFormat: "brackets"
667
- }) }).toString();
668
- }
669
- /** Checks if the response wants to redirect to an external URL. */
670
- function isExternalResponse(response) {
671
- return response?.status === 409 && !!response?.headers?.[EXTERNAL_NAVIGATION_HEADER];
672
- }
673
- /**
674
- * Performs the internal navigation when an external navigation to a hybrid view has been made.
675
- * This method is meant to be called on router creation.
676
- */
677
- async function handleExternalNavigation() {
678
- debug.external("Handling an external navigation.");
679
- const options = JSON.parse(window.sessionStorage.getItem("hybridly:external") || "{}");
680
- window.sessionStorage.removeItem(STORAGE_EXTERNAL_KEY);
681
- debug.external("Options from the session storage:", options);
682
- setContext({ url: makeUrl(getRouterContext().url, { hash: window.location.hash }).toString() });
683
- await navigate({
684
- type: "initial",
685
- preserveState: true,
686
- preserveScroll: options.preserveScroll
687
- });
688
- }
689
- /** Checks if the navigation being initialized points to an external location. */
690
- function isExternalNavigation() {
691
- try {
692
- return window.sessionStorage.getItem(STORAGE_EXTERNAL_KEY) !== null;
693
- } catch {}
694
- return false;
695
- }
696
- //#endregion
697
1500
  //#region src/dialog/index.ts
698
1501
  /**
699
1502
  * Closes the dialog.
@@ -719,62 +1522,6 @@ async function closeDialog(options) {
719
1522
  });
720
1523
  }
721
1524
  //#endregion
722
- //#region src/router/preload.ts
723
- /**
724
- * Checks if there is a preloaded request for the given URL.
725
- */
726
- function isPreloaded(targetUrl) {
727
- return getInternalRouterContext().preloadCache.has(targetUrl.toString()) ?? false;
728
- }
729
- /**
730
- * Gets the response of a preloaded request.
731
- */
732
- function getPreloadedRequest(targetUrl) {
733
- return getInternalRouterContext().preloadCache.get(targetUrl.toString());
734
- }
735
- /**
736
- * Stores the response of a preloaded request.
737
- */
738
- function storePreloadRequest(targetUrl, response) {
739
- getInternalRouterContext().preloadCache.set(targetUrl.toString(), response);
740
- }
741
- /**
742
- * Discards a preloaded request.
743
- */
744
- function discardPreloadedRequest(targetUrl) {
745
- return getInternalRouterContext().preloadCache.delete(targetUrl.toString());
746
- }
747
- /** Preloads a hybrid request. */
748
- async function performPreloadRequest(options) {
749
- const context = getRouterContext();
750
- const url = makeUrl(options.url ?? context.url);
751
- if (isPreloaded(url)) {
752
- debug.router("This request is already preloaded.");
753
- return false;
754
- }
755
- if (context.pendingNavigation) {
756
- debug.router("A navigation is pending, preload aborted.");
757
- return false;
758
- }
759
- if (options.method !== "GET") {
760
- debug.router("Cannot preload non-GET requests.");
761
- return false;
762
- }
763
- debug.router(`Preloading response for [${url.toString()}]`);
764
- try {
765
- const response = await performHybridRequest(url, options);
766
- if (!isHybridResponse(response)) {
767
- debug.router("Preload result was invalid.");
768
- return false;
769
- }
770
- storePreloadRequest(url, response);
771
- return true;
772
- } catch (error) {
773
- debug.router("Preloading failed.");
774
- return false;
775
- }
776
- }
777
- //#endregion
778
1525
  //#region src/router/router.ts
779
1526
  /**
780
1527
  * The hybridly router.
@@ -786,13 +1533,13 @@ async function performPreloadRequest(options) {
786
1533
  * router.get('/posts/edit', { post })
787
1534
  */
788
1535
  const router = {
789
- abort: async () => getRouterContext().pendingNavigation?.controller.abort(),
790
- active: () => !!getRouterContext().pendingNavigation,
1536
+ abort: () => cancelNavigationRequest(),
791
1537
  navigate: async (options) => await performHybridNavigation(options),
792
1538
  reload: async (options) => await performHybridNavigation({
793
1539
  preserveScroll: true,
794
1540
  preserveState: true,
795
1541
  replace: true,
1542
+ mode: "async",
796
1543
  ...options
797
1544
  }),
798
1545
  get: async (url, options = {}) => await performHybridNavigation({
@@ -825,11 +1572,6 @@ const router = {
825
1572
  method: "DELETE"
826
1573
  }),
827
1574
  local: async (url, options = {}) => await performLocalNavigation(url, options),
828
- preload: async (url, options = {}) => await performPreloadRequest({
829
- ...options,
830
- url,
831
- method: "GET"
832
- }),
833
1575
  external: (url, data = {}) => navigateToExternalUrl(url, data),
834
1576
  to: async (name, parameters, options) => {
835
1577
  const url = generateRouteFromName(name, parameters);
@@ -842,7 +1584,7 @@ const router = {
842
1584
  },
843
1585
  matches: (name, parameters) => currentRouteMatches(name, parameters),
844
1586
  current: () => getCurrentRouteName(),
845
- dialog: { close: (options) => closeDialog(options) },
1587
+ dialog: { close: (options = {}) => closeDialog(options) },
846
1588
  history: {
847
1589
  get: (key) => getHistoryMemo(key),
848
1590
  remember: (key, value) => remember(key, value)
@@ -853,274 +1595,6 @@ async function createRouter(options) {
853
1595
  await initializeContext(options);
854
1596
  return await initializeRouter();
855
1597
  }
856
- /** Performs every action necessary to make a hybrid navigation. */
857
- async function performHybridNavigation(options) {
858
- const navigationId = random();
859
- const context = getRouterContext();
860
- debug.router("Making a hybrid navigation:", {
861
- context,
862
- options,
863
- navigationId
864
- });
865
- try {
866
- if (!options.method) {
867
- debug.router("Setting method to GET because none was provided.");
868
- options.method = "GET";
869
- }
870
- options.method = options.method.toUpperCase();
871
- if ((hasFiles(options.data) || options.useFormData) && !(options.data instanceof FormData)) {
872
- options.data = objectToFormData(options.data);
873
- debug.router("Converted data to FormData.", options.data);
874
- }
875
- if (!(options.data instanceof FormData) && options.method === "GET" && Object.keys(options.data ?? {}).length) {
876
- debug.router("Transforming data to query parameters.", options.data);
877
- options.url = makeUrl(options.url ?? context.url, { query: options.data });
878
- options.data = {};
879
- }
880
- if ([
881
- "PUT",
882
- "PATCH",
883
- "DELETE"
884
- ].includes(options.method) && options.spoof !== false) {
885
- debug.router(`Automatically spoofing method ${options.method}.`);
886
- if (options.data instanceof FormData) options.data.append("_method", options.method);
887
- else if (typeof options.data === "undefined") options.data = { _method: options.method };
888
- else if (options.data instanceof Object && Object.keys(options.data).length >= 0) Object.assign(options.data, { _method: options.method });
889
- else debug.router("Could not spoof method because body type is not supported.", options.data);
890
- options.method = "POST";
891
- }
892
- if (!await runHooks("before", options.hooks, options, context)) {
893
- debug.router("\"before\" event returned false, aborting the navigation.");
894
- throw new NavigationCancelledError("The navigation was cancelled by the \"before\" event.");
895
- }
896
- if (context.pendingNavigation) {
897
- debug.router("Aborting current navigation.", context.pendingNavigation);
898
- context.pendingNavigation?.controller?.abort();
899
- }
900
- saveScrollPositions();
901
- const targetUrl = makeUrl(options.url ?? context.url, options.transformUrl);
902
- const abortController = new AbortController();
903
- setContext({ pendingNavigation: {
904
- id: navigationId,
905
- url: targetUrl,
906
- controller: abortController,
907
- status: "pending",
908
- options
909
- } });
910
- await runHooks("start", options.hooks, context);
911
- debug.router("Making request with axios.");
912
- const response = await performHybridRequest(targetUrl, options, abortController);
913
- if (await runHooks("data", options.hooks, response, context) === false) return { response };
914
- if (isExternalResponse(response)) {
915
- debug.router("The response is explicitely external.");
916
- await performExternalNavigation({
917
- url: fillHash(targetUrl, response.headers[EXTERNAL_NAVIGATION_HEADER]),
918
- preserveScroll: options.preserveScroll === true,
919
- target: response.headers[EXTERNAL_NAVIGATION_TARGET_HEADER] ?? "current"
920
- });
921
- return { response };
922
- }
923
- if (isDownloadResponse(response)) {
924
- debug.router("The response returns a file to download.");
925
- await handleDownloadResponse(response);
926
- return { response };
927
- }
928
- if (!isHybridResponse(response)) throw new NotAHybridResponseError(response);
929
- debug.router("The response respects the Hybridly protocol.");
930
- const payload = response.data;
931
- if (payload.view && (options.only?.length ?? options.except?.length) && payload.view.component === context.view.component) {
932
- debug.router(`Merging ${options.only ? "\"only\"" : "\"except\""} properties.`, payload.view.properties);
933
- const mergedPayloadProperties = merge(context.view.properties, payload.view.properties);
934
- if (options.errorBag) mergedPayloadProperties.errors[options.errorBag] = payload.view.properties.errors[options.errorBag] ?? {};
935
- else mergedPayloadProperties.errors = payload.view.properties.errors;
936
- payload.view.properties = mergedPayloadProperties;
937
- debug.router("Merged properties:", payload.view.properties);
938
- }
939
- await navigate({
940
- type: "server",
941
- payload: {
942
- ...payload,
943
- url: fillHash(targetUrl, payload.url)
944
- },
945
- preserveScroll: options.preserveScroll,
946
- preserveState: options.preserveState,
947
- preserveUrl: options.preserveUrl,
948
- replace: options.replace === true || options.preserveUrl || sameUrls(payload.url, window.location.href) && !sameHashes(payload.url, window.location.href)
949
- });
950
- if (Object.keys(context.view.properties.errors ?? {}).length > 0) {
951
- const errors = (() => {
952
- if (options.errorBag && typeof context.view.properties.errors === "object") return context.view.properties.errors[options.errorBag] ?? {};
953
- return context.view.properties.errors;
954
- })();
955
- debug.router("The request returned validation errors.", errors);
956
- setContext({ pendingNavigation: {
957
- ...context.pendingNavigation,
958
- status: "error"
959
- } });
960
- await runHooks("error", options.hooks, errors, context);
961
- } else {
962
- setContext({ pendingNavigation: {
963
- ...context.pendingNavigation,
964
- status: "success"
965
- } });
966
- await runHooks("success", options.hooks, payload, context);
967
- }
968
- return { response };
969
- } catch (error) {
970
- await match(error.constructor.name, {
971
- NavigationCancelledError: async () => {
972
- debug.router("The request was cancelled through the \"before\" hook.", error);
973
- await runHooks("abort", options.hooks, context);
974
- },
975
- AbortError: async () => {
976
- debug.router("The request was aborted.", error);
977
- await runHooks("abort", options.hooks, context);
978
- },
979
- NotAHybridResponseError: async () => {
980
- debug.router("The response was not hybrid.");
981
- console.error(error);
982
- await runHooks("invalid", options.hooks, error, context);
983
- if (context.responseErrorModals) showResponseErrorModal(error.response.data);
984
- },
985
- default: async () => {
986
- if (error?.name === "CanceledError") {
987
- debug.router("The request was cancelled.", error);
988
- await runHooks("abort", options.hooks, context);
989
- } else {
990
- debug.router("An unknown error occured.", error);
991
- console.error(error);
992
- await runHooks("exception", options.hooks, error, context);
993
- }
994
- }
995
- });
996
- await runHooks("fail", options.hooks, context);
997
- return { error: {
998
- type: error.constructor.name,
999
- actual: error
1000
- } };
1001
- } finally {
1002
- debug.router("Ending navigation.");
1003
- await runHooks("after", options.hooks, context);
1004
- if (context.pendingNavigation?.id === navigationId) setContext({ pendingNavigation: void 0 });
1005
- }
1006
- }
1007
- /** Checks if the response contains a hybrid header. */
1008
- function isHybridResponse(response) {
1009
- return !!response?.headers[HYBRIDLY_HEADER];
1010
- }
1011
- /**
1012
- * Makes an internal navigation that swaps the view and updates the context.
1013
- * @internal
1014
- */
1015
- async function navigate(options) {
1016
- const context = getRouterContext();
1017
- options.hasDialog ??= !!options.payload?.dialog;
1018
- debug.router("Making an internal navigation:", {
1019
- context,
1020
- options
1021
- });
1022
- await runHooks("navigating", {}, options, context);
1023
- options.payload ??= payloadFromContext();
1024
- options.payload.view ??= payloadFromContext().view;
1025
- function evaluateConditionalOption(option) {
1026
- return typeof option === "function" ? option(options) : option;
1027
- }
1028
- const shouldPreserveState = evaluateConditionalOption(options.preserveState);
1029
- const shouldPreserveScroll = evaluateConditionalOption(options.preserveScroll);
1030
- const shouldReplaceHistory = evaluateConditionalOption(options.replace);
1031
- const shouldReplaceUrl = evaluateConditionalOption(options.preserveUrl);
1032
- const shouldPreserveView = !options.payload.view.component;
1033
- if (shouldPreserveState && getHistoryMemo() && options.payload.view.component === context.view.component) {
1034
- debug.history("Setting the memo from this history entry into the current context.");
1035
- setContext({ memo: getHistoryMemo() });
1036
- }
1037
- if (shouldReplaceUrl) {
1038
- debug.router(`Preserving the current URL (${context.url}) instead of navigating to ${options.payload.url}`);
1039
- options.payload.url = context.url;
1040
- }
1041
- setContext({
1042
- ...shouldPreserveView ? {
1043
- view: {
1044
- component: context.view.component,
1045
- properties: merge(context.view.properties, options.payload.view.properties),
1046
- deferred: context.view.deferred
1047
- },
1048
- url: context.url,
1049
- version: options.payload.version,
1050
- dialog: context.dialog
1051
- } : options.payload,
1052
- memo: {}
1053
- });
1054
- if (options.updateHistoryState !== false) {
1055
- debug.router(`Target URL is ${context.url}, current window URL is ${window.location.href}.`, { shouldReplaceHistory });
1056
- setHistoryState({ replace: shouldReplaceHistory });
1057
- }
1058
- if (context.view.deferred?.length) {
1059
- debug.router("Request has deferred properties, queueing a partial reload:", context.view.deferred);
1060
- context.adapter.executeOnMounted(async () => {
1061
- await performHybridNavigation({
1062
- preserveScroll: true,
1063
- preserveState: true,
1064
- replace: true,
1065
- only: context.view.deferred
1066
- });
1067
- });
1068
- }
1069
- const viewComponent = !shouldPreserveView ? await context.adapter.resolveComponent(context.view.component) : void 0;
1070
- if (viewComponent) debug.router(`Component [${context.view.component}] resolved to:`, viewComponent);
1071
- await context.adapter.onViewSwap({
1072
- component: viewComponent,
1073
- dialog: context.dialog,
1074
- properties: options.payload?.view?.properties,
1075
- preserveState: shouldPreserveState,
1076
- onMounted: (hookOptions) => runHooks("mounted", {}, {
1077
- ...options,
1078
- ...hookOptions
1079
- }, context)
1080
- });
1081
- if (options.type === "back-forward") restoreScrollPositions();
1082
- else if (!shouldPreserveScroll) resetScrollPositions();
1083
- await runHooks("navigated", {}, options, context);
1084
- }
1085
- async function performHybridRequest(targetUrl, options, abortController) {
1086
- const context = getInternalRouterContext();
1087
- const preloaded = options.method === "GET" ? getPreloadedRequest(targetUrl) : false;
1088
- if (preloaded) {
1089
- debug.router(`Found a pre-loaded request for [${targetUrl}]`);
1090
- discardPreloadedRequest(targetUrl);
1091
- return preloaded;
1092
- }
1093
- return await context.axios.request({
1094
- url: targetUrl.toString(),
1095
- method: options.method,
1096
- data: options.method === "GET" ? {} : options.data,
1097
- params: options.method === "GET" ? options.data : {},
1098
- signal: abortController?.signal,
1099
- headers: {
1100
- ...options.headers,
1101
- ...context.dialog ? { [DIALOG_KEY_HEADER]: context.dialog.key } : {},
1102
- ...context.dialog ? { [DIALOG_REDIRECT_HEADER]: context.dialog.redirectUrl ?? "" } : {},
1103
- ...when(options.only !== void 0 || options.except !== void 0, {
1104
- [PARTIAL_COMPONENT_HEADER]: context.view.component,
1105
- ...when(options.only, { [ONLY_DATA_HEADER]: JSON.stringify(options.only) }, {}),
1106
- ...when(options.except, { [EXCEPT_DATA_HEADER]: JSON.stringify(options.except) }, {})
1107
- }, {}),
1108
- ...when(options.errorBag, { [ERROR_BAG_HEADER]: options.errorBag }, {}),
1109
- ...when(context.version, { [VERSION_HEADER]: context.version }, {}),
1110
- [HYBRIDLY_HEADER]: true,
1111
- "X-Requested-With": "XMLHttpRequest",
1112
- "Accept": "text/html, application/xhtml+xml"
1113
- },
1114
- responseType: "arraybuffer",
1115
- validateStatus: () => true,
1116
- onUploadProgress: async (event) => {
1117
- await runHooks("progress", options.hooks, {
1118
- event,
1119
- percentage: Math.round(event.loaded / (event.total ?? 0) * 100)
1120
- }, context);
1121
- }
1122
- });
1123
- }
1124
1598
  /** Initializes the router by reading the context and registering events if necessary. */
1125
1599
  async function initializeRouter() {
1126
1600
  const context = getRouterContext();
@@ -1139,25 +1613,6 @@ async function initializeRouter() {
1139
1613
  await runHooks("ready", {}, context);
1140
1614
  return context;
1141
1615
  }
1142
- /** Performs a local navigation to the given component without a round-trip. */
1143
- async function performLocalNavigation(targetUrl, options) {
1144
- const context = getRouterContext();
1145
- const url = normalizeUrl(targetUrl);
1146
- return await navigate({
1147
- ...options,
1148
- type: "local",
1149
- payload: {
1150
- version: context.version,
1151
- dialog: options?.dialog === false ? void 0 : options?.dialog ?? context.dialog,
1152
- url,
1153
- view: {
1154
- component: options?.component ?? context.view.component,
1155
- properties: options?.properties ?? context.view.properties,
1156
- deferred: []
1157
- }
1158
- }
1159
- });
1160
- }
1161
1616
  //#endregion
1162
1617
  //#region src/authorization.ts
1163
1618
  /**
@@ -1168,4 +1623,4 @@ function can(resource, action) {
1168
1623
  return resource.authorization?.[action] ?? false;
1169
1624
  }
1170
1625
  //#endregion
1171
- export { can, constants_exports as constants, createRouter, definePlugin, getRouterContext, makeUrl, registerHook, route, router, sameUrls };
1626
+ export { can, constants_exports as constants, createRouter, createXhrHttpClient, definePlugin, getRouterContext, isHttpAbortError, isHttpError, makeUrl, parseQueryString, registerHook, route, router, sameUrls, stringifyQueryString };