@esmx/router 3.0.0-rc.17 → 3.0.0-rc.19
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/LICENSE +1 -1
- package/README.md +70 -0
- package/README.zh-CN.md +70 -0
- package/dist/error.d.ts +23 -0
- package/dist/error.mjs +61 -0
- package/dist/increment-id.d.ts +7 -0
- package/dist/increment-id.mjs +11 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.mjs +14 -3
- package/dist/index.test.mjs +8 -0
- package/dist/location.d.ts +15 -0
- package/dist/location.mjs +53 -0
- package/dist/location.test.d.ts +8 -0
- package/dist/location.test.mjs +370 -0
- package/dist/matcher.d.ts +3 -0
- package/dist/matcher.mjs +44 -0
- package/dist/matcher.test.mjs +1492 -0
- package/dist/micro-app.d.ts +18 -0
- package/dist/micro-app.dom.test.d.ts +1 -0
- package/dist/micro-app.dom.test.mjs +532 -0
- package/dist/micro-app.mjs +80 -0
- package/dist/navigation.d.ts +43 -0
- package/dist/navigation.mjs +143 -0
- package/dist/navigation.test.d.ts +1 -0
- package/dist/navigation.test.mjs +681 -0
- package/dist/options.d.ts +4 -0
- package/dist/options.mjs +88 -0
- package/dist/route-task.d.ts +40 -0
- package/dist/route-task.mjs +75 -0
- package/dist/route-task.test.d.ts +1 -0
- package/dist/route-task.test.mjs +673 -0
- package/dist/route-transition.d.ts +53 -0
- package/dist/route-transition.mjs +307 -0
- package/dist/route-transition.test.d.ts +1 -0
- package/dist/route-transition.test.mjs +146 -0
- package/dist/route.d.ts +72 -0
- package/dist/route.mjs +194 -0
- package/dist/route.test.d.ts +1 -0
- package/dist/route.test.mjs +1664 -0
- package/dist/router-back.test.d.ts +1 -0
- package/dist/router-back.test.mjs +361 -0
- package/dist/router-forward.test.d.ts +1 -0
- package/dist/router-forward.test.mjs +376 -0
- package/dist/router-go.test.d.ts +1 -0
- package/dist/router-go.test.mjs +73 -0
- package/dist/router-guards-cleanup.test.d.ts +1 -0
- package/dist/router-guards-cleanup.test.mjs +437 -0
- package/dist/router-link.d.ts +10 -0
- package/dist/router-link.mjs +126 -0
- package/dist/router-push.test.d.ts +1 -0
- package/dist/router-push.test.mjs +115 -0
- package/dist/router-replace.test.d.ts +1 -0
- package/dist/router-replace.test.mjs +114 -0
- package/dist/router-resolve.test.d.ts +1 -0
- package/dist/router-resolve.test.mjs +393 -0
- package/dist/router-restart-app.dom.test.d.ts +1 -0
- package/dist/router-restart-app.dom.test.mjs +616 -0
- package/dist/router-window-navigation.test.d.ts +1 -0
- package/dist/router-window-navigation.test.mjs +359 -0
- package/dist/router.d.ts +109 -102
- package/dist/router.mjs +260 -361
- package/dist/types.d.ts +246 -0
- package/dist/types.mjs +18 -0
- package/dist/util.d.ts +26 -0
- package/dist/util.mjs +53 -0
- package/dist/util.test.d.ts +1 -0
- package/dist/util.test.mjs +1020 -0
- package/package.json +10 -13
- package/src/error.ts +84 -0
- package/src/increment-id.ts +12 -0
- package/src/index.test.ts +9 -0
- package/src/index.ts +54 -3
- package/src/location.test.ts +406 -0
- package/src/location.ts +96 -0
- package/src/matcher.test.ts +1685 -0
- package/src/matcher.ts +59 -0
- package/src/micro-app.dom.test.ts +708 -0
- package/src/micro-app.ts +101 -0
- package/src/navigation.test.ts +858 -0
- package/src/navigation.ts +195 -0
- package/src/options.ts +131 -0
- package/src/route-task.test.ts +901 -0
- package/src/route-task.ts +105 -0
- package/src/route-transition.test.ts +178 -0
- package/src/route-transition.ts +425 -0
- package/src/route.test.ts +2014 -0
- package/src/route.ts +308 -0
- package/src/router-back.test.ts +487 -0
- package/src/router-forward.test.ts +506 -0
- package/src/router-go.test.ts +91 -0
- package/src/router-guards-cleanup.test.ts +595 -0
- package/src/router-link.ts +235 -0
- package/src/router-push.test.ts +140 -0
- package/src/router-replace.test.ts +139 -0
- package/src/router-resolve.test.ts +475 -0
- package/src/router-restart-app.dom.test.ts +783 -0
- package/src/router-window-navigation.test.ts +457 -0
- package/src/router.ts +289 -470
- package/src/types.ts +341 -0
- package/src/util.test.ts +1262 -0
- package/src/util.ts +116 -0
- package/dist/history/abstract.d.ts +0 -29
- package/dist/history/abstract.mjs +0 -107
- package/dist/history/base.d.ts +0 -79
- package/dist/history/base.mjs +0 -275
- package/dist/history/html.d.ts +0 -22
- package/dist/history/html.mjs +0 -183
- package/dist/history/index.d.ts +0 -7
- package/dist/history/index.mjs +0 -16
- package/dist/matcher/create-matcher.d.ts +0 -5
- package/dist/matcher/create-matcher.mjs +0 -218
- package/dist/matcher/create-matcher.spec.mjs +0 -0
- package/dist/matcher/index.d.ts +0 -1
- package/dist/matcher/index.mjs +0 -1
- package/dist/task-pipe/index.d.ts +0 -1
- package/dist/task-pipe/index.mjs +0 -1
- package/dist/task-pipe/task.d.ts +0 -30
- package/dist/task-pipe/task.mjs +0 -66
- package/dist/utils/bom.d.ts +0 -5
- package/dist/utils/bom.mjs +0 -10
- package/dist/utils/encoding.d.ts +0 -48
- package/dist/utils/encoding.mjs +0 -44
- package/dist/utils/guards.d.ts +0 -9
- package/dist/utils/guards.mjs +0 -12
- package/dist/utils/index.d.ts +0 -7
- package/dist/utils/index.mjs +0 -27
- package/dist/utils/path.d.ts +0 -60
- package/dist/utils/path.mjs +0 -281
- package/dist/utils/path.spec.mjs +0 -27
- package/dist/utils/scroll.d.ts +0 -25
- package/dist/utils/scroll.mjs +0 -59
- package/dist/utils/utils.d.ts +0 -16
- package/dist/utils/utils.mjs +0 -11
- package/dist/utils/warn.d.ts +0 -2
- package/dist/utils/warn.mjs +0 -12
- package/src/history/abstract.ts +0 -149
- package/src/history/base.ts +0 -408
- package/src/history/html.ts +0 -228
- package/src/history/index.ts +0 -20
- package/src/matcher/create-matcher.spec.ts +0 -3
- package/src/matcher/create-matcher.ts +0 -293
- package/src/matcher/index.ts +0 -1
- package/src/task-pipe/index.ts +0 -1
- package/src/task-pipe/task.ts +0 -97
- package/src/utils/bom.ts +0 -14
- package/src/utils/encoding.ts +0 -153
- package/src/utils/guards.ts +0 -25
- package/src/utils/index.ts +0 -27
- package/src/utils/path.spec.ts +0 -32
- package/src/utils/path.ts +0 -417
- package/src/utils/scroll.ts +0 -120
- package/src/utils/utils.ts +0 -30
- package/src/utils/warn.ts +0 -13
- /package/dist/{matcher/create-matcher.spec.d.ts → index.test.d.ts} +0 -0
- /package/dist/{utils/path.spec.d.ts → matcher.test.d.ts} +0 -0
package/src/location.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { RouteLocationInput } from './types';
|
|
2
|
+
import { isNotNullish } from './util';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Normalizes a URL input into a URL object.
|
|
6
|
+
* @param url - The URL or string to normalize.
|
|
7
|
+
* @param base - The base URL to resolve against if the input is relative.
|
|
8
|
+
* @returns A URL object.
|
|
9
|
+
*/
|
|
10
|
+
export function normalizeURL(url: string | URL, base: URL): URL {
|
|
11
|
+
if (url instanceof URL) {
|
|
12
|
+
return url;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Handle protocol-relative URLs (e.g., //example.com)
|
|
16
|
+
if (url.startsWith('//')) {
|
|
17
|
+
// Using http: as a default protocol for protocol-relative URLs.
|
|
18
|
+
return new URL(`http:${url}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Handle root-relative paths
|
|
22
|
+
if (url.startsWith('/')) {
|
|
23
|
+
const newBase = new URL('.', base);
|
|
24
|
+
const parsed = new URL(url, newBase);
|
|
25
|
+
// This ensures that the path is resolved relative to the base's path directory.
|
|
26
|
+
parsed.pathname = newBase.pathname.slice(0, -1) + parsed.pathname;
|
|
27
|
+
return parsed;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
// Try to parse as an absolute URL.
|
|
32
|
+
// This is the WHATWG standard approach (new URL()) and works consistently across all modern browsers and Node.js.
|
|
33
|
+
// We use a try-catch block because the standard URL constructor throws an error for invalid URLs.
|
|
34
|
+
//
|
|
35
|
+
// NOTE: While `URL.parse()` might be observed in Chromium-based browsers (e.g., Chrome, Edge),
|
|
36
|
+
// it is a non-standard, legacy feature implemented by the V8 engine for Node.js compatibility.
|
|
37
|
+
// It is not part of the WHATWG URL Standard and is not supported by other browsers like Firefox or Safari.
|
|
38
|
+
// Therefore, relying on it would compromise cross-browser compatibility.
|
|
39
|
+
return new URL(url);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
// Otherwise, parse as a relative URL
|
|
42
|
+
return new URL(url, base);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Parses a RouteLocationInput object into a full URL.
|
|
48
|
+
* @param toInput - The route location input.
|
|
49
|
+
* @param baseURL - The base URL to resolve against.
|
|
50
|
+
* @returns The parsed URL object.
|
|
51
|
+
*/
|
|
52
|
+
export function parseLocation(toInput: RouteLocationInput, baseURL: URL): URL {
|
|
53
|
+
if (typeof toInput === 'string') {
|
|
54
|
+
return normalizeURL(toInput, baseURL);
|
|
55
|
+
}
|
|
56
|
+
const url = normalizeURL(toInput.path ?? toInput.url ?? '', baseURL);
|
|
57
|
+
const searchParams = url.searchParams;
|
|
58
|
+
|
|
59
|
+
// Priority: queryArray > query > query in path
|
|
60
|
+
const mergedQuery: Record<string, string | string[]> = {};
|
|
61
|
+
|
|
62
|
+
// First, add query values
|
|
63
|
+
if (toInput.query) {
|
|
64
|
+
Object.entries(toInput.query).forEach(([key, value]) => {
|
|
65
|
+
if (typeof value !== 'undefined') {
|
|
66
|
+
mergedQuery[key] = value;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Then, add queryArray values (higher priority)
|
|
72
|
+
if (toInput.queryArray) {
|
|
73
|
+
Object.entries(toInput.queryArray).forEach(([key, value]) => {
|
|
74
|
+
if (typeof value !== 'undefined') {
|
|
75
|
+
mergedQuery[key] = value;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
Object.entries(mergedQuery).forEach(([key, value]) => {
|
|
81
|
+
searchParams.delete(key); // Clear previous params with the same name
|
|
82
|
+
value = Array.isArray(value) ? value : [value];
|
|
83
|
+
value
|
|
84
|
+
.filter((v) => isNotNullish(v) && !Number.isNaN(v))
|
|
85
|
+
.forEach((v) => {
|
|
86
|
+
searchParams.append(key, String(v));
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Set the hash (URL fragment identifier)
|
|
91
|
+
if (toInput.hash) {
|
|
92
|
+
url.hash = toInput.hash;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return url;
|
|
96
|
+
}
|