Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match alphanumeric params only #14

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/types/routeMethods.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,23 @@ test('public parent routes have correct type for parameters', () => {
param2: string,
param3: boolean,
}]>()
})

test('param names must be alphanumeric', () => {
const routes = [
{
name: 'foo',
path: '/:param1-:param2/:param3_:param4',
component,
},
] as const satisfies Routes

const router = createRouter(routes)

expectTypeOf(router.routes.foo).parameters.toEqualTypeOf<[{
param1: string,
param2: string,
param3: string,
param4: string,
}]>()
})
16 changes: 11 additions & 5 deletions src/types/routeMethods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Param, ParamGetSet, ParamGetter } from '@/types/params'
import { Route, Routes } from '@/types/routes'
import { Identity, IsAny, IsEmptyObject, TupleCanBeAllUndefined, UnionToIntersection } from '@/types/utilities'
import { Identity, IsAny, IsEmptyObject, ReplaceAll, TupleCanBeAllUndefined, UnionToIntersection } from '@/types/utilities'
import { Path } from '@/utilities/path'

export type RouteMethod<
Expand Down Expand Up @@ -58,17 +58,23 @@ export type ExtractRouteMethodParams<T> = T extends RouteMethod<infer Params>
export type ExtractParamsFromPath<
TPath extends Route['path']
> = TPath extends Path
? ExtractParamsFromPathString<TPath['path'], TPath['params']>
? ExtractParamsFromPathString<UnifyParamEnds<TPath['path']>, TPath['params']>
: TPath extends string
? ExtractParamsFromPathString<TPath>
? ExtractParamsFromPathString<UnifyParamEnds<TPath>>
: never

type ParamEnd = '/'

type UnifyParamEnds<
TPath extends string
> = ReplaceAll<ReplaceAll<TPath, '-', ParamEnd>, '_', ParamEnd>
Comment on lines +68 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than nesting ReplaceAll, could we use a union?

ReplaceAll<TPath, '-' | '_', ParamEnd>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time you have a union the type will produce a union. And we want to replace each character individually so the output type is a single template literal.


export type ExtractParamsFromPathString<
TPath extends string,
TParams extends Record<string, Param> = Record<never, never>
> = TPath extends `${infer Path}/`
> = TPath extends `${infer Path}${ParamEnd}`
? ExtractParamsFromPathString<Path, TParams>
: TPath extends `${string}:${infer Param}/${infer Rest}`
: TPath extends `${string}:${infer Param}${ParamEnd}${infer Rest}`
? MergeParams<{ [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams> }, ExtractParamsFromPathString<Rest, TParams>>
: TPath extends `${string}:${infer Param}`
? { [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams> }
Expand Down
11 changes: 10 additions & 1 deletion src/types/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ export type UnionToIntersection<Union> = (
? Intersection & Union
: never

export type MaybeLazy<T> = T | (() => Promise<T>)
export type MaybeLazy<T> = T | (() => Promise<T>)

// Copied and modified from [type-fest](https://github.com/sindresorhus/type-fest/blob/main/source/replace.d.ts)
export type ReplaceAll<
Input extends string,
Search extends string,
Replacement extends string
> = Input extends `${infer Head}${Search}${infer Tail}`
? `${Head}${Replacement}${ReplaceAll<Tail, Search, Replacement>}`
: Input