wevu 6.7.2 → 6.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.mts +1 -0
- package/dist/api.mjs +2 -0
- package/dist/compiler.mjs +2 -3
- package/dist/defineProperty-JH0tR899.mjs +39 -0
- package/dist/fetch.d.mts +26 -0
- package/dist/fetch.mjs +345 -0
- package/dist/index-Ciz2Ye1L.d.mts +246 -0
- package/dist/index.d.mts +238 -410
- package/dist/index.mjs +1609 -2560
- package/dist/jsx-runtime.d.mts +1 -1
- package/dist/jsx-runtime.mjs +1 -1
- package/dist/ref-BeA_Is-2.mjs +804 -0
- package/dist/router-5qgy8oOS.mjs +329 -0
- package/dist/router-BxV0btOX.d.mts +46 -0
- package/dist/router.d.mts +192 -0
- package/dist/router.mjs +1412 -0
- package/dist/store-DFP_p2kt.mjs +504 -0
- package/dist/store.d.mts +2 -0
- package/dist/store.mjs +2 -0
- package/package.json +31 -2
- /package/dist/{weappIntrinsicElements-CihhL3Md.d.mts → weappIntrinsicElements-BKiRK0cC.d.mts} +0 -0
package/dist/router.mjs
ADDED
|
@@ -0,0 +1,1412 @@
|
|
|
1
|
+
import { d as reactive } from "./ref-BeA_Is-2.mjs";
|
|
2
|
+
import { L as readonly, S as onShow, j as getCurrentSetupContext, l as onLoad, n as useNativeRouter$1, t as useNativePageRouter$1, v as onRouteDone } from "./router-5qgy8oOS.mjs";
|
|
3
|
+
//#region src/routerInternal/path.ts
|
|
4
|
+
function normalizePathSegments(path) {
|
|
5
|
+
const segments = [];
|
|
6
|
+
for (const segment of path.split("/")) {
|
|
7
|
+
if (!segment || segment === ".") continue;
|
|
8
|
+
if (segment === "..") {
|
|
9
|
+
if (segments.length > 0) segments.pop();
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
segments.push(segment);
|
|
13
|
+
}
|
|
14
|
+
return segments;
|
|
15
|
+
}
|
|
16
|
+
function resolvePath(path, currentPath) {
|
|
17
|
+
if (!path) return normalizePathSegments(currentPath).join("/");
|
|
18
|
+
if (path.startsWith("/")) return normalizePathSegments(path).join("/");
|
|
19
|
+
if (path.startsWith("./") || path.startsWith("../")) {
|
|
20
|
+
const baseSegments = normalizePathSegments(currentPath);
|
|
21
|
+
if (baseSegments.length > 0) baseSegments.pop();
|
|
22
|
+
for (const segment of path.split("/")) {
|
|
23
|
+
if (!segment || segment === ".") continue;
|
|
24
|
+
if (segment === "..") {
|
|
25
|
+
if (baseSegments.length > 0) baseSegments.pop();
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
baseSegments.push(segment);
|
|
29
|
+
}
|
|
30
|
+
return baseSegments.join("/");
|
|
31
|
+
}
|
|
32
|
+
return normalizePathSegments(path).join("/");
|
|
33
|
+
}
|
|
34
|
+
function createAbsoluteRoutePath(path) {
|
|
35
|
+
return path ? `/${path}` : "/";
|
|
36
|
+
}
|
|
37
|
+
function isDynamicRoutePath(path) {
|
|
38
|
+
return /(?:^|\/):/.test(path);
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/routerInternal/query.ts
|
|
42
|
+
function decodeQuerySegment(value) {
|
|
43
|
+
return decodeURIComponent(value.replace(/\+/g, " "));
|
|
44
|
+
}
|
|
45
|
+
function encodeQuerySegment(value) {
|
|
46
|
+
return encodeURIComponent(value);
|
|
47
|
+
}
|
|
48
|
+
function pushQueryValue(target, key, value) {
|
|
49
|
+
const previous = target[key];
|
|
50
|
+
if (previous === void 0) {
|
|
51
|
+
target[key] = value;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(previous)) {
|
|
55
|
+
previous.push(value);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
target[key] = [previous, value];
|
|
59
|
+
}
|
|
60
|
+
function pushRouteParamValue(target, key, value) {
|
|
61
|
+
const previous = target[key];
|
|
62
|
+
if (previous === void 0) {
|
|
63
|
+
target[key] = value;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(previous)) {
|
|
67
|
+
previous.push(value);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
target[key] = [previous, value];
|
|
71
|
+
}
|
|
72
|
+
function normalizeQueryValue(value) {
|
|
73
|
+
if (value === void 0) return;
|
|
74
|
+
if (value === null) return null;
|
|
75
|
+
return String(value);
|
|
76
|
+
}
|
|
77
|
+
function normalizeQuery(query) {
|
|
78
|
+
const normalized = {};
|
|
79
|
+
for (const key of Object.keys(query)) {
|
|
80
|
+
const rawValue = query[key];
|
|
81
|
+
if (Array.isArray(rawValue)) {
|
|
82
|
+
for (const item of rawValue) {
|
|
83
|
+
const next = normalizeQueryValue(item);
|
|
84
|
+
if (next !== void 0) pushQueryValue(normalized, key, next);
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const next = normalizeQueryValue(rawValue);
|
|
89
|
+
if (next !== void 0) pushQueryValue(normalized, key, next);
|
|
90
|
+
}
|
|
91
|
+
return normalized;
|
|
92
|
+
}
|
|
93
|
+
function normalizeRouteParamValue(value) {
|
|
94
|
+
if (value === void 0 || value === null) return;
|
|
95
|
+
return String(value);
|
|
96
|
+
}
|
|
97
|
+
function normalizeRouteParams(params) {
|
|
98
|
+
const normalized = {};
|
|
99
|
+
for (const key of Object.keys(params)) {
|
|
100
|
+
const rawValue = params[key];
|
|
101
|
+
if (Array.isArray(rawValue)) {
|
|
102
|
+
for (const item of rawValue) {
|
|
103
|
+
const next = normalizeRouteParamValue(item);
|
|
104
|
+
if (next !== void 0) pushRouteParamValue(normalized, key, next);
|
|
105
|
+
}
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const next = normalizeRouteParamValue(rawValue);
|
|
109
|
+
if (next !== void 0) pushRouteParamValue(normalized, key, next);
|
|
110
|
+
}
|
|
111
|
+
return normalized;
|
|
112
|
+
}
|
|
113
|
+
function normalizeHash(rawHash) {
|
|
114
|
+
if (!rawHash) return "";
|
|
115
|
+
return rawHash.startsWith("#") ? rawHash : `#${rawHash}`;
|
|
116
|
+
}
|
|
117
|
+
function parseQuery(search) {
|
|
118
|
+
const normalizedSearch = search.startsWith("?") ? search.slice(1) : search;
|
|
119
|
+
const query = {};
|
|
120
|
+
if (!normalizedSearch) return query;
|
|
121
|
+
for (const segment of normalizedSearch.split("&")) {
|
|
122
|
+
if (!segment) continue;
|
|
123
|
+
const equalIndex = segment.indexOf("=");
|
|
124
|
+
const rawKey = equalIndex >= 0 ? segment.slice(0, equalIndex) : segment;
|
|
125
|
+
if (!rawKey) continue;
|
|
126
|
+
pushQueryValue(query, decodeQuerySegment(rawKey), equalIndex >= 0 ? decodeQuerySegment(segment.slice(equalIndex + 1)) : null);
|
|
127
|
+
}
|
|
128
|
+
return query;
|
|
129
|
+
}
|
|
130
|
+
function stringifyQuery(query = {}) {
|
|
131
|
+
const normalizedQuery = normalizeQuery(query);
|
|
132
|
+
const segments = [];
|
|
133
|
+
for (const key of Object.keys(normalizedQuery)) {
|
|
134
|
+
const encodedKey = encodeQuerySegment(key);
|
|
135
|
+
const rawValue = normalizedQuery[key];
|
|
136
|
+
if (Array.isArray(rawValue)) {
|
|
137
|
+
for (const item of rawValue) if (item === null) segments.push(encodedKey);
|
|
138
|
+
else segments.push(`${encodedKey}=${encodeQuerySegment(item)}`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (rawValue === null) {
|
|
142
|
+
segments.push(encodedKey);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
segments.push(`${encodedKey}=${encodeQuerySegment(rawValue)}`);
|
|
146
|
+
}
|
|
147
|
+
return segments.join("&");
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/routerInternal/location.ts
|
|
151
|
+
const DEFAULT_ROUTE_RESOLVE_CODEC$1 = {
|
|
152
|
+
parseQuery,
|
|
153
|
+
stringifyQuery
|
|
154
|
+
};
|
|
155
|
+
function parsePathInput(input, codec = DEFAULT_ROUTE_RESOLVE_CODEC$1) {
|
|
156
|
+
const hashIndex = input.indexOf("#");
|
|
157
|
+
const withoutHash = hashIndex >= 0 ? input.slice(0, hashIndex) : input;
|
|
158
|
+
const hash = hashIndex >= 0 ? normalizeHash(input.slice(hashIndex + 1)) : "";
|
|
159
|
+
const queryIndex = withoutHash.indexOf("?");
|
|
160
|
+
if (queryIndex < 0) return {
|
|
161
|
+
path: withoutHash,
|
|
162
|
+
query: {},
|
|
163
|
+
hash
|
|
164
|
+
};
|
|
165
|
+
return {
|
|
166
|
+
path: withoutHash.slice(0, queryIndex),
|
|
167
|
+
query: normalizeQuery(codec.parseQuery(withoutHash.slice(queryIndex + 1))),
|
|
168
|
+
hash
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function createRouteLocation(path, query, hash = "", name, params = {}, queryStringifier = stringifyQuery) {
|
|
172
|
+
const normalizedPath = resolvePath(path, "");
|
|
173
|
+
const queryString = queryStringifier(query);
|
|
174
|
+
const fullPathBase = createAbsoluteRoutePath(normalizedPath);
|
|
175
|
+
const normalizedHash = normalizeHash(hash);
|
|
176
|
+
const fullPath = queryString ? `${fullPathBase}?${queryString}` : fullPathBase;
|
|
177
|
+
const location = {
|
|
178
|
+
path: normalizedPath,
|
|
179
|
+
fullPath: normalizedHash ? `${fullPath}${normalizedHash}` : fullPath,
|
|
180
|
+
query,
|
|
181
|
+
hash: normalizedHash,
|
|
182
|
+
params
|
|
183
|
+
};
|
|
184
|
+
if (name !== void 0) location.name = name;
|
|
185
|
+
return location;
|
|
186
|
+
}
|
|
187
|
+
function getCurrentMiniProgramPage() {
|
|
188
|
+
const getCurrentPagesFn = globalThis.getCurrentPages;
|
|
189
|
+
if (typeof getCurrentPagesFn !== "function") return;
|
|
190
|
+
try {
|
|
191
|
+
const pages = getCurrentPagesFn();
|
|
192
|
+
if (!Array.isArray(pages) || pages.length === 0) return;
|
|
193
|
+
return pages[pages.length - 1];
|
|
194
|
+
} catch (_unused) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function resolveCurrentRoute(queryOverride) {
|
|
199
|
+
var _currentPage$options;
|
|
200
|
+
const currentPage = getCurrentMiniProgramPage();
|
|
201
|
+
if (!currentPage) return createRouteLocation("", {});
|
|
202
|
+
return createRouteLocation(typeof currentPage.route === "string" ? currentPage.route : typeof currentPage.__route__ === "string" ? currentPage.__route__ : "", normalizeQuery(queryOverride !== null && queryOverride !== void 0 ? queryOverride : (_currentPage$options = currentPage.options) !== null && _currentPage$options !== void 0 ? _currentPage$options : {}), "", void 0, {});
|
|
203
|
+
}
|
|
204
|
+
//#endregion
|
|
205
|
+
//#region src/routerInternal/params.ts
|
|
206
|
+
function parsePathParamToken(segment) {
|
|
207
|
+
var _match$;
|
|
208
|
+
const match = /^:(\w+)(?:\([^)]*\))?([+*?])?$/.exec(segment);
|
|
209
|
+
if (!match) return;
|
|
210
|
+
return {
|
|
211
|
+
key: match[1],
|
|
212
|
+
modifier: (_match$ = match[2]) !== null && _match$ !== void 0 ? _match$ : ""
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function normalizePathParamValues(value) {
|
|
216
|
+
if (value === void 0) return [];
|
|
217
|
+
if (Array.isArray(value)) return value;
|
|
218
|
+
return [value];
|
|
219
|
+
}
|
|
220
|
+
function stringifyPathParamSegment(value) {
|
|
221
|
+
return encodeURIComponent(value);
|
|
222
|
+
}
|
|
223
|
+
function decodeRouteParamSegment(value) {
|
|
224
|
+
try {
|
|
225
|
+
return decodeURIComponent(value);
|
|
226
|
+
} catch (_unused) {
|
|
227
|
+
return value;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function resolveNamedRoutePath(pathTemplate, params, routeName) {
|
|
231
|
+
const resolvedSegments = [];
|
|
232
|
+
const consumedKeys = /* @__PURE__ */ new Set();
|
|
233
|
+
for (const rawSegment of pathTemplate.split("/")) {
|
|
234
|
+
if (!rawSegment) continue;
|
|
235
|
+
const token = parsePathParamToken(rawSegment);
|
|
236
|
+
if (!token) {
|
|
237
|
+
resolvedSegments.push(rawSegment);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const values = normalizePathParamValues(params[token.key]);
|
|
241
|
+
if (token.modifier === "") {
|
|
242
|
+
if (values.length !== 1) throw new Error(`Missing required param "${token.key}" for named route "${routeName}"`);
|
|
243
|
+
consumedKeys.add(token.key);
|
|
244
|
+
resolvedSegments.push(stringifyPathParamSegment(values[0]));
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (token.modifier === "?") {
|
|
248
|
+
if (values.length === 0) continue;
|
|
249
|
+
if (values.length > 1) throw new Error(`Param "${token.key}" for named route "${routeName}" must be a single value`);
|
|
250
|
+
consumedKeys.add(token.key);
|
|
251
|
+
resolvedSegments.push(stringifyPathParamSegment(values[0]));
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (token.modifier === "+") {
|
|
255
|
+
if (values.length === 0) throw new Error(`Missing required repeatable param "${token.key}" for named route "${routeName}"`);
|
|
256
|
+
consumedKeys.add(token.key);
|
|
257
|
+
for (const value of values) resolvedSegments.push(stringifyPathParamSegment(value));
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (values.length > 0) consumedKeys.add(token.key);
|
|
261
|
+
for (const value of values) resolvedSegments.push(stringifyPathParamSegment(value));
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
path: resolvedSegments.join("/"),
|
|
265
|
+
consumedKeys
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function hasNonEmptyRouteParamValue(value) {
|
|
269
|
+
if (value === void 0) return false;
|
|
270
|
+
if (Array.isArray(value)) return value.length > 0;
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
function assertNoUnexpectedNamedRouteParams(params, consumedKeys, routeName) {
|
|
274
|
+
const unexpectedKeys = Object.keys(params).filter((key) => !consumedKeys.has(key)).filter((key) => hasNonEmptyRouteParamValue(params[key]));
|
|
275
|
+
if (unexpectedKeys.length === 0) return;
|
|
276
|
+
throw new Error(`Unexpected params for named route "${routeName}": ${unexpectedKeys.join(", ")}`);
|
|
277
|
+
}
|
|
278
|
+
function resolveNamedRouteLocation(to, lookup, paramsMode) {
|
|
279
|
+
const routeName = to.name;
|
|
280
|
+
if (typeof routeName !== "string" || !routeName) return to;
|
|
281
|
+
if (to.path !== void 0 || to.fullPath !== void 0) return to;
|
|
282
|
+
const routeRecord = lookup.recordByName.get(routeName);
|
|
283
|
+
if (!routeRecord) throw new Error(`Named route "${routeName}" is not defined in useRouter({ routes | namedRoutes })`);
|
|
284
|
+
const params = to.params ? normalizeRouteParams(to.params) : {};
|
|
285
|
+
const resolvedResult = resolveNamedRoutePath(routeRecord.path, params, routeName);
|
|
286
|
+
if (paramsMode === "strict") assertNoUnexpectedNamedRouteParams(params, resolvedResult.consumedKeys, routeName);
|
|
287
|
+
return {
|
|
288
|
+
...to,
|
|
289
|
+
path: resolvedResult.path ? `/${resolvedResult.path}` : "/",
|
|
290
|
+
params
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/routerInternal/matching.ts
|
|
295
|
+
function splitRoutePathSegments(path) {
|
|
296
|
+
return path.split("/").filter(Boolean);
|
|
297
|
+
}
|
|
298
|
+
function listRouteRecordMatchPaths(record) {
|
|
299
|
+
return [record.path, ...record.aliasPaths];
|
|
300
|
+
}
|
|
301
|
+
function resolveMatchedRouteRecordChain(record, lookup) {
|
|
302
|
+
const matchedRecords = [];
|
|
303
|
+
const visitedNames = /* @__PURE__ */ new Set();
|
|
304
|
+
let currentRecord = record;
|
|
305
|
+
while (currentRecord) {
|
|
306
|
+
if (visitedNames.has(currentRecord.name)) break;
|
|
307
|
+
visitedNames.add(currentRecord.name);
|
|
308
|
+
matchedRecords.unshift(currentRecord);
|
|
309
|
+
if (!currentRecord.parentName) break;
|
|
310
|
+
currentRecord = lookup.recordByName.get(currentRecord.parentName);
|
|
311
|
+
}
|
|
312
|
+
return matchedRecords;
|
|
313
|
+
}
|
|
314
|
+
function mergeMatchedRouteMeta(matchedRecords) {
|
|
315
|
+
let mergedRouteMeta;
|
|
316
|
+
for (const matchedRecord of matchedRecords) {
|
|
317
|
+
if (!matchedRecord.meta) continue;
|
|
318
|
+
if (!mergedRouteMeta) mergedRouteMeta = {};
|
|
319
|
+
Object.assign(mergedRouteMeta, matchedRecord.meta);
|
|
320
|
+
}
|
|
321
|
+
return mergedRouteMeta;
|
|
322
|
+
}
|
|
323
|
+
function collectRouteNamesForRemoval(routeName, recordByName) {
|
|
324
|
+
const namesToRemove = /* @__PURE__ */ new Set();
|
|
325
|
+
if (!recordByName.has(routeName)) return namesToRemove;
|
|
326
|
+
namesToRemove.add(routeName);
|
|
327
|
+
let expanded = true;
|
|
328
|
+
while (expanded) {
|
|
329
|
+
expanded = false;
|
|
330
|
+
for (const [name, record] of recordByName.entries()) {
|
|
331
|
+
if (!record.parentName || namesToRemove.has(name)) continue;
|
|
332
|
+
if (namesToRemove.has(record.parentName)) {
|
|
333
|
+
namesToRemove.add(name);
|
|
334
|
+
expanded = true;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return namesToRemove;
|
|
339
|
+
}
|
|
340
|
+
function buildRouteParamsFromMatch(matchValues) {
|
|
341
|
+
const params = {};
|
|
342
|
+
for (const [key, values] of matchValues.entries()) {
|
|
343
|
+
if (values.length === 0) continue;
|
|
344
|
+
params[key] = values.length === 1 ? values[0] : values;
|
|
345
|
+
}
|
|
346
|
+
return params;
|
|
347
|
+
}
|
|
348
|
+
function matchRoutePathParams(pathTemplate, targetPath) {
|
|
349
|
+
const templateSegments = splitRoutePathSegments(pathTemplate);
|
|
350
|
+
const targetSegments = splitRoutePathSegments(targetPath);
|
|
351
|
+
const matchedValues = /* @__PURE__ */ new Map();
|
|
352
|
+
const pushValue = (key, value) => {
|
|
353
|
+
const previous = matchedValues.get(key);
|
|
354
|
+
if (previous) {
|
|
355
|
+
previous.push(value);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
matchedValues.set(key, [value]);
|
|
359
|
+
};
|
|
360
|
+
const popValue = (key) => {
|
|
361
|
+
const previous = matchedValues.get(key);
|
|
362
|
+
if (!previous || previous.length === 0) return;
|
|
363
|
+
previous.pop();
|
|
364
|
+
if (previous.length === 0) matchedValues.delete(key);
|
|
365
|
+
};
|
|
366
|
+
const consumeRepeatableToken = (key, startIndex, count) => {
|
|
367
|
+
for (let index = 0; index < count; index += 1) pushValue(key, decodeRouteParamSegment(targetSegments[startIndex + index]));
|
|
368
|
+
return () => {
|
|
369
|
+
for (let index = 0; index < count; index += 1) popValue(key);
|
|
370
|
+
};
|
|
371
|
+
};
|
|
372
|
+
const matchRecursively = (templateIndex, targetIndex) => {
|
|
373
|
+
if (templateIndex >= templateSegments.length) return targetIndex >= targetSegments.length;
|
|
374
|
+
const templateSegment = templateSegments[templateIndex];
|
|
375
|
+
const token = parsePathParamToken(templateSegment);
|
|
376
|
+
if (!token) {
|
|
377
|
+
if (targetIndex >= targetSegments.length || templateSegment !== targetSegments[targetIndex]) return false;
|
|
378
|
+
return matchRecursively(templateIndex + 1, targetIndex + 1);
|
|
379
|
+
}
|
|
380
|
+
if (token.modifier === "") {
|
|
381
|
+
if (targetIndex >= targetSegments.length) return false;
|
|
382
|
+
pushValue(token.key, decodeRouteParamSegment(targetSegments[targetIndex]));
|
|
383
|
+
const matched = matchRecursively(templateIndex + 1, targetIndex + 1);
|
|
384
|
+
if (!matched) popValue(token.key);
|
|
385
|
+
return matched;
|
|
386
|
+
}
|
|
387
|
+
if (token.modifier === "?") {
|
|
388
|
+
if (targetIndex < targetSegments.length) {
|
|
389
|
+
pushValue(token.key, decodeRouteParamSegment(targetSegments[targetIndex]));
|
|
390
|
+
if (matchRecursively(templateIndex + 1, targetIndex + 1)) return true;
|
|
391
|
+
popValue(token.key);
|
|
392
|
+
}
|
|
393
|
+
return matchRecursively(templateIndex + 1, targetIndex);
|
|
394
|
+
}
|
|
395
|
+
const minimumCount = token.modifier === "+" ? 1 : 0;
|
|
396
|
+
const maximumCount = targetSegments.length - targetIndex;
|
|
397
|
+
for (let count = maximumCount; count >= minimumCount; count -= 1) {
|
|
398
|
+
const rollback = consumeRepeatableToken(token.key, targetIndex, count);
|
|
399
|
+
if (matchRecursively(templateIndex + 1, targetIndex + count)) return true;
|
|
400
|
+
rollback();
|
|
401
|
+
}
|
|
402
|
+
return false;
|
|
403
|
+
};
|
|
404
|
+
if (!matchRecursively(0, 0)) return;
|
|
405
|
+
return buildRouteParamsFromMatch(matchedValues);
|
|
406
|
+
}
|
|
407
|
+
function resolveMatchedRouteRecord(target, lookup) {
|
|
408
|
+
if (target.name) {
|
|
409
|
+
const byName = lookup.recordByName.get(target.name);
|
|
410
|
+
if (byName) return {
|
|
411
|
+
record: byName,
|
|
412
|
+
matchedRecords: resolveMatchedRouteRecordChain(byName, lookup)
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
const staticNamedRoute = lookup.nameByStaticPath.get(target.path);
|
|
416
|
+
if (staticNamedRoute) {
|
|
417
|
+
const record = lookup.recordByName.get(staticNamedRoute);
|
|
418
|
+
if (record) return {
|
|
419
|
+
record,
|
|
420
|
+
matchedRecords: resolveMatchedRouteRecordChain(record, lookup),
|
|
421
|
+
matchedPath: target.path
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
for (const record of lookup.recordByName.values()) for (const routePath of listRouteRecordMatchPaths(record)) {
|
|
425
|
+
if (!isDynamicRoutePath(routePath)) continue;
|
|
426
|
+
const matchedParams = matchRoutePathParams(routePath, target.path);
|
|
427
|
+
if (!matchedParams) continue;
|
|
428
|
+
return {
|
|
429
|
+
record,
|
|
430
|
+
matchedRecords: resolveMatchedRouteRecordChain(record, lookup),
|
|
431
|
+
matchedPath: routePath,
|
|
432
|
+
params: matchedParams
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
//#endregion
|
|
437
|
+
//#region src/routerInternal/recordsWarnings.ts
|
|
438
|
+
function warnRouteConfig(message) {
|
|
439
|
+
if (typeof console === "undefined" || typeof console.warn !== "function") return;
|
|
440
|
+
console.warn(`[wevu/router] ${message}`);
|
|
441
|
+
}
|
|
442
|
+
function formatRoutePathForWarning(path) {
|
|
443
|
+
const normalizedPath = resolvePath(path, "");
|
|
444
|
+
if (!normalizedPath) return "/";
|
|
445
|
+
return createAbsoluteRoutePath(normalizedPath);
|
|
446
|
+
}
|
|
447
|
+
function hasCircularChildrenReference(routeRecord, ancestorRecords = /* @__PURE__ */ new Set()) {
|
|
448
|
+
if (ancestorRecords.has(routeRecord)) return true;
|
|
449
|
+
if (!Array.isArray(routeRecord.children) || routeRecord.children.length === 0) return false;
|
|
450
|
+
const nextAncestorRecords = new Set(ancestorRecords);
|
|
451
|
+
nextAncestorRecords.add(routeRecord);
|
|
452
|
+
for (const childRecord of routeRecord.children) if (hasCircularChildrenReference(childRecord, nextAncestorRecords)) return true;
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
function assertValidAddRouteInput(routeRecord) {
|
|
456
|
+
const routeName = typeof routeRecord.name === "string" ? routeRecord.name.trim() : "";
|
|
457
|
+
if (!routeName) throw new Error("Route name is required when adding a named route");
|
|
458
|
+
if (typeof routeRecord.path !== "string" || !routeRecord.path) throw new Error(`Route path is required when adding named route "${routeName}"`);
|
|
459
|
+
if (hasCircularChildrenReference(routeRecord)) throw new Error(`Circular children reference detected when adding named route "${routeName}"`);
|
|
460
|
+
}
|
|
461
|
+
function warnDuplicateRouteEntries(routeEntries) {
|
|
462
|
+
const latestRouteInfoByName = /* @__PURE__ */ new Map();
|
|
463
|
+
const duplicateMessages = [];
|
|
464
|
+
for (const routeEntry of routeEntries) {
|
|
465
|
+
var _routeEntry$source;
|
|
466
|
+
const routeName = routeEntry.route.name.trim();
|
|
467
|
+
if (!routeName) continue;
|
|
468
|
+
const currentSource = (_routeEntry$source = routeEntry.source) !== null && _routeEntry$source !== void 0 ? _routeEntry$source : "namedRoutes";
|
|
469
|
+
const currentPath = formatRoutePathForWarning(routeEntry.route.path);
|
|
470
|
+
const previousInfo = latestRouteInfoByName.get(routeName);
|
|
471
|
+
if (previousInfo) duplicateMessages.push(`"${routeName}" (${previousInfo.source}:${previousInfo.path} -> ${currentSource}:${currentPath})`);
|
|
472
|
+
latestRouteInfoByName.set(routeName, {
|
|
473
|
+
source: currentSource,
|
|
474
|
+
path: currentPath
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
if (duplicateMessages.length === 0) return;
|
|
478
|
+
warnRouteConfig(`duplicate route names detected, later entries override earlier ones: ${duplicateMessages.join(", ")}`);
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region src/routerInternal/records.ts
|
|
482
|
+
function normalizeBeforeEnterGuards(beforeEnter) {
|
|
483
|
+
if (!beforeEnter) return [];
|
|
484
|
+
if (Array.isArray(beforeEnter)) return beforeEnter;
|
|
485
|
+
return [beforeEnter];
|
|
486
|
+
}
|
|
487
|
+
function normalizeRouteRecordRaw(route, parentName) {
|
|
488
|
+
const routeName = route.name.trim();
|
|
489
|
+
if (!routeName) return;
|
|
490
|
+
const normalizedPath = resolvePath(route.path, "");
|
|
491
|
+
if (!normalizedPath) return;
|
|
492
|
+
return {
|
|
493
|
+
name: routeName,
|
|
494
|
+
path: normalizedPath,
|
|
495
|
+
aliasPaths: normalizeRouteRecordAliasPaths(route.alias, normalizedPath),
|
|
496
|
+
parentName,
|
|
497
|
+
meta: route.meta,
|
|
498
|
+
beforeEnterGuards: normalizeBeforeEnterGuards(route.beforeEnter),
|
|
499
|
+
redirect: route.redirect
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
function normalizeRouteRecordAliasPaths(alias, normalizedPath) {
|
|
503
|
+
if (!alias) return [];
|
|
504
|
+
const aliasList = Array.isArray(alias) ? alias : [alias];
|
|
505
|
+
const normalizedAliasPaths = [];
|
|
506
|
+
for (const aliasPath of aliasList) {
|
|
507
|
+
if (typeof aliasPath !== "string") continue;
|
|
508
|
+
const normalizedAliasPath = resolvePath(aliasPath, "");
|
|
509
|
+
if (!normalizedAliasPath || normalizedAliasPath === normalizedPath) continue;
|
|
510
|
+
if (!normalizedAliasPaths.includes(normalizedAliasPath)) normalizedAliasPaths.push(normalizedAliasPath);
|
|
511
|
+
}
|
|
512
|
+
return normalizedAliasPaths;
|
|
513
|
+
}
|
|
514
|
+
function normalizeNamedRouteEntries(namedRoutes, source = "namedRoutes") {
|
|
515
|
+
if (!namedRoutes) return [];
|
|
516
|
+
if (Array.isArray(namedRoutes)) return flattenNamedRouteRecords(namedRoutes, void 0, void 0, [], source, source);
|
|
517
|
+
const normalizedEntries = [];
|
|
518
|
+
for (const [rawName, rawPath] of Object.entries(namedRoutes)) {
|
|
519
|
+
const routeName = typeof rawName === "string" ? rawName.trim() : "";
|
|
520
|
+
if (!routeName) {
|
|
521
|
+
warnRouteConfig(`ignored route record at ${source}: route name is required`);
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
if (typeof rawPath !== "string" || !rawPath) {
|
|
525
|
+
warnRouteConfig(`ignored route record "${routeName}" at ${source}: route path is required`);
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
normalizedEntries.push({
|
|
529
|
+
route: {
|
|
530
|
+
name: routeName,
|
|
531
|
+
path: rawPath
|
|
532
|
+
},
|
|
533
|
+
source
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
return normalizedEntries;
|
|
537
|
+
}
|
|
538
|
+
function resolveRouteOptionEntries(options) {
|
|
539
|
+
return [...normalizeNamedRouteEntries(options.routes, "routes"), ...normalizeNamedRouteEntries(options.namedRoutes, "namedRoutes")];
|
|
540
|
+
}
|
|
541
|
+
function normalizeNestedRoutePath(path, parentPath) {
|
|
542
|
+
if (!parentPath || path.startsWith("/")) return resolvePath(path, "");
|
|
543
|
+
return resolvePath(`${createAbsoluteRoutePath(parentPath)}/${path}`, "");
|
|
544
|
+
}
|
|
545
|
+
function normalizeAliasInputList(alias) {
|
|
546
|
+
if (!alias) return [];
|
|
547
|
+
if (Array.isArray(alias)) return alias.filter((item) => typeof item === "string" && item.length > 0);
|
|
548
|
+
return typeof alias === "string" && alias.length > 0 ? [alias] : [];
|
|
549
|
+
}
|
|
550
|
+
function mergeAliasPaths(aliasPaths) {
|
|
551
|
+
const mergedAliasPaths = [];
|
|
552
|
+
for (const aliasPath of aliasPaths) {
|
|
553
|
+
if (!aliasPath || mergedAliasPaths.includes(aliasPath)) continue;
|
|
554
|
+
mergedAliasPaths.push(aliasPath);
|
|
555
|
+
}
|
|
556
|
+
return mergedAliasPaths;
|
|
557
|
+
}
|
|
558
|
+
function createRouteRecordAliasValue(aliasPaths) {
|
|
559
|
+
if (aliasPaths.length === 0) return;
|
|
560
|
+
if (aliasPaths.length === 1) return createAbsoluteRoutePath(aliasPaths[0]);
|
|
561
|
+
return aliasPaths.map((aliasPath) => createAbsoluteRoutePath(aliasPath));
|
|
562
|
+
}
|
|
563
|
+
function flattenNamedRouteRecords(records, parentPath, parentName, parentAliasPaths = [], source, pathPrefix = "namedRoutes", ancestorRecords = /* @__PURE__ */ new Set()) {
|
|
564
|
+
const flattenedRecords = [];
|
|
565
|
+
for (const [index, record] of records.entries()) {
|
|
566
|
+
const routeConfigPath = `${pathPrefix}[${index}]`;
|
|
567
|
+
if (ancestorRecords.has(record)) {
|
|
568
|
+
warnRouteConfig(`ignored route record at ${routeConfigPath}: detected circular children reference`);
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
const routeName = typeof (record === null || record === void 0 ? void 0 : record.name) === "string" ? record.name.trim() : "";
|
|
572
|
+
if (!routeName) {
|
|
573
|
+
warnRouteConfig(`ignored route record at ${routeConfigPath}: route name is required`);
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
if (typeof record.path !== "string" || !record.path) {
|
|
577
|
+
warnRouteConfig(`ignored route record "${routeName}" at ${routeConfigPath}: route path is required`);
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
const normalizedPath = normalizeNestedRoutePath(record.path, parentPath);
|
|
581
|
+
const normalizedDirectAliasPaths = [];
|
|
582
|
+
const directAliasByNormalizedPath = /* @__PURE__ */ new Map();
|
|
583
|
+
for (const rawAliasPath of normalizeAliasInputList(record.alias)) {
|
|
584
|
+
const normalizedAliasPath = normalizeNestedRoutePath(rawAliasPath, parentPath);
|
|
585
|
+
if (!normalizedAliasPath) continue;
|
|
586
|
+
if (normalizedAliasPath === normalizedPath) {
|
|
587
|
+
warnRouteConfig(`ignored alias "${createAbsoluteRoutePath(normalizedAliasPath)}" for route "${routeName}" at ${routeConfigPath}: alias is same as route path`);
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
const duplicateAliasPath = directAliasByNormalizedPath.get(normalizedAliasPath);
|
|
591
|
+
if (duplicateAliasPath) {
|
|
592
|
+
warnRouteConfig(`ignored duplicate alias "${createAbsoluteRoutePath(normalizedAliasPath)}" for route "${routeName}" at ${routeConfigPath}: already declared by "${duplicateAliasPath}"`);
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
directAliasByNormalizedPath.set(normalizedAliasPath, rawAliasPath);
|
|
596
|
+
normalizedDirectAliasPaths.push(normalizedAliasPath);
|
|
597
|
+
}
|
|
598
|
+
const normalizedInheritedAliasPaths = record.path.startsWith("/") ? [] : parentAliasPaths.map((parentAliasPath) => normalizeNestedRoutePath(record.path, parentAliasPath)).filter(Boolean).filter((aliasPath) => aliasPath !== normalizedPath);
|
|
599
|
+
const normalizedAliasPaths = mergeAliasPaths([...normalizedDirectAliasPaths, ...normalizedInheritedAliasPaths]);
|
|
600
|
+
const normalizedRecord = {
|
|
601
|
+
...record,
|
|
602
|
+
name: routeName,
|
|
603
|
+
path: normalizedPath ? createAbsoluteRoutePath(normalizedPath) : "/",
|
|
604
|
+
alias: createRouteRecordAliasValue(normalizedAliasPaths)
|
|
605
|
+
};
|
|
606
|
+
flattenedRecords.push({
|
|
607
|
+
route: normalizedRecord,
|
|
608
|
+
parentName,
|
|
609
|
+
source
|
|
610
|
+
});
|
|
611
|
+
if (Array.isArray(record.children) && record.children.length > 0) {
|
|
612
|
+
const nextAncestorRecords = new Set(ancestorRecords);
|
|
613
|
+
nextAncestorRecords.add(record);
|
|
614
|
+
flattenedRecords.push(...flattenNamedRouteRecords(record.children, normalizedPath, routeName, normalizedAliasPaths, source, `${routeConfigPath}.children`, nextAncestorRecords));
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return flattenedRecords;
|
|
618
|
+
}
|
|
619
|
+
function createNamedRouteNameByStaticPath(recordByName) {
|
|
620
|
+
const nameByStaticPath = /* @__PURE__ */ new Map();
|
|
621
|
+
for (const [name, record] of recordByName.entries()) {
|
|
622
|
+
if (!isDynamicRoutePath(record.path) && !nameByStaticPath.has(record.path)) nameByStaticPath.set(record.path, name);
|
|
623
|
+
for (const aliasPath of record.aliasPaths) if (!isDynamicRoutePath(aliasPath) && !nameByStaticPath.has(aliasPath)) nameByStaticPath.set(aliasPath, name);
|
|
624
|
+
}
|
|
625
|
+
return nameByStaticPath;
|
|
626
|
+
}
|
|
627
|
+
function createNamedRouteLookup(routeEntries) {
|
|
628
|
+
const recordByName = /* @__PURE__ */ new Map();
|
|
629
|
+
for (const routeRecord of routeEntries) {
|
|
630
|
+
const normalizedRecord = normalizeRouteRecordRaw(routeRecord.route, routeRecord.parentName);
|
|
631
|
+
if (!normalizedRecord) continue;
|
|
632
|
+
recordByName.set(normalizedRecord.name, normalizedRecord);
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
recordByName,
|
|
636
|
+
nameByStaticPath: createNamedRouteNameByStaticPath(recordByName)
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
//#endregion
|
|
640
|
+
//#region src/routerInternal/snapshots.ts
|
|
641
|
+
function cloneRouteMeta(meta) {
|
|
642
|
+
if (!meta) return;
|
|
643
|
+
return { ...meta };
|
|
644
|
+
}
|
|
645
|
+
function freezeRouteRecordRawSnapshot(routeRecord) {
|
|
646
|
+
const snapshot = {
|
|
647
|
+
name: routeRecord.name,
|
|
648
|
+
path: routeRecord.path
|
|
649
|
+
};
|
|
650
|
+
if (routeRecord.meta !== void 0) snapshot.meta = cloneRouteMeta(routeRecord.meta);
|
|
651
|
+
if (Array.isArray(routeRecord.alias)) snapshot.alias = Object.freeze(routeRecord.alias.slice());
|
|
652
|
+
else if (routeRecord.alias !== void 0) snapshot.alias = routeRecord.alias;
|
|
653
|
+
if (Array.isArray(routeRecord.beforeEnter)) snapshot.beforeEnter = Object.freeze(routeRecord.beforeEnter.slice());
|
|
654
|
+
else if (routeRecord.beforeEnter !== void 0) snapshot.beforeEnter = routeRecord.beforeEnter;
|
|
655
|
+
if (routeRecord.redirect !== void 0) snapshot.redirect = routeRecord.redirect;
|
|
656
|
+
return Object.freeze(snapshot);
|
|
657
|
+
}
|
|
658
|
+
function createRouterOptionsSnapshot(normalizedTabBarEntries, normalizedNamedRoutes, paramsMode, maxRedirects, routeResolveCodec, rejectOnError) {
|
|
659
|
+
const tabBarEntriesSnapshot = Object.freeze(normalizedTabBarEntries.map((path) => createAbsoluteRoutePath(path)));
|
|
660
|
+
const routesSnapshot = Object.freeze(normalizedNamedRoutes.map(freezeRouteRecordRawSnapshot));
|
|
661
|
+
return Object.freeze({
|
|
662
|
+
tabBarEntries: tabBarEntriesSnapshot,
|
|
663
|
+
routes: routesSnapshot,
|
|
664
|
+
namedRoutes: routesSnapshot,
|
|
665
|
+
paramsMode,
|
|
666
|
+
maxRedirects,
|
|
667
|
+
parseQuery: routeResolveCodec.parseQuery,
|
|
668
|
+
stringifyQuery: routeResolveCodec.stringifyQuery,
|
|
669
|
+
rejectOnError
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
function normalizeRouteRecordMatched(record, matchedPath) {
|
|
673
|
+
const matchedRecord = {
|
|
674
|
+
name: record.name,
|
|
675
|
+
path: createAbsoluteRoutePath(record.path)
|
|
676
|
+
};
|
|
677
|
+
const normalizedMatchedPath = typeof matchedPath === "string" && matchedPath ? createAbsoluteRoutePath(resolvePath(matchedPath, "")) : void 0;
|
|
678
|
+
if (normalizedMatchedPath && normalizedMatchedPath !== matchedRecord.path) matchedRecord.aliasPath = normalizedMatchedPath;
|
|
679
|
+
if (record.meta !== void 0) matchedRecord.meta = cloneRouteMeta(record.meta);
|
|
680
|
+
return matchedRecord;
|
|
681
|
+
}
|
|
682
|
+
function cloneRouteRecordMatchedList(matched) {
|
|
683
|
+
if (!matched) return;
|
|
684
|
+
return matched.map((record) => ({
|
|
685
|
+
name: record.name,
|
|
686
|
+
path: record.path,
|
|
687
|
+
aliasPath: record.aliasPath,
|
|
688
|
+
meta: cloneRouteMeta(record.meta)
|
|
689
|
+
}));
|
|
690
|
+
}
|
|
691
|
+
function cloneLocationQuery(query) {
|
|
692
|
+
const cloned = {};
|
|
693
|
+
for (const key of Object.keys(query)) {
|
|
694
|
+
const value = query[key];
|
|
695
|
+
cloned[key] = Array.isArray(value) ? value.slice() : value;
|
|
696
|
+
}
|
|
697
|
+
return cloned;
|
|
698
|
+
}
|
|
699
|
+
function cloneRouteParams(params) {
|
|
700
|
+
const cloned = {};
|
|
701
|
+
for (const key of Object.keys(params)) {
|
|
702
|
+
const value = params[key];
|
|
703
|
+
cloned[key] = Array.isArray(value) ? value.slice() : value;
|
|
704
|
+
}
|
|
705
|
+
return cloned;
|
|
706
|
+
}
|
|
707
|
+
function cloneRouteLocationRedirectedFrom(redirectedFrom) {
|
|
708
|
+
if (!redirectedFrom) return;
|
|
709
|
+
const cloned = {
|
|
710
|
+
path: redirectedFrom.path,
|
|
711
|
+
fullPath: redirectedFrom.fullPath,
|
|
712
|
+
query: cloneLocationQuery(redirectedFrom.query),
|
|
713
|
+
hash: redirectedFrom.hash,
|
|
714
|
+
params: cloneRouteParams(redirectedFrom.params)
|
|
715
|
+
};
|
|
716
|
+
if (redirectedFrom.name !== void 0) cloned.name = redirectedFrom.name;
|
|
717
|
+
if (redirectedFrom.meta !== void 0) cloned.meta = cloneRouteMeta(redirectedFrom.meta);
|
|
718
|
+
if (redirectedFrom.href !== void 0) cloned.href = redirectedFrom.href;
|
|
719
|
+
if (redirectedFrom.matched !== void 0) cloned.matched = cloneRouteRecordMatchedList(redirectedFrom.matched);
|
|
720
|
+
return cloned;
|
|
721
|
+
}
|
|
722
|
+
function createRedirectedFromSnapshot(route) {
|
|
723
|
+
const snapshot = {
|
|
724
|
+
path: route.path,
|
|
725
|
+
fullPath: route.fullPath,
|
|
726
|
+
query: cloneLocationQuery(route.query),
|
|
727
|
+
hash: route.hash,
|
|
728
|
+
params: cloneRouteParams(route.params)
|
|
729
|
+
};
|
|
730
|
+
if (route.name !== void 0) snapshot.name = route.name;
|
|
731
|
+
if (route.meta !== void 0) snapshot.meta = cloneRouteMeta(route.meta);
|
|
732
|
+
if (route.href !== void 0) snapshot.href = route.href;
|
|
733
|
+
if (route.matched !== void 0) snapshot.matched = cloneRouteRecordMatchedList(route.matched);
|
|
734
|
+
return snapshot;
|
|
735
|
+
}
|
|
736
|
+
function snapshotRouteLocation(route) {
|
|
737
|
+
const snapshot = {
|
|
738
|
+
path: route.path,
|
|
739
|
+
fullPath: route.fullPath,
|
|
740
|
+
query: cloneLocationQuery(route.query),
|
|
741
|
+
hash: route.hash,
|
|
742
|
+
params: cloneRouteParams(route.params)
|
|
743
|
+
};
|
|
744
|
+
if (route.meta !== void 0) snapshot.meta = cloneRouteMeta(route.meta);
|
|
745
|
+
if (route.href !== void 0) snapshot.href = route.href;
|
|
746
|
+
if (route.matched !== void 0) snapshot.matched = cloneRouteRecordMatchedList(route.matched);
|
|
747
|
+
if (route.redirectedFrom !== void 0) snapshot.redirectedFrom = cloneRouteLocationRedirectedFrom(route.redirectedFrom);
|
|
748
|
+
if (route.name !== void 0) snapshot.name = route.name;
|
|
749
|
+
return snapshot;
|
|
750
|
+
}
|
|
751
|
+
function createNativeRouteUrl(target, queryStringifier = stringifyQuery) {
|
|
752
|
+
const basePath = createAbsoluteRoutePath(target.path);
|
|
753
|
+
const queryString = queryStringifier(target.query);
|
|
754
|
+
return queryString ? `${basePath}?${queryString}` : basePath;
|
|
755
|
+
}
|
|
756
|
+
function hasLocationQuery(query) {
|
|
757
|
+
return Object.keys(query).length > 0;
|
|
758
|
+
}
|
|
759
|
+
//#endregion
|
|
760
|
+
//#region src/router/types.ts
|
|
761
|
+
const NavigationFailureType = {
|
|
762
|
+
unknown: 1,
|
|
763
|
+
aborted: 4,
|
|
764
|
+
cancelled: 8,
|
|
765
|
+
duplicated: 16
|
|
766
|
+
};
|
|
767
|
+
//#endregion
|
|
768
|
+
//#region src/router/navigationCore.ts
|
|
769
|
+
function normalizeNavigationErrorMessage(error) {
|
|
770
|
+
if (typeof error === "string") return error;
|
|
771
|
+
if (error && typeof error === "object") {
|
|
772
|
+
const message = error.errMsg;
|
|
773
|
+
if (typeof message === "string") return message;
|
|
774
|
+
if (error instanceof Error) return error.message;
|
|
775
|
+
}
|
|
776
|
+
return "";
|
|
777
|
+
}
|
|
778
|
+
function resolveNavigationFailureType(error) {
|
|
779
|
+
const normalizedMessage = normalizeNavigationErrorMessage(error).toLowerCase();
|
|
780
|
+
if (/already|same|duplicat|重复/.test(normalizedMessage)) return NavigationFailureType.duplicated;
|
|
781
|
+
if (/cancel|取消/.test(normalizedMessage)) return NavigationFailureType.cancelled;
|
|
782
|
+
if (/abort|interrupt|中断/.test(normalizedMessage)) return NavigationFailureType.aborted;
|
|
783
|
+
return NavigationFailureType.unknown;
|
|
784
|
+
}
|
|
785
|
+
function createNavigationFailure(type, to, from, cause) {
|
|
786
|
+
const message = normalizeNavigationErrorMessage(cause) || "Navigation failed";
|
|
787
|
+
const error = new Error(message);
|
|
788
|
+
error.__wevuNavigationFailure = true;
|
|
789
|
+
error.type = type;
|
|
790
|
+
error.to = to;
|
|
791
|
+
error.from = from;
|
|
792
|
+
error.cause = cause;
|
|
793
|
+
return error;
|
|
794
|
+
}
|
|
795
|
+
function isNavigationFailure(error, type) {
|
|
796
|
+
if (!error || typeof error !== "object") return false;
|
|
797
|
+
const navigationError = error;
|
|
798
|
+
if (navigationError.__wevuNavigationFailure !== true) return false;
|
|
799
|
+
if (type === void 0) return true;
|
|
800
|
+
return navigationError.type === type;
|
|
801
|
+
}
|
|
802
|
+
function isNavigationRedirectCandidate(value) {
|
|
803
|
+
if (!value || typeof value !== "object") return false;
|
|
804
|
+
return "to" in value;
|
|
805
|
+
}
|
|
806
|
+
function isRouteLocationRawCandidate(value) {
|
|
807
|
+
if (typeof value === "string") return true;
|
|
808
|
+
if (!value || typeof value !== "object") return false;
|
|
809
|
+
const candidate = value;
|
|
810
|
+
return "path" in candidate || "fullPath" in candidate || "query" in candidate || "hash" in candidate || "name" in candidate || "params" in candidate;
|
|
811
|
+
}
|
|
812
|
+
async function runNavigationGuards(guards, context, resolveRoute) {
|
|
813
|
+
for (const guard of guards) try {
|
|
814
|
+
const result = await guard(context.to, context.from, context);
|
|
815
|
+
if (isNavigationFailure(result)) return {
|
|
816
|
+
status: "failure",
|
|
817
|
+
failure: result
|
|
818
|
+
};
|
|
819
|
+
if (result === false) return {
|
|
820
|
+
status: "failure",
|
|
821
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, "Navigation aborted by guard")
|
|
822
|
+
};
|
|
823
|
+
if (isNavigationRedirectCandidate(result)) {
|
|
824
|
+
if (!context.to) return {
|
|
825
|
+
status: "failure",
|
|
826
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, "Redirect is not supported in back navigation guards")
|
|
827
|
+
};
|
|
828
|
+
return {
|
|
829
|
+
status: "redirect",
|
|
830
|
+
target: resolveRoute(result.to, context.to.path),
|
|
831
|
+
replace: result.replace
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
if (isRouteLocationRawCandidate(result)) {
|
|
835
|
+
if (!context.to) return {
|
|
836
|
+
status: "failure",
|
|
837
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, "Redirect is not supported in back navigation guards")
|
|
838
|
+
};
|
|
839
|
+
return {
|
|
840
|
+
status: "redirect",
|
|
841
|
+
target: resolveRoute(result, context.to.path)
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
} catch (error) {
|
|
845
|
+
return {
|
|
846
|
+
status: "failure",
|
|
847
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, error)
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
return { status: "continue" };
|
|
851
|
+
}
|
|
852
|
+
function executeNavigationMethod(method, options, to, from) {
|
|
853
|
+
return new Promise((resolve) => {
|
|
854
|
+
let settled = false;
|
|
855
|
+
const finalize = (result) => {
|
|
856
|
+
if (settled) return;
|
|
857
|
+
settled = true;
|
|
858
|
+
resolve(result);
|
|
859
|
+
};
|
|
860
|
+
try {
|
|
861
|
+
const maybePromise = method({
|
|
862
|
+
...options,
|
|
863
|
+
success: () => finalize(),
|
|
864
|
+
fail: (error) => {
|
|
865
|
+
finalize(createNavigationFailure(resolveNavigationFailureType(error), to, from, error));
|
|
866
|
+
}
|
|
867
|
+
});
|
|
868
|
+
if (maybePromise && typeof maybePromise.then === "function") maybePromise.then(() => finalize(), (error) => finalize(createNavigationFailure(resolveNavigationFailureType(error), to, from, error)));
|
|
869
|
+
} catch (error) {
|
|
870
|
+
finalize(createNavigationFailure(resolveNavigationFailureType(error), to, from, error));
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
async function runAfterEachHooks(hooks, context) {
|
|
875
|
+
for (const hook of hooks) try {
|
|
876
|
+
await hook(context.to, context.from, context.failure, context);
|
|
877
|
+
} catch (_unused) {}
|
|
878
|
+
}
|
|
879
|
+
async function runNavigationErrorHooks(handlers, error, context) {
|
|
880
|
+
for (const handler of handlers) try {
|
|
881
|
+
await handler(error, context);
|
|
882
|
+
} catch (_unused2) {}
|
|
883
|
+
}
|
|
884
|
+
function isNativeFailureLikeError(value) {
|
|
885
|
+
if (!value || typeof value !== "object") return false;
|
|
886
|
+
return typeof value.errMsg === "string";
|
|
887
|
+
}
|
|
888
|
+
function shouldEmitNavigationError(failure) {
|
|
889
|
+
if (failure.type === NavigationFailureType.unknown) return true;
|
|
890
|
+
if (failure.cause instanceof Error) return true;
|
|
891
|
+
if (failure.type === NavigationFailureType.aborted && failure.cause !== void 0) {
|
|
892
|
+
if (typeof failure.cause === "string") return false;
|
|
893
|
+
if (isNativeFailureLikeError(failure.cause)) return false;
|
|
894
|
+
return true;
|
|
895
|
+
}
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
//#endregion
|
|
899
|
+
//#region src/router/resolve.ts
|
|
900
|
+
const DEFAULT_ROUTE_RESOLVE_CODEC = {
|
|
901
|
+
parseQuery,
|
|
902
|
+
stringifyQuery
|
|
903
|
+
};
|
|
904
|
+
function resolveRouteLocation(to, currentPath = "", codec = DEFAULT_ROUTE_RESOLVE_CODEC) {
|
|
905
|
+
var _ref, _to$path, _parsedFromFullPath$q, _to$hash;
|
|
906
|
+
if (typeof to === "string") {
|
|
907
|
+
const parsed = parsePathInput(to, codec);
|
|
908
|
+
return createRouteLocation(resolvePath(parsed.path, currentPath), parsed.query, parsed.hash, void 0, {}, codec.stringifyQuery);
|
|
909
|
+
}
|
|
910
|
+
const parsedFromFullPath = typeof to.fullPath === "string" ? parsePathInput(to.fullPath, codec) : void 0;
|
|
911
|
+
return createRouteLocation(resolvePath((_ref = (_to$path = to.path) !== null && _to$path !== void 0 ? _to$path : parsedFromFullPath === null || parsedFromFullPath === void 0 ? void 0 : parsedFromFullPath.path) !== null && _ref !== void 0 ? _ref : currentPath, currentPath), to.query ? normalizeQuery(to.query) : (_parsedFromFullPath$q = parsedFromFullPath === null || parsedFromFullPath === void 0 ? void 0 : parsedFromFullPath.query) !== null && _parsedFromFullPath$q !== void 0 ? _parsedFromFullPath$q : {}, normalizeHash((_to$hash = to.hash) !== null && _to$hash !== void 0 ? _to$hash : parsedFromFullPath === null || parsedFromFullPath === void 0 ? void 0 : parsedFromFullPath.hash), typeof to.name === "string" ? to.name : void 0, to.params ? normalizeRouteParams(to.params) : {}, codec.stringifyQuery);
|
|
912
|
+
}
|
|
913
|
+
//#endregion
|
|
914
|
+
//#region src/router/useRoute.ts
|
|
915
|
+
function useRoute() {
|
|
916
|
+
if (!getCurrentSetupContext()) throw new Error("useRoute() 必须在 setup() 的同步阶段调用");
|
|
917
|
+
const currentRoute = resolveCurrentRoute();
|
|
918
|
+
const routeState = reactive({
|
|
919
|
+
path: currentRoute.path,
|
|
920
|
+
fullPath: currentRoute.fullPath,
|
|
921
|
+
query: currentRoute.query,
|
|
922
|
+
hash: currentRoute.hash,
|
|
923
|
+
params: currentRoute.params,
|
|
924
|
+
name: currentRoute.name
|
|
925
|
+
});
|
|
926
|
+
if (currentRoute.meta !== void 0) routeState.meta = currentRoute.meta;
|
|
927
|
+
function syncRoute(queryOverride) {
|
|
928
|
+
const nextRoute = resolveCurrentRoute(queryOverride);
|
|
929
|
+
routeState.path = nextRoute.path;
|
|
930
|
+
routeState.fullPath = nextRoute.fullPath;
|
|
931
|
+
routeState.query = nextRoute.query;
|
|
932
|
+
routeState.hash = nextRoute.hash;
|
|
933
|
+
if (nextRoute.meta === void 0) delete routeState.meta;
|
|
934
|
+
else routeState.meta = nextRoute.meta;
|
|
935
|
+
routeState.params = nextRoute.params;
|
|
936
|
+
routeState.name = nextRoute.name;
|
|
937
|
+
}
|
|
938
|
+
onLoad((query) => {
|
|
939
|
+
syncRoute(query);
|
|
940
|
+
});
|
|
941
|
+
onShow(() => {
|
|
942
|
+
syncRoute();
|
|
943
|
+
});
|
|
944
|
+
onRouteDone(() => {
|
|
945
|
+
syncRoute();
|
|
946
|
+
});
|
|
947
|
+
return readonly(routeState);
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* @description 获取当前组件路径语义的原生 Router
|
|
951
|
+
*/
|
|
952
|
+
function useNativeRouter() {
|
|
953
|
+
return useNativeRouter$1();
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* @description 获取当前页面路径语义的原生 Page Router
|
|
957
|
+
*/
|
|
958
|
+
function useNativePageRouter() {
|
|
959
|
+
return useNativePageRouter$1();
|
|
960
|
+
}
|
|
961
|
+
//#endregion
|
|
962
|
+
//#region src/router/navigationResult.ts
|
|
963
|
+
function createNavigationRunResult(mode, from, to, failure) {
|
|
964
|
+
return {
|
|
965
|
+
mode,
|
|
966
|
+
from,
|
|
967
|
+
to,
|
|
968
|
+
failure
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
function createNavigationResultController(options) {
|
|
972
|
+
const { afterEachHooks, errorHandlers, nativeRouter, rejectOnError } = options;
|
|
973
|
+
async function emitNavigationAfterEach(result) {
|
|
974
|
+
await runAfterEachHooks(afterEachHooks, {
|
|
975
|
+
mode: result.mode,
|
|
976
|
+
to: result.to,
|
|
977
|
+
from: result.from,
|
|
978
|
+
nativeRouter,
|
|
979
|
+
failure: result.failure
|
|
980
|
+
});
|
|
981
|
+
if (result.failure && shouldEmitNavigationError(result.failure)) {
|
|
982
|
+
var _result$failure$cause;
|
|
983
|
+
await runNavigationErrorHooks(errorHandlers, (_result$failure$cause = result.failure.cause) !== null && _result$failure$cause !== void 0 ? _result$failure$cause : result.failure, {
|
|
984
|
+
mode: result.mode,
|
|
985
|
+
to: result.to,
|
|
986
|
+
from: result.from,
|
|
987
|
+
nativeRouter,
|
|
988
|
+
failure: result.failure
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
function shouldRejectNavigationFailure(failure) {
|
|
993
|
+
return rejectOnError && shouldEmitNavigationError(failure);
|
|
994
|
+
}
|
|
995
|
+
async function settleNavigationResult(result) {
|
|
996
|
+
await emitNavigationAfterEach(result);
|
|
997
|
+
if (result.failure && shouldRejectNavigationFailure(result.failure)) throw result.failure;
|
|
998
|
+
return result.failure;
|
|
999
|
+
}
|
|
1000
|
+
return { settleNavigationResult };
|
|
1001
|
+
}
|
|
1002
|
+
//#endregion
|
|
1003
|
+
//#region src/router/navigationTargetHelpers.ts
|
|
1004
|
+
function createDuplicatedFailure(target, from) {
|
|
1005
|
+
return createNavigationFailure(NavigationFailureType.duplicated, target, from, "Avoided redundant navigation to current location");
|
|
1006
|
+
}
|
|
1007
|
+
function createTabBarQueryFailure(target, from) {
|
|
1008
|
+
return createNavigationFailure(NavigationFailureType.aborted, target, from, "switchTab does not support query parameters");
|
|
1009
|
+
}
|
|
1010
|
+
function createTooManyRedirectsFailure(target, from, maxRedirects) {
|
|
1011
|
+
return createNavigationFailure(NavigationFailureType.aborted, target, from, `Navigation redirected more than ${maxRedirects} times`);
|
|
1012
|
+
}
|
|
1013
|
+
function createHashOnlyNavigationFailure(target, from) {
|
|
1014
|
+
return createNavigationFailure(NavigationFailureType.aborted, target, from, "Hash-only navigation is not supported in mini-program router");
|
|
1015
|
+
}
|
|
1016
|
+
function createForwardNotSupportedFailure(from) {
|
|
1017
|
+
return createNavigationFailure(NavigationFailureType.aborted, void 0, from, "Forward navigation is not supported in mini-program router");
|
|
1018
|
+
}
|
|
1019
|
+
function applyRedirectedFrom(redirectedTarget, currentTarget) {
|
|
1020
|
+
redirectedTarget.redirectedFrom = currentTarget.redirectedFrom ? cloneRouteLocationRedirectedFrom(currentTarget.redirectedFrom) : createRedirectedFromSnapshot(currentTarget);
|
|
1021
|
+
}
|
|
1022
|
+
async function resolveRouteRecordRedirect(redirect, to, from, resolveWithCodec) {
|
|
1023
|
+
const redirectResult = typeof redirect === "function" ? await redirect(to, from) : redirect;
|
|
1024
|
+
if (isNavigationRedirectCandidate(redirectResult)) return {
|
|
1025
|
+
target: resolveWithCodec(redirectResult.to, to.path),
|
|
1026
|
+
replace: redirectResult.replace
|
|
1027
|
+
};
|
|
1028
|
+
return {
|
|
1029
|
+
target: resolveWithCodec(redirectResult, to.path),
|
|
1030
|
+
replace: true
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
//#endregion
|
|
1034
|
+
//#region src/router/navigationTarget.ts
|
|
1035
|
+
async function navigateWithTarget(options) {
|
|
1036
|
+
const { mode, target, from, nativeRouter, routeResolveCodec, namedRouteLookup, beforeEachGuards, beforeResolveGuards, maxRedirects, tabBarPathSet, resolveWithCodec } = options;
|
|
1037
|
+
let currentTarget = target;
|
|
1038
|
+
let currentMode = mode;
|
|
1039
|
+
let redirectCount = 0;
|
|
1040
|
+
while (true) {
|
|
1041
|
+
var _matchedRouteResult$m;
|
|
1042
|
+
const tabBarTarget = tabBarPathSet.has(currentTarget.path);
|
|
1043
|
+
if (createNativeRouteUrl(currentTarget, routeResolveCodec.stringifyQuery) === createNativeRouteUrl(from, routeResolveCodec.stringifyQuery) && currentTarget.hash !== from.hash) return createNavigationRunResult(currentMode, from, currentTarget, createHashOnlyNavigationFailure(currentTarget, from));
|
|
1044
|
+
if (tabBarTarget ? currentTarget.path === from.path : currentTarget.fullPath === from.fullPath) return createNavigationRunResult(currentMode, from, currentTarget, createDuplicatedFailure(currentTarget, from));
|
|
1045
|
+
if (tabBarTarget && hasLocationQuery(currentTarget.query)) return createNavigationRunResult(currentMode, from, currentTarget, createTabBarQueryFailure(currentTarget, from));
|
|
1046
|
+
const beforeEachResult = await runNavigationGuards(beforeEachGuards, {
|
|
1047
|
+
mode: currentMode,
|
|
1048
|
+
to: currentTarget,
|
|
1049
|
+
from,
|
|
1050
|
+
nativeRouter
|
|
1051
|
+
}, resolveWithCodec);
|
|
1052
|
+
if (beforeEachResult.status === "failure") return createNavigationRunResult(currentMode, from, currentTarget, beforeEachResult.failure);
|
|
1053
|
+
if (beforeEachResult.status === "redirect") {
|
|
1054
|
+
const redirectedTarget = beforeEachResult.target;
|
|
1055
|
+
const redirectedMode = beforeEachResult.replace === true ? "replace" : beforeEachResult.replace === false ? "push" : currentMode;
|
|
1056
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
1057
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
1058
|
+
currentTarget = redirectedTarget;
|
|
1059
|
+
currentMode = redirectedMode;
|
|
1060
|
+
redirectCount += 1;
|
|
1061
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
const matchedRouteResult = resolveMatchedRouteRecord(currentTarget, namedRouteLookup);
|
|
1066
|
+
const matchedRouteRecords = (_matchedRouteResult$m = matchedRouteResult === null || matchedRouteResult === void 0 ? void 0 : matchedRouteResult.matchedRecords) !== null && _matchedRouteResult$m !== void 0 ? _matchedRouteResult$m : [];
|
|
1067
|
+
let redirectedByRouteRecord = false;
|
|
1068
|
+
for (const matchedRouteRecord of matchedRouteRecords) {
|
|
1069
|
+
if (matchedRouteRecord.redirect === void 0) continue;
|
|
1070
|
+
let redirectedByRecord;
|
|
1071
|
+
try {
|
|
1072
|
+
redirectedByRecord = await resolveRouteRecordRedirect(matchedRouteRecord.redirect, currentTarget, from, resolveWithCodec);
|
|
1073
|
+
} catch (error) {
|
|
1074
|
+
return createNavigationRunResult(currentMode, from, currentTarget, createNavigationFailure(NavigationFailureType.aborted, currentTarget, from, error));
|
|
1075
|
+
}
|
|
1076
|
+
const redirectedMode = redirectedByRecord.replace === false ? "push" : "replace";
|
|
1077
|
+
const redirectedTarget = redirectedByRecord.target;
|
|
1078
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
1079
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
1080
|
+
currentTarget = redirectedTarget;
|
|
1081
|
+
currentMode = redirectedMode;
|
|
1082
|
+
redirectCount += 1;
|
|
1083
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
1084
|
+
redirectedByRouteRecord = true;
|
|
1085
|
+
break;
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
if (redirectedByRouteRecord) continue;
|
|
1089
|
+
let redirectedByBeforeEnter = false;
|
|
1090
|
+
for (const matchedRouteRecord of matchedRouteRecords) {
|
|
1091
|
+
if (matchedRouteRecord.beforeEnterGuards.length === 0) continue;
|
|
1092
|
+
const beforeEnterResult = await runNavigationGuards(new Set(matchedRouteRecord.beforeEnterGuards), {
|
|
1093
|
+
mode: currentMode,
|
|
1094
|
+
to: currentTarget,
|
|
1095
|
+
from,
|
|
1096
|
+
nativeRouter
|
|
1097
|
+
}, resolveWithCodec);
|
|
1098
|
+
if (beforeEnterResult.status === "failure") return createNavigationRunResult(currentMode, from, currentTarget, beforeEnterResult.failure);
|
|
1099
|
+
if (beforeEnterResult.status === "redirect") {
|
|
1100
|
+
const redirectedTarget = beforeEnterResult.target;
|
|
1101
|
+
const redirectedMode = beforeEnterResult.replace === true ? "replace" : beforeEnterResult.replace === false ? "push" : currentMode;
|
|
1102
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
1103
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
1104
|
+
currentTarget = redirectedTarget;
|
|
1105
|
+
currentMode = redirectedMode;
|
|
1106
|
+
redirectCount += 1;
|
|
1107
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
1108
|
+
redirectedByBeforeEnter = true;
|
|
1109
|
+
break;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
if (redirectedByBeforeEnter) continue;
|
|
1114
|
+
const beforeResolveResult = await runNavigationGuards(beforeResolveGuards, {
|
|
1115
|
+
mode: currentMode,
|
|
1116
|
+
to: currentTarget,
|
|
1117
|
+
from,
|
|
1118
|
+
nativeRouter
|
|
1119
|
+
}, resolveWithCodec);
|
|
1120
|
+
if (beforeResolveResult.status === "failure") return createNavigationRunResult(currentMode, from, currentTarget, beforeResolveResult.failure);
|
|
1121
|
+
if (beforeResolveResult.status === "redirect") {
|
|
1122
|
+
const redirectedTarget = beforeResolveResult.target;
|
|
1123
|
+
const redirectedMode = beforeResolveResult.replace === true ? "replace" : beforeResolveResult.replace === false ? "push" : currentMode;
|
|
1124
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
1125
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
1126
|
+
currentTarget = redirectedTarget;
|
|
1127
|
+
currentMode = redirectedMode;
|
|
1128
|
+
redirectCount += 1;
|
|
1129
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
1130
|
+
continue;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
if (tabBarTarget) {
|
|
1134
|
+
const result = await executeNavigationMethod(nativeRouter.switchTab, { url: createAbsoluteRoutePath(currentTarget.path) }, currentTarget, from);
|
|
1135
|
+
if (isNavigationFailure(result)) return createNavigationRunResult(currentMode, from, currentTarget, result);
|
|
1136
|
+
return createNavigationRunResult(currentMode, from, currentTarget);
|
|
1137
|
+
}
|
|
1138
|
+
const result = await executeNavigationMethod(currentMode === "push" ? nativeRouter.navigateTo : nativeRouter.redirectTo, { url: createNativeRouteUrl(currentTarget, routeResolveCodec.stringifyQuery) }, currentTarget, from);
|
|
1139
|
+
if (isNavigationFailure(result)) return createNavigationRunResult(currentMode, from, currentTarget, result);
|
|
1140
|
+
return createNavigationRunResult(currentMode, from, currentTarget);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
//#endregion
|
|
1144
|
+
//#region src/router/navigationApi.ts
|
|
1145
|
+
function createNavigationApi(options) {
|
|
1146
|
+
const { nativeRouter, route, routeResolveCodec, namedRouteLookup, beforeEachGuards, beforeResolveGuards, maxRedirects, tabBarPathSet, resolveWithCodec, settleNavigationResult } = options;
|
|
1147
|
+
async function push(to) {
|
|
1148
|
+
const from = snapshotRouteLocation(route);
|
|
1149
|
+
let target;
|
|
1150
|
+
try {
|
|
1151
|
+
target = resolveWithCodec(to, from.path);
|
|
1152
|
+
} catch (error) {
|
|
1153
|
+
return settleNavigationResult(createNavigationRunResult("push", from, void 0, createNavigationFailure(NavigationFailureType.unknown, void 0, from, error)));
|
|
1154
|
+
}
|
|
1155
|
+
return settleNavigationResult(await navigateWithTarget({
|
|
1156
|
+
mode: "push",
|
|
1157
|
+
target,
|
|
1158
|
+
from,
|
|
1159
|
+
nativeRouter,
|
|
1160
|
+
routeResolveCodec,
|
|
1161
|
+
namedRouteLookup,
|
|
1162
|
+
beforeEachGuards,
|
|
1163
|
+
beforeResolveGuards,
|
|
1164
|
+
maxRedirects,
|
|
1165
|
+
tabBarPathSet,
|
|
1166
|
+
resolveWithCodec
|
|
1167
|
+
}));
|
|
1168
|
+
}
|
|
1169
|
+
async function replace(to) {
|
|
1170
|
+
const from = snapshotRouteLocation(route);
|
|
1171
|
+
let target;
|
|
1172
|
+
try {
|
|
1173
|
+
target = resolveWithCodec(to, from.path);
|
|
1174
|
+
} catch (error) {
|
|
1175
|
+
return settleNavigationResult(createNavigationRunResult("replace", from, void 0, createNavigationFailure(NavigationFailureType.unknown, void 0, from, error)));
|
|
1176
|
+
}
|
|
1177
|
+
return settleNavigationResult(await navigateWithTarget({
|
|
1178
|
+
mode: "replace",
|
|
1179
|
+
target,
|
|
1180
|
+
from,
|
|
1181
|
+
nativeRouter,
|
|
1182
|
+
routeResolveCodec,
|
|
1183
|
+
namedRouteLookup,
|
|
1184
|
+
beforeEachGuards,
|
|
1185
|
+
beforeResolveGuards,
|
|
1186
|
+
maxRedirects,
|
|
1187
|
+
tabBarPathSet,
|
|
1188
|
+
resolveWithCodec
|
|
1189
|
+
}));
|
|
1190
|
+
}
|
|
1191
|
+
async function back(delta = 1) {
|
|
1192
|
+
const from = snapshotRouteLocation(route);
|
|
1193
|
+
const beforeEachResult = await runNavigationGuards(beforeEachGuards, {
|
|
1194
|
+
mode: "back",
|
|
1195
|
+
from,
|
|
1196
|
+
nativeRouter
|
|
1197
|
+
}, resolveWithCodec);
|
|
1198
|
+
if (beforeEachResult.status === "failure") return settleNavigationResult(createNavigationRunResult("back", from, void 0, beforeEachResult.failure));
|
|
1199
|
+
if (beforeEachResult.status === "redirect") return settleNavigationResult(createNavigationRunResult("back", from, void 0, createNavigationFailure(NavigationFailureType.aborted, void 0, from, "Redirect is not supported in back navigation guards")));
|
|
1200
|
+
const beforeResolveResult = await runNavigationGuards(beforeResolveGuards, {
|
|
1201
|
+
mode: "back",
|
|
1202
|
+
from,
|
|
1203
|
+
nativeRouter
|
|
1204
|
+
}, resolveWithCodec);
|
|
1205
|
+
if (beforeResolveResult.status === "failure") return settleNavigationResult(createNavigationRunResult("back", from, void 0, beforeResolveResult.failure));
|
|
1206
|
+
if (beforeResolveResult.status === "redirect") return settleNavigationResult(createNavigationRunResult("back", from, void 0, createNavigationFailure(NavigationFailureType.aborted, void 0, from, "Redirect is not supported in back navigation guards")));
|
|
1207
|
+
const result = await executeNavigationMethod(nativeRouter.navigateBack, { delta }, void 0, from);
|
|
1208
|
+
return settleNavigationResult(isNavigationFailure(result) ? createNavigationRunResult("back", from, void 0, result) : createNavigationRunResult("back", from));
|
|
1209
|
+
}
|
|
1210
|
+
async function go(delta) {
|
|
1211
|
+
if (delta < 0) return back(Math.abs(delta));
|
|
1212
|
+
if (delta === 0) return;
|
|
1213
|
+
const from = snapshotRouteLocation(route);
|
|
1214
|
+
return settleNavigationResult(createNavigationRunResult("back", from, void 0, createForwardNotSupportedFailure(from)));
|
|
1215
|
+
}
|
|
1216
|
+
async function forward() {
|
|
1217
|
+
return go(1);
|
|
1218
|
+
}
|
|
1219
|
+
return {
|
|
1220
|
+
push,
|
|
1221
|
+
replace,
|
|
1222
|
+
back,
|
|
1223
|
+
go,
|
|
1224
|
+
forward
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
//#endregion
|
|
1228
|
+
//#region src/router/routeRegistry.ts
|
|
1229
|
+
function normalizeRouteRecordForOutput(record) {
|
|
1230
|
+
const normalizedRoute = {
|
|
1231
|
+
name: record.name,
|
|
1232
|
+
path: record.path ? `/${record.path}` : "/"
|
|
1233
|
+
};
|
|
1234
|
+
if (record.meta !== void 0) normalizedRoute.meta = cloneRouteMeta(record.meta);
|
|
1235
|
+
if (record.aliasPaths.length === 1) normalizedRoute.alias = createAbsoluteRoutePath(record.aliasPaths[0]);
|
|
1236
|
+
else if (record.aliasPaths.length > 1) normalizedRoute.alias = record.aliasPaths.map((aliasPath) => createAbsoluteRoutePath(aliasPath));
|
|
1237
|
+
if (record.beforeEnterGuards.length === 1) normalizedRoute.beforeEnter = record.beforeEnterGuards[0];
|
|
1238
|
+
else if (record.beforeEnterGuards.length > 1) normalizedRoute.beforeEnter = record.beforeEnterGuards.slice();
|
|
1239
|
+
if (record.redirect !== void 0) normalizedRoute.redirect = record.redirect;
|
|
1240
|
+
return normalizedRoute;
|
|
1241
|
+
}
|
|
1242
|
+
function createRouteRegistry(namedRouteLookup) {
|
|
1243
|
+
const hasRoute = (name) => namedRouteLookup.recordByName.has(name);
|
|
1244
|
+
const getRoutes = () => Array.from(namedRouteLookup.recordByName.values()).map(normalizeRouteRecordForOutput);
|
|
1245
|
+
const addRoute = ((parentNameOrRoute, maybeRoute) => {
|
|
1246
|
+
const route = typeof parentNameOrRoute === "string" ? maybeRoute : parentNameOrRoute;
|
|
1247
|
+
if (!route) throw new Error("Route record is required when adding a child route");
|
|
1248
|
+
assertValidAddRouteInput(route);
|
|
1249
|
+
const parentRouteName = typeof parentNameOrRoute === "string" ? parentNameOrRoute : void 0;
|
|
1250
|
+
const parentRouteRecord = parentRouteName ? namedRouteLookup.recordByName.get(parentRouteName) : void 0;
|
|
1251
|
+
if (parentRouteName && !parentRouteRecord) throw new Error(`Parent route "${parentRouteName}" is not defined in useRouter({ routes | namedRoutes })`);
|
|
1252
|
+
const routeRecords = flattenNamedRouteRecords([route], parentRouteRecord === null || parentRouteRecord === void 0 ? void 0 : parentRouteRecord.path, parentRouteName, parentRouteRecord === null || parentRouteRecord === void 0 ? void 0 : parentRouteRecord.aliasPaths, void 0, "addRoute");
|
|
1253
|
+
if (routeRecords.length === 0) throw new Error("Route name and path are required when adding a named route");
|
|
1254
|
+
const addedRoutes = [];
|
|
1255
|
+
for (const routeRecord of routeRecords) {
|
|
1256
|
+
const normalizedRoute = normalizeRouteRecordRaw(routeRecord.route, routeRecord.parentName);
|
|
1257
|
+
if (!normalizedRoute) continue;
|
|
1258
|
+
const existingRoute = namedRouteLookup.recordByName.get(normalizedRoute.name);
|
|
1259
|
+
if (existingRoute) {
|
|
1260
|
+
const namesToRemove = collectRouteNamesForRemoval(existingRoute.name, namedRouteLookup.recordByName);
|
|
1261
|
+
for (const routeName of namesToRemove) namedRouteLookup.recordByName.delete(routeName);
|
|
1262
|
+
warnRouteConfig(`addRoute() replaced existing route "${normalizedRoute.name}" (${createAbsoluteRoutePath(existingRoute.path)} -> ${createAbsoluteRoutePath(normalizedRoute.path)}) and removed ${namesToRemove.size - 1} nested route(s)`);
|
|
1263
|
+
}
|
|
1264
|
+
namedRouteLookup.recordByName.set(normalizedRoute.name, normalizedRoute);
|
|
1265
|
+
addedRoutes.push(normalizedRoute);
|
|
1266
|
+
}
|
|
1267
|
+
namedRouteLookup.nameByStaticPath = createNamedRouteNameByStaticPath(namedRouteLookup.recordByName);
|
|
1268
|
+
return () => {
|
|
1269
|
+
let changed = false;
|
|
1270
|
+
for (const addedRoute of addedRoutes) if (namedRouteLookup.recordByName.get(addedRoute.name) === addedRoute) {
|
|
1271
|
+
namedRouteLookup.recordByName.delete(addedRoute.name);
|
|
1272
|
+
changed = true;
|
|
1273
|
+
}
|
|
1274
|
+
if (changed) namedRouteLookup.nameByStaticPath = createNamedRouteNameByStaticPath(namedRouteLookup.recordByName);
|
|
1275
|
+
};
|
|
1276
|
+
});
|
|
1277
|
+
function removeRoute(name) {
|
|
1278
|
+
const namesToRemove = collectRouteNamesForRemoval(name, namedRouteLookup.recordByName);
|
|
1279
|
+
if (namesToRemove.size === 0) return;
|
|
1280
|
+
let changed = false;
|
|
1281
|
+
for (const routeName of namesToRemove) changed = namedRouteLookup.recordByName.delete(routeName) || changed;
|
|
1282
|
+
if (changed) namedRouteLookup.nameByStaticPath = createNamedRouteNameByStaticPath(namedRouteLookup.recordByName);
|
|
1283
|
+
}
|
|
1284
|
+
function clearRoutes() {
|
|
1285
|
+
namedRouteLookup.recordByName.clear();
|
|
1286
|
+
namedRouteLookup.nameByStaticPath.clear();
|
|
1287
|
+
}
|
|
1288
|
+
return {
|
|
1289
|
+
hasRoute,
|
|
1290
|
+
getRoutes,
|
|
1291
|
+
addRoute,
|
|
1292
|
+
removeRoute,
|
|
1293
|
+
clearRoutes
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
//#endregion
|
|
1297
|
+
//#region src/router/useRouter.ts
|
|
1298
|
+
/**
|
|
1299
|
+
* @description 获取高阶路由导航器(对齐 Vue Router 心智)
|
|
1300
|
+
*/
|
|
1301
|
+
function useRouter(options = {}) {
|
|
1302
|
+
var _options$maxRedirects, _options$paramsMode, _options$rejectOnErro, _options$parseQuery, _options$stringifyQue, _options$tabBarEntrie;
|
|
1303
|
+
const nativeRouter = useNativeRouter();
|
|
1304
|
+
const route = useRoute();
|
|
1305
|
+
const beforeEachGuards = /* @__PURE__ */ new Set();
|
|
1306
|
+
const beforeResolveGuards = /* @__PURE__ */ new Set();
|
|
1307
|
+
const afterEachHooks = /* @__PURE__ */ new Set();
|
|
1308
|
+
const errorHandlers = /* @__PURE__ */ new Set();
|
|
1309
|
+
const maxRedirects = (_options$maxRedirects = options.maxRedirects) !== null && _options$maxRedirects !== void 0 ? _options$maxRedirects : 10;
|
|
1310
|
+
const paramsMode = (_options$paramsMode = options.paramsMode) !== null && _options$paramsMode !== void 0 ? _options$paramsMode : "loose";
|
|
1311
|
+
const rejectOnError = (_options$rejectOnErro = options.rejectOnError) !== null && _options$rejectOnErro !== void 0 ? _options$rejectOnErro : true;
|
|
1312
|
+
const routeResolveCodec = {
|
|
1313
|
+
parseQuery: (_options$parseQuery = options.parseQuery) !== null && _options$parseQuery !== void 0 ? _options$parseQuery : parseQuery,
|
|
1314
|
+
stringifyQuery: (_options$stringifyQue = options.stringifyQuery) !== null && _options$stringifyQue !== void 0 ? _options$stringifyQue : stringifyQuery
|
|
1315
|
+
};
|
|
1316
|
+
const readyPromise = Promise.resolve();
|
|
1317
|
+
const routeEntries = resolveRouteOptionEntries(options);
|
|
1318
|
+
warnDuplicateRouteEntries(routeEntries);
|
|
1319
|
+
const namedRouteLookup = createNamedRouteLookup(routeEntries);
|
|
1320
|
+
const normalizedTabBarEntries = ((_options$tabBarEntrie = options.tabBarEntries) !== null && _options$tabBarEntrie !== void 0 ? _options$tabBarEntrie : []).map((path) => resolvePath(path, "")).filter(Boolean);
|
|
1321
|
+
const tabBarPathSet = new Set(normalizedTabBarEntries);
|
|
1322
|
+
const routeRegistry = createRouteRegistry(namedRouteLookup);
|
|
1323
|
+
const routerOptions = createRouterOptionsSnapshot(normalizedTabBarEntries, routeRegistry.getRoutes(), paramsMode, maxRedirects, routeResolveCodec, rejectOnError);
|
|
1324
|
+
function resolveWithCodec(to, currentPath) {
|
|
1325
|
+
const resolved = resolveRouteLocation(typeof to === "string" ? to : resolveNamedRouteLocation(to, namedRouteLookup, paramsMode), currentPath, routeResolveCodec);
|
|
1326
|
+
resolved.href = resolved.fullPath;
|
|
1327
|
+
const matchedResult = resolveMatchedRouteRecord(resolved, namedRouteLookup);
|
|
1328
|
+
if (matchedResult) {
|
|
1329
|
+
const matchedRecord = matchedResult.record;
|
|
1330
|
+
if (resolved.name === void 0) resolved.name = matchedRecord.name;
|
|
1331
|
+
const mergedRouteMeta = mergeMatchedRouteMeta(matchedResult.matchedRecords);
|
|
1332
|
+
if (mergedRouteMeta !== void 0) resolved.meta = cloneRouteMeta(mergedRouteMeta);
|
|
1333
|
+
if (matchedResult.params && Object.keys(resolved.params).length === 0) resolved.params = cloneRouteParams(matchedResult.params);
|
|
1334
|
+
resolved.matched = matchedResult.matchedRecords.map((record, index, records) => {
|
|
1335
|
+
return normalizeRouteRecordMatched(record, index === records.length - 1 ? matchedResult.matchedPath : void 0);
|
|
1336
|
+
});
|
|
1337
|
+
} else resolved.matched = [];
|
|
1338
|
+
return resolved;
|
|
1339
|
+
}
|
|
1340
|
+
function resolve(to) {
|
|
1341
|
+
return resolveWithCodec(to, route.path);
|
|
1342
|
+
}
|
|
1343
|
+
function install(_app) {}
|
|
1344
|
+
function isReady() {
|
|
1345
|
+
return readyPromise;
|
|
1346
|
+
}
|
|
1347
|
+
const navigationApi = createNavigationApi({
|
|
1348
|
+
nativeRouter,
|
|
1349
|
+
route,
|
|
1350
|
+
routeResolveCodec,
|
|
1351
|
+
namedRouteLookup,
|
|
1352
|
+
beforeEachGuards,
|
|
1353
|
+
beforeResolveGuards,
|
|
1354
|
+
maxRedirects,
|
|
1355
|
+
tabBarPathSet,
|
|
1356
|
+
resolveWithCodec,
|
|
1357
|
+
settleNavigationResult: createNavigationResultController({
|
|
1358
|
+
afterEachHooks,
|
|
1359
|
+
errorHandlers,
|
|
1360
|
+
nativeRouter,
|
|
1361
|
+
rejectOnError
|
|
1362
|
+
}).settleNavigationResult
|
|
1363
|
+
});
|
|
1364
|
+
function beforeEach(guard) {
|
|
1365
|
+
beforeEachGuards.add(guard);
|
|
1366
|
+
return () => {
|
|
1367
|
+
beforeEachGuards.delete(guard);
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
function beforeResolve(guard) {
|
|
1371
|
+
beforeResolveGuards.add(guard);
|
|
1372
|
+
return () => {
|
|
1373
|
+
beforeResolveGuards.delete(guard);
|
|
1374
|
+
};
|
|
1375
|
+
}
|
|
1376
|
+
function afterEach(hook) {
|
|
1377
|
+
afterEachHooks.add(hook);
|
|
1378
|
+
return () => {
|
|
1379
|
+
afterEachHooks.delete(hook);
|
|
1380
|
+
};
|
|
1381
|
+
}
|
|
1382
|
+
function onError(handler) {
|
|
1383
|
+
errorHandlers.add(handler);
|
|
1384
|
+
return () => {
|
|
1385
|
+
errorHandlers.delete(handler);
|
|
1386
|
+
};
|
|
1387
|
+
}
|
|
1388
|
+
return {
|
|
1389
|
+
nativeRouter,
|
|
1390
|
+
options: routerOptions,
|
|
1391
|
+
currentRoute: route,
|
|
1392
|
+
install,
|
|
1393
|
+
resolve,
|
|
1394
|
+
isReady,
|
|
1395
|
+
push: navigationApi.push,
|
|
1396
|
+
replace: navigationApi.replace,
|
|
1397
|
+
back: navigationApi.back,
|
|
1398
|
+
go: navigationApi.go,
|
|
1399
|
+
forward: navigationApi.forward,
|
|
1400
|
+
hasRoute: routeRegistry.hasRoute,
|
|
1401
|
+
getRoutes: routeRegistry.getRoutes,
|
|
1402
|
+
addRoute: routeRegistry.addRoute,
|
|
1403
|
+
removeRoute: routeRegistry.removeRoute,
|
|
1404
|
+
clearRoutes: routeRegistry.clearRoutes,
|
|
1405
|
+
beforeEach,
|
|
1406
|
+
beforeResolve,
|
|
1407
|
+
afterEach,
|
|
1408
|
+
onError
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
//#endregion
|
|
1412
|
+
export { NavigationFailureType, createNavigationFailure, isNavigationFailure, parseQuery, resolveRouteLocation, stringifyQuery, useNativePageRouter, useNativeRouter, useRoute, useRouter };
|