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

Path types #16

Merged
merged 2 commits into from
Dec 18, 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
16 changes: 8 additions & 8 deletions src/types/routeMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export type ExtractRouteMethodParams<T> = T extends RouteMethod<infer Params>
export type ExtractParamsFromPath<
TPath extends Route['path']
> = TPath extends Path
? ExtractParamsFromPathString<UnifyParamEnds<TPath['path']>, TPath['params']>
? TPath['params']
: TPath extends string
? ExtractParamsFromPathString<UnifyParamEnds<TPath>>
? Path<TPath, {}>['params']
: never
Comment on lines +61 to 64
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is where you can see rather than extracting param types it just accepts what Path returns for the params type.


type ParamEnd = '/'
Expand All @@ -71,12 +71,12 @@ type UnifyParamEnds<

export type ExtractParamsFromPathString<
TPath extends string,
TParams extends Record<string, Param> = Record<never, never>
> = TPath extends `${infer Path}${ParamEnd}`
TParams extends Record<string, Param | undefined> = Record<never, never>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These | undefined additions just account for the user defined param types being an optional record which adds | undefined to every property.

> = UnifyParamEnds<TPath> extends `${infer Path}${ParamEnd}`
? ExtractParamsFromPathString<Path, TParams>
: TPath extends `${string}:${infer Param}${ParamEnd}${infer Rest}`
: UnifyParamEnds<TPath> extends `${string}:${infer Param}${ParamEnd}${infer Rest}`
? MergeParams<{ [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams> }, ExtractParamsFromPathString<Rest, TParams>>
: TPath extends `${string}:${infer Param}`
: UnifyParamEnds<TPath> extends `${string}:${infer Param}`
Comment on lines +75 to +79
Copy link
Contributor Author

@pleek91 pleek91 Dec 17, 2023

Choose a reason for hiding this comment

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

I moved wrapping the path in UnifyParamEnds into this type so that we don't have to remember wrap any path string we pass into this type in it.

? { [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams> }
: Record<never, never>

Expand Down Expand Up @@ -115,7 +115,7 @@ type ExtractParamName<

type ExtractPathParamType<
TParam extends string,
TParams extends Record<string, Param>
TParams extends Record<string, Param | undefined>
> = TParam extends `?${infer OptionalParam}`
? OptionalParam extends keyof TParams
? ExtractParamType<TParams[OptionalParam]> | undefined
Expand All @@ -124,7 +124,7 @@ type ExtractPathParamType<
? ExtractParamType<TParams[TParam]>
: string

type ExtractParamType<TParam extends Param> = TParam extends ParamGetSet<infer Type>
type ExtractParamType<TParam extends Param | undefined> = TParam extends ParamGetSet<infer Type>
? Type
: TParam extends ParamGetter
? ReturnType<TParam>
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Path<
P extends PathParams<T> = any
> = {
path: T,
params: P,
params: Identity<ExtractParamsFromPathString<T, P>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the implementation of path params. Essentially just moving the ExtractParamsFromPathString type here rather than using it in the route method types.

}

export function path<T extends string, P extends PathParams<T>>(_path: T, _params: Identity<P>): Path<T, P> {
Expand Down