Skip to content

Commit e2966b0

Browse files
committed
refactor: put TOut type param first
1 parent 8fd456f commit e2966b0

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

packages/experiments-playground/src/router/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import {
33
experimental_createRouter,
44
createStaticResolver,
55
MatcherPatternPathStatic,
6-
MatcherPatternPathStar,
76
MatcherPatternPathCustomParams,
87
normalizeRouteRecord,
9-
PARAM_INTEGER,
8+
PARAM_PARSER_INTEGER,
109
} from 'vue-router/experimental'
1110
import type {
1211
EXPERIMENTAL_RouteRecordNormalized_Matchable,
@@ -144,7 +143,10 @@ const r_profiles_detail = normalizeRouteRecord({
144143
path: new MatcherPatternPathCustomParams(
145144
/^\/profiles\/([^/]+)$/i,
146145
{
147-
userId: PARAM_INTEGER,
146+
// this version handles all kind of params but in practice,
147+
// the generation should recognize this is a single required param
148+
// and therefore userId is of type number
149+
userId: PARAM_PARSER_INTEGER,
148150
},
149151
['profiles', 0]
150152
),

packages/router/src/experimental/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export {
2525
MatcherPatternPathCustomParams,
2626
// custom param parsers
2727
// TODO: find a more elegant format than having 4 variants per param type
28-
PARAM_INTEGER,
28+
PARAM_PARSER_INTEGER,
29+
PARAM_INTEGER_SINGLE,
2930
PARAM_NUMBER_OPTIONAL,
3031
PARAM_NUMBER_REPEATABLE,
3132
PARAM_NUMBER_REPEATABLE_OPTIONAL,

packages/router/src/experimental/route-resolver/matchers/matcher-pattern.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,16 @@ export class MatcherPatternPathStar
121121
// new MatcherPatternPathStatic('/team')
122122

123123
export interface Param_GetSet<
124-
TIn extends string | string[] | null | undefined =
125-
| string
126-
| string[]
127-
| null
128-
| undefined,
129124
TOut = string | string[] | null,
125+
TIn extends string | string[] | null = string | string[] | null,
130126
> {
131127
get?: (value: NoInfer<TIn>) => TOut
132128
set?: (value: NoInfer<TOut>) => TIn
133129
}
134130

135131
export type ParamParser_Generic =
136-
| Param_GetSet<string, any>
137-
| Param_GetSet<string[], any>
132+
| Param_GetSet<any, string>
133+
| Param_GetSet<any, string[]>
138134
// TODO: these are possible values for optional params
139135
// | null | undefined
140136

@@ -147,7 +143,7 @@ export type ParamParser_Generic =
147143
export function defineParamParser<TOut, TIn extends string | string[]>(parser: {
148144
get?: (value: TIn) => TOut
149145
set?: (value: TOut) => TIn
150-
}): Param_GetSet<TIn, TOut> {
146+
}): Param_GetSet<TOut, TIn> {
151147
return parser
152148
}
153149

@@ -189,7 +185,7 @@ export const PATH_PARAM_DEFAULT_PARSER: Param_GetSet = {
189185
*/
190186

191187
export type ParamsFromParsers<P extends Record<string, ParamParser_Generic>> = {
192-
[K in keyof P]: P[K] extends Param_GetSet<infer TIn, infer TOut>
188+
[K in keyof P]: P[K] extends Param_GetSet<infer TOut, infer TIn>
193189
? unknown extends TOut // if any or unknown, use the value of TIn, which defaults to string | string[]
194190
? TIn
195191
: TOut
@@ -199,7 +195,7 @@ export type ParamsFromParsers<P extends Record<string, ParamParser_Generic>> = {
199195
interface MatcherPatternPathCustomParamOptions<
200196
TIn extends string | string[] | null = string | string[] | null,
201197
TOut = string | string[] | null,
202-
> extends Param_GetSet<TIn, TOut> {
198+
> extends Param_GetSet<TOut, TIn> {
203199
repeat?: boolean
204200
// NOTE: not needed because in the regexp, the value is undefined if
205201
// the group is optional and not given
@@ -221,7 +217,7 @@ type ExtractParamTypeFromOptions<TParamsOptions> = {
221217

222218
const IS_INTEGER_RE = /^-?[1-9]\d*$/
223219

224-
export const PARAM_INTEGER = {
220+
export const PARAM_INTEGER_SINGLE = {
225221
get: (value: string) => {
226222
if (IS_INTEGER_RE.test(value)) {
227223
const num = Number(value)
@@ -232,26 +228,46 @@ export const PARAM_INTEGER = {
232228
throw miss()
233229
},
234230
set: (value: number) => String(value),
235-
} satisfies Param_GetSet<string, number>
231+
} satisfies Param_GetSet<number, string>
236232

237233
export const PARAM_NUMBER_OPTIONAL = {
238234
get: (value: string | null) =>
239-
value == null ? null : PARAM_INTEGER.get(value),
235+
value == null ? null : PARAM_INTEGER_SINGLE.get(value),
240236
set: (value: number | null) =>
241-
value != null ? PARAM_INTEGER.set(value) : null,
242-
} satisfies Param_GetSet<string | null, number | null>
237+
value != null ? PARAM_INTEGER_SINGLE.set(value) : null,
238+
} satisfies Param_GetSet<number | null, string | null>
243239

244240
export const PARAM_NUMBER_REPEATABLE = {
245-
get: (value: string[]) => value.map(PARAM_INTEGER.get),
246-
set: (value: number[]) => value.map(PARAM_INTEGER.set),
247-
} satisfies Param_GetSet<string[], number[]>
241+
get: (value: string[]) => value.map(PARAM_INTEGER_SINGLE.get),
242+
set: (value: number[]) => value.map(PARAM_INTEGER_SINGLE.set),
243+
} satisfies Param_GetSet<number[], string[]>
248244

249245
export const PARAM_NUMBER_REPEATABLE_OPTIONAL = {
250246
get: (value: string[] | null) =>
251247
value == null ? null : PARAM_NUMBER_REPEATABLE.get(value),
252248
set: (value: number[] | null) =>
253249
value != null ? PARAM_NUMBER_REPEATABLE.set(value) : null,
254-
} satisfies Param_GetSet<string[] | null, number[] | null>
250+
} satisfies Param_GetSet<number[] | null, string[] | null>
251+
252+
/**
253+
* Native Param parser for integers.
254+
*
255+
* @internal
256+
*/
257+
export const PARAM_PARSER_INTEGER: Param_GetSet<number | number[] | null> = {
258+
get: value =>
259+
Array.isArray(value)
260+
? PARAM_NUMBER_REPEATABLE.get(value)
261+
: value != null
262+
? PARAM_INTEGER_SINGLE.get(value)
263+
: null,
264+
set: value =>
265+
Array.isArray(value)
266+
? PARAM_NUMBER_REPEATABLE.set(value)
267+
: value != null
268+
? PARAM_INTEGER_SINGLE.set(value)
269+
: null,
270+
}
255271

256272
export class MatcherPatternPathCustomParams<
257273
TParamsOptions,

0 commit comments

Comments
 (0)