|
1 | | -import type { |
2 | | - CustomRouteBlock, |
3 | | - CustomRouteBlockQueryParamOptions, |
4 | | -} from './customBlock' |
| 1 | +import type { CustomRouteBlock } from './customBlock' |
5 | 2 | import { joinPath, mergeRouteRecordOverride, warn } from './utils' |
6 | 3 | import { encodePath } from '../utils/encoding' |
7 | | -import type { RouteRecordRaw } from '../../types' |
8 | 4 |
|
9 | 5 | export const enum TreeNodeType { |
10 | 6 | static, |
11 | 7 | group, |
12 | 8 | param, |
13 | 9 | } |
14 | 10 |
|
15 | | -export interface RouteRecordOverride extends Partial< |
16 | | - Pick<RouteRecordRaw, 'meta' | 'props' | 'path'> |
17 | | -> { |
18 | | - name?: string | undefined | false |
19 | | - |
20 | | - /** |
21 | | - * Path aliases. |
22 | | - */ |
23 | | - alias?: string[] |
24 | | - |
25 | | - /** |
26 | | - * Param Parsers information. |
27 | | - */ |
28 | | - params?: { |
29 | | - path?: Record<string, string> |
30 | | - |
31 | | - query?: Record<string, string | RouteRecordOverrideQueryParamOptions> |
32 | | - } |
33 | | -} |
34 | | - |
35 | | -export interface RouteRecordOverrideQueryParamOptions extends CustomRouteBlockQueryParamOptions { |
36 | | - default?: string |
37 | | - required?: boolean |
38 | | -} |
| 11 | +/** |
| 12 | + * Internal merged-overrides shape used by tree nodes. Same structure as |
| 13 | + * {@link CustomRouteBlock} (the user-facing `<route>` block / `definePage()` |
| 14 | + * payload). |
| 15 | + */ |
| 16 | +export type RouteRecordOverride = CustomRouteBlock |
39 | 17 |
|
40 | 18 | export type SubSegment = string | TreePathParam |
41 | 19 |
|
@@ -221,7 +199,6 @@ class _TreeNodeValueBase { |
221 | 199 | */ |
222 | 200 | removeOverride(key: keyof CustomRouteBlock) { |
223 | 201 | for (const [_filePath, routeBlock] of this._overrides) { |
224 | | - // @ts-expect-error |
225 | 202 | delete routeBlock[key] |
226 | 203 | } |
227 | 204 | } |
@@ -402,16 +379,47 @@ export const escapeRegex = (str: string): string => |
402 | 379 | export class TreeNodeValueParam extends _TreeNodeValueBase { |
403 | 380 | override _type: TreeNodeType.param = TreeNodeType.param |
404 | 381 |
|
| 382 | + /** |
| 383 | + * @param rawSegment The raw segment as defined by the file structure, e.g. |
| 384 | + * `[id]`, `prefix-[param]-end`, etc. |
| 385 | + * |
| 386 | + * @param parent The parent node in the tree, if any. |
| 387 | + * |
| 388 | + * @param filenamePathParams Path params parsed from the file segment |
| 389 | + * (filename convention). The public `pathParams` getter overlays |
| 390 | + * `definePage()` parser overrides on top of these. |
| 391 | + * |
| 392 | + * @param pathSegment The transformed version of the segment into a |
| 393 | + * vue-router path, e.g. `:id`, `prefix-:param-end`, etc. |
| 394 | + * |
| 395 | + * @param subSegments Array of sub segments. This is usually one single |
| 396 | + * element but can have more for paths like `prefix-[param]-end.vue`. |
| 397 | + */ |
405 | 398 | constructor( |
406 | 399 | rawSegment: string, |
407 | 400 | parent: TreeNodeValue | undefined, |
408 | | - public pathParams: TreePathParam[], |
| 401 | + private filenamePathParams: TreePathParam[], |
409 | 402 | pathSegment: string, |
410 | 403 | subSegments: SubSegment[] |
411 | 404 | ) { |
412 | 405 | super(rawSegment, parent, pathSegment, subSegments) |
413 | 406 | } |
414 | 407 |
|
| 408 | + /** |
| 409 | + * Path params for this node, with `definePage({ params: { path: ... } })` |
| 410 | + * parser overrides applied on top of the filename-based parsers. |
| 411 | + */ |
| 412 | + get pathParams(): TreePathParam[] { |
| 413 | + const overridePath = this.overrides.params?.path |
| 414 | + if (!overridePath) return this.filenamePathParams |
| 415 | + return this.filenamePathParams.map(p => |
| 416 | + // an explicit `null` override removes the filename-based parser |
| 417 | + overridePath[p.paramName] !== undefined |
| 418 | + ? { ...p, parser: overridePath[p.paramName] } |
| 419 | + : p |
| 420 | + ) |
| 421 | + } |
| 422 | + |
415 | 423 | // Calculate score for each subsegment to handle mixed static/param parts |
416 | 424 | get score(): number[] { |
417 | 425 | return this.subSegments.map(segment => { |
|
0 commit comments