Skip to content

Commit 53ff9f7

Browse files
committed
refactor: native parsers
1 parent eeb10d0 commit 53ff9f7

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

playground-experimental/src/pages/[...path].vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import doc from '../main.ts?raw'
33
44
const route = useRoute()
55
route.params.path
6-
route.params.page
6+
7+
console.log('typeof', typeof route.params.active)
8+
console.log('value', route.params.active)
9+
console.log('typeof', typeof route.params.other)
10+
console.log('value', route.params.other)
711
812
definePage({
913
meta: {
@@ -20,6 +24,11 @@ definePage({
2024
default: 1,
2125
format: 'array',
2226
},
27+
other: 'bool',
28+
active: {
29+
parser: 'bool',
30+
default: false,
31+
},
2332
},
2433
},
2534
})

playground-experimental/typed-router.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ declare module 'vue-router/auto-routes' {
2222
*/
2323
export interface RouteNamedMap {
2424
'/(home)': RouteRecordInfo<'/(home)', '/', Record<never, never>, Record<never, never>>,
25-
'not-found': RouteRecordInfo<'not-found', '/:path(.*)', { path: string, page?: number }, { path: string, page: number }>,
25+
'not-found': RouteRecordInfo<'not-found', '/:path(.*)', { path: string, page?: number, other: boolean, active?: boolean }, { path: string, page: number, other: boolean, active: boolean }>,
2626
'/a.[b].c.[d]': RouteRecordInfo<'/a.[b].c.[d]', '/a/:b/c/:d', { b: string, d: string }, { b: string, d: string }>,
2727
'/b': RouteRecordInfo<'/b', '/b', Record<never, never>, Record<never, never>>,
2828
'/blog/[slug]+': RouteRecordInfo<'/blog/[slug]+', '/blog/:slug+', { slug: string[] }, { slug: [string, ...string[]] }>,

src/codegen/generateParamParsers.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ export type ParamParsersMap = Map<
1212
}
1313
>
1414

15+
// just for type strictness
16+
const _NATIVE_PARAM_PARSERS = ['int', 'bool'] as const
17+
const NATIVE_PARAM_PARSERS = _NATIVE_PARAM_PARSERS as readonly string[]
18+
const NATIVE_PARAM_PARSERS_TYPES = {
19+
int: 'number',
20+
bool: 'boolean',
21+
} satisfies Record<(typeof _NATIVE_PARAM_PARSERS)[number], string>
22+
1523
export function warnMissingParamParsers(
1624
tree: PrefixTree,
1725
paramParsers: ParamParsersMap
1826
) {
1927
for (const node of tree.getChildrenDeepSorted()) {
2028
for (const param of node.params) {
2129
if (param.parser && !paramParsers.has(param.parser)) {
22-
if (param.parser !== 'int') {
30+
if (!NATIVE_PARAM_PARSERS.includes(param.parser)) {
2331
console.warn(
2432
`Parameter parser "${param.parser}" not found for route "${node.fullPath}".`
2533
)
@@ -48,8 +56,10 @@ export function generateParamsTypes(
4856
if (param.parser) {
4957
if (parparsersMap.has(param.parser)) {
5058
return parparsersMap.get(param.parser)!.typeName
51-
} else if (param.parser === 'int') {
52-
return 'number'
59+
} else if (param.parser in NATIVE_PARAM_PARSERS_TYPES) {
60+
return NATIVE_PARAM_PARSERS_TYPES[
61+
param.parser as keyof typeof NATIVE_PARAM_PARSERS_TYPES
62+
]
5363
}
5464
}
5565
return null
@@ -61,15 +71,18 @@ export function generateParamParserOptions(
6171
importsMap: ImportsMap,
6272
paramParsers: ParamParsersMap
6373
): string {
74+
if (!param.parser) return ''
75+
6476
// we prioritize custom parsers to let users override them
65-
if (param.parser && paramParsers.has(param.parser)) {
77+
if (paramParsers.has(param.parser)) {
6678
const { name, absolutePath } = paramParsers.get(param.parser)!
6779
const varName = `PARAM_PARSER__${name}`
6880
importsMap.add(absolutePath, { name: 'parser', as: varName })
6981
return varName
70-
} else if (param.parser === 'int') {
71-
importsMap.add('vue-router/experimental', `PARAM_PARSER_INT`)
72-
return `PARAM_PARSER_INT`
82+
} else if (NATIVE_PARAM_PARSERS.includes(param.parser)) {
83+
const varName = `PARAM_PARSER_${param.parser.toUpperCase()}`
84+
importsMap.add('vue-router/experimental', varName)
85+
return varName
7386
}
7487
return ''
7588
}

src/core/definePage.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,7 @@ function extractQueryParams(
316316
) {
317317
paramInfo.format = paramProp.value.value as 'value' | 'array'
318318
} else if (paramProp.key.name === 'default') {
319-
if (
320-
typeof paramProp.value.extra?.raw === 'string' &&
321-
paramProp.value.extra
322-
) {
319+
if (typeof paramProp.value.extra?.raw === 'string') {
323320
paramInfo.default = paramProp.value.extra.raw
324321
} else {
325322
warn(

0 commit comments

Comments
 (0)