Skip to content

Commit 6d25c26

Browse files
committed
feat: expose param parser type
1 parent 489547f commit 6d25c26

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

packages/router/src/experimental/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type {
4040
MatcherPatternQuery,
4141
MatcherParamsFormatted,
4242
EmptyParams,
43+
ParamParser,
4344
} from './route-resolver/matchers/matcher-pattern'
4445

4546
import type { RouteRecordNormalized } from '../matcher/types'

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class MatcherPatternPathStar
120120
// new MatcherPatternPathStatic('/')
121121
// new MatcherPatternPathStatic('/team')
122122

123-
export interface Param_GetSet<
123+
export interface ParamParser<
124124
TOut = string | string[] | null,
125125
TIn extends string | string[] | null = string | string[] | null,
126126
> {
@@ -129,8 +129,8 @@ export interface Param_GetSet<
129129
}
130130

131131
export type ParamParser_Generic =
132-
| Param_GetSet<any, string>
133-
| Param_GetSet<any, string[]>
132+
| ParamParser<any, string>
133+
| ParamParser<any, string[]>
134134
// TODO: these are possible values for optional params
135135
// | null | undefined
136136

@@ -143,18 +143,18 @@ export type ParamParser_Generic =
143143
export function defineParamParser<TOut, TIn extends string | string[]>(parser: {
144144
get?: (value: TIn) => TOut
145145
set?: (value: TOut) => TIn
146-
}): Param_GetSet<TOut, TIn> {
146+
}): ParamParser<TOut, TIn> {
147147
return parser
148148
}
149149

150150
const PATH_PARAM_DEFAULT_GET = (value: string | string[] | null | undefined) =>
151151
value ?? null
152-
export const PATH_PARAM_SINGLE_DEFAULT: Param_GetSet<string, string> = {}
152+
export const PATH_PARAM_SINGLE_DEFAULT: ParamParser<string, string> = {}
153153

154154
const PATH_PARAM_DEFAULT_SET = (value: unknown) =>
155155
value && Array.isArray(value) ? value.map(String) : String(value)
156156
// TODO: `(value an null | undefined)` for types
157-
export const PATH_PARAM_DEFAULT_PARSER: Param_GetSet = {
157+
export const PATH_PARAM_DEFAULT_PARSER: ParamParser = {
158158
get: PATH_PARAM_DEFAULT_GET,
159159
set: PATH_PARAM_DEFAULT_SET,
160160
}
@@ -185,7 +185,7 @@ export const PATH_PARAM_DEFAULT_PARSER: Param_GetSet = {
185185
*/
186186

187187
export type ParamsFromParsers<P extends Record<string, ParamParser_Generic>> = {
188-
[K in keyof P]: P[K] extends Param_GetSet<infer TOut, infer TIn>
188+
[K in keyof P]: P[K] extends ParamParser<infer TOut, infer TIn>
189189
? unknown extends TOut // if any or unknown, use the value of TIn, which defaults to string | string[]
190190
? TIn
191191
: TOut
@@ -195,7 +195,7 @@ export type ParamsFromParsers<P extends Record<string, ParamParser_Generic>> = {
195195
interface MatcherPatternPathCustomParamOptions<
196196
TIn extends string | string[] | null = string | string[] | null,
197197
TOut = string | string[] | null,
198-
> extends Param_GetSet<TOut, TIn> {
198+
> extends ParamParser<TOut, TIn> {
199199
repeat?: boolean
200200
// NOTE: not needed because in the regexp, the value is undefined if
201201
// the group is optional and not given
@@ -228,33 +228,33 @@ export const PARAM_INTEGER_SINGLE = {
228228
throw miss()
229229
},
230230
set: (value: number) => String(value),
231-
} satisfies Param_GetSet<number, string>
231+
} satisfies ParamParser<number, string>
232232

233233
export const PARAM_NUMBER_OPTIONAL = {
234234
get: (value: string | null) =>
235235
value == null ? null : PARAM_INTEGER_SINGLE.get(value),
236236
set: (value: number | null) =>
237237
value != null ? PARAM_INTEGER_SINGLE.set(value) : null,
238-
} satisfies Param_GetSet<number | null, string | null>
238+
} satisfies ParamParser<number | null, string | null>
239239

240240
export const PARAM_NUMBER_REPEATABLE = {
241241
get: (value: string[]) => value.map(PARAM_INTEGER_SINGLE.get),
242242
set: (value: number[]) => value.map(PARAM_INTEGER_SINGLE.set),
243-
} satisfies Param_GetSet<number[], string[]>
243+
} satisfies ParamParser<number[], string[]>
244244

245245
export const PARAM_NUMBER_REPEATABLE_OPTIONAL = {
246246
get: (value: string[] | null) =>
247247
value == null ? null : PARAM_NUMBER_REPEATABLE.get(value),
248248
set: (value: number[] | null) =>
249249
value != null ? PARAM_NUMBER_REPEATABLE.set(value) : null,
250-
} satisfies Param_GetSet<number[] | null, string[] | null>
250+
} satisfies ParamParser<number[] | null, string[] | null>
251251

252252
/**
253253
* Native Param parser for integers.
254254
*
255255
* @internal
256256
*/
257-
export const PARAM_PARSER_INT: Param_GetSet<number | number[] | null> = {
257+
export const PARAM_PARSER_INT: ParamParser<number | number[] | null> = {
258258
get: value =>
259259
Array.isArray(value)
260260
? PARAM_NUMBER_REPEATABLE.get(value)
@@ -338,7 +338,7 @@ export class MatcherPatternPathCustomParams<
338338
}
339339
const paramName = this.paramsKeys[paramIndex++]
340340
const paramOptions = this.params[paramName]
341-
const value: ReturnType<NonNullable<Param_GetSet['set']>> = (
341+
const value: ReturnType<NonNullable<ParamParser['set']>> = (
342342
paramOptions.set || identityFn
343343
)(params[paramName])
344344

0 commit comments

Comments
 (0)