Skip to content

Commit

Permalink
Merge pull request #14 from kitbagjs/alpha-numeric-params
Browse files Browse the repository at this point in the history
Match alphanumeric params only
  • Loading branch information
pleek91 authored Dec 15, 2023
2 parents 5bd86a0 + 1b7e5a5 commit 66ad903
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
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>

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

0 comments on commit 66ad903

Please sign in to comment.