@esmx/router 3.0.0-rc.18 → 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 -30
- 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/types/index.d.ts +0 -694
- package/dist/types/index.mjs +0 -6
- 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 -282
- 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 -292
- 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/types/index.ts +0 -858
- 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 -418
- 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/dist/route.mjs
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { parseLocation } from "./location.mjs";
|
|
2
|
+
import { parsedOptions } from "./options.mjs";
|
|
3
|
+
import {
|
|
4
|
+
RouteType
|
|
5
|
+
} from "./types.mjs";
|
|
6
|
+
import { isNonEmptyPlainObject, isPlainObject } from "./util.mjs";
|
|
7
|
+
export const NON_ENUMERABLE_PROPERTIES = [
|
|
8
|
+
// Private fields - internal implementation details
|
|
9
|
+
"_handled",
|
|
10
|
+
"_handle",
|
|
11
|
+
"_handleResult",
|
|
12
|
+
"_options",
|
|
13
|
+
// SSR-specific properties - meaningless in client environment
|
|
14
|
+
"req",
|
|
15
|
+
"res",
|
|
16
|
+
// Internal context - used by framework internally
|
|
17
|
+
"context",
|
|
18
|
+
// Status code - internal status information
|
|
19
|
+
"statusCode",
|
|
20
|
+
// Route behavior overrides - framework internal logic
|
|
21
|
+
"confirm",
|
|
22
|
+
// Layer configuration - used for layer routes
|
|
23
|
+
"layer"
|
|
24
|
+
];
|
|
25
|
+
export function applyRouteParams(match, toInput, base, to) {
|
|
26
|
+
if (!isPlainObject(toInput) || !isNonEmptyPlainObject(toInput.params) || !match.matches.length) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const lastMatch = match.matches[match.matches.length - 1];
|
|
30
|
+
const current = to.pathname.split("/");
|
|
31
|
+
const next = new URL(
|
|
32
|
+
lastMatch.compile(toInput.params).substring(1),
|
|
33
|
+
base
|
|
34
|
+
).pathname.split("/");
|
|
35
|
+
next.forEach((item, index) => {
|
|
36
|
+
current[index] = item || current[index];
|
|
37
|
+
});
|
|
38
|
+
to.pathname = current.join("/");
|
|
39
|
+
Object.assign(match.params, toInput.params);
|
|
40
|
+
}
|
|
41
|
+
export class Route {
|
|
42
|
+
// Private fields for handle validation
|
|
43
|
+
_handled = false;
|
|
44
|
+
_handle = null;
|
|
45
|
+
_handleResult = null;
|
|
46
|
+
_options;
|
|
47
|
+
// Public properties
|
|
48
|
+
statusCode = null;
|
|
49
|
+
state;
|
|
50
|
+
keepScrollPosition;
|
|
51
|
+
/** Custom confirm handler that overrides default route-transition confirm logic */
|
|
52
|
+
confirm;
|
|
53
|
+
/** Layer configuration for layer routes */
|
|
54
|
+
layer;
|
|
55
|
+
// Read-only properties
|
|
56
|
+
type;
|
|
57
|
+
req;
|
|
58
|
+
res;
|
|
59
|
+
context;
|
|
60
|
+
url;
|
|
61
|
+
path;
|
|
62
|
+
fullPath;
|
|
63
|
+
hash;
|
|
64
|
+
params = {};
|
|
65
|
+
query = {};
|
|
66
|
+
queryArray = {};
|
|
67
|
+
meta;
|
|
68
|
+
matched;
|
|
69
|
+
config;
|
|
70
|
+
constructor(routeOptions = {}) {
|
|
71
|
+
var _a;
|
|
72
|
+
const {
|
|
73
|
+
toType = RouteType.push,
|
|
74
|
+
toInput = "/",
|
|
75
|
+
from = null,
|
|
76
|
+
options = parsedOptions()
|
|
77
|
+
} = routeOptions;
|
|
78
|
+
this._options = options;
|
|
79
|
+
this.type = toType;
|
|
80
|
+
this.req = options.req;
|
|
81
|
+
this.res = options.res;
|
|
82
|
+
this.context = options.context;
|
|
83
|
+
const base = options.base;
|
|
84
|
+
const to = options.normalizeURL(parseLocation(toInput, base), from);
|
|
85
|
+
const isSameOrigin = to.origin === base.origin;
|
|
86
|
+
const isSameBase = to.pathname.startsWith(base.pathname);
|
|
87
|
+
const match = isSameOrigin && isSameBase ? options.matcher(to, base) : null;
|
|
88
|
+
this.url = to;
|
|
89
|
+
this.path = match ? to.pathname.substring(base.pathname.length - 1) : to.pathname;
|
|
90
|
+
this.fullPath = (match ? this.path : to.pathname) + to.search + to.hash;
|
|
91
|
+
this.matched = match ? match.matches : Object.freeze([]);
|
|
92
|
+
this.keepScrollPosition = isPlainObject(toInput) ? Boolean(toInput.keepScrollPosition) : false;
|
|
93
|
+
this.confirm = isPlainObject(toInput) && toInput.confirm ? toInput.confirm : null;
|
|
94
|
+
this.layer = toType === RouteType.pushLayer && isPlainObject(toInput) && toInput.layer ? toInput.layer : null;
|
|
95
|
+
this.config = this.matched.length > 0 ? this.matched[this.matched.length - 1] : null;
|
|
96
|
+
this.meta = ((_a = this.config) == null ? void 0 : _a.meta) || {};
|
|
97
|
+
const state = {};
|
|
98
|
+
if (isPlainObject(toInput) && toInput.state) {
|
|
99
|
+
Object.assign(state, toInput.state);
|
|
100
|
+
}
|
|
101
|
+
this.state = state;
|
|
102
|
+
for (const key of new Set(to.searchParams.keys())) {
|
|
103
|
+
this.query[key] = to.searchParams.get(key);
|
|
104
|
+
this.queryArray[key] = to.searchParams.getAll(key);
|
|
105
|
+
}
|
|
106
|
+
this.hash = to.hash;
|
|
107
|
+
if (match) {
|
|
108
|
+
applyRouteParams(match, toInput, base, to);
|
|
109
|
+
Object.assign(this.params, match.params);
|
|
110
|
+
}
|
|
111
|
+
if (isPlainObject(toInput) && typeof toInput.statusCode === "number") {
|
|
112
|
+
this.statusCode = toInput.statusCode;
|
|
113
|
+
}
|
|
114
|
+
for (const property of NON_ENUMERABLE_PROPERTIES) {
|
|
115
|
+
Object.defineProperty(this, property, { enumerable: false });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
get isPush() {
|
|
119
|
+
return this.type.startsWith("push");
|
|
120
|
+
}
|
|
121
|
+
// handle related getter/setter
|
|
122
|
+
get handle() {
|
|
123
|
+
return this._handle;
|
|
124
|
+
}
|
|
125
|
+
set handle(val) {
|
|
126
|
+
this.setHandle(val);
|
|
127
|
+
}
|
|
128
|
+
get handleResult() {
|
|
129
|
+
return this._handleResult;
|
|
130
|
+
}
|
|
131
|
+
set handleResult(val) {
|
|
132
|
+
this._handleResult = val;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Set handle function with validation logic wrapper
|
|
136
|
+
*/
|
|
137
|
+
setHandle(val) {
|
|
138
|
+
if (typeof val !== "function") {
|
|
139
|
+
this._handle = null;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const self = this;
|
|
143
|
+
this._handle = function handle(to, from, router) {
|
|
144
|
+
if (self._handled) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
"Route handle hook can only be called once per navigation"
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
self._handled = true;
|
|
150
|
+
return val.call(this, to, from, router);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Apply navigation-generated state to current route
|
|
155
|
+
* Used by route handlers to add system state like pageId
|
|
156
|
+
* @param navigationState Navigation-generated state to apply
|
|
157
|
+
*/
|
|
158
|
+
applyNavigationState(navigationState) {
|
|
159
|
+
Object.assign(this.state, navigationState);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Sync all properties of current route to target route object
|
|
163
|
+
* Used for route object updates in reactive systems
|
|
164
|
+
* @param targetRoute Target route object
|
|
165
|
+
*/
|
|
166
|
+
syncTo(targetRoute) {
|
|
167
|
+
Object.assign(targetRoute, this);
|
|
168
|
+
for (const property of NON_ENUMERABLE_PROPERTIES) {
|
|
169
|
+
if (!(property in this && property in targetRoute)) continue;
|
|
170
|
+
const value = Reflect.get(this, property);
|
|
171
|
+
Reflect.set(targetRoute, property, value);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Clone current route instance
|
|
176
|
+
* Returns a new Route instance with same configuration and state
|
|
177
|
+
*/
|
|
178
|
+
clone() {
|
|
179
|
+
const toInput = {
|
|
180
|
+
path: this.fullPath,
|
|
181
|
+
state: { ...this.state },
|
|
182
|
+
...this.confirm && { confirm: this.confirm },
|
|
183
|
+
...this.layer && { layer: this.layer },
|
|
184
|
+
...this.statusCode !== null && { statusCode: this.statusCode }
|
|
185
|
+
};
|
|
186
|
+
const options = this._options;
|
|
187
|
+
const clonedRoute = new Route({
|
|
188
|
+
options,
|
|
189
|
+
toType: this.type,
|
|
190
|
+
toInput
|
|
191
|
+
});
|
|
192
|
+
return clonedRoute;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|