-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify extract param type, move check for optional
- Loading branch information
1 parent
d74174b
commit 64ce2bf
Showing
5 changed files
with
51 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,10 @@ | ||
import { ExtractParamName } from '@/types/params' | ||
import { LiteralParam, Param, ParamGetSet, ParamGetter } from '@/types/paramTypes' | ||
import { ExtractRouteParamTypesWithOptional } from '@/types/params' | ||
import { Routes } from '@/types/route' | ||
import { RoutesName, RoutesMap } from '@/types/routesMap' | ||
import { Identity } from '@/types/utilities' | ||
import { MakeOptional } from '@/utilities/makeOptional' | ||
import type { ZodSchema } from 'zod' | ||
|
||
export type RouteGetByKey<TRoutes extends Routes, TKey extends RoutesName<TRoutes>> = RoutesMap<TRoutes>[TKey] | ||
|
||
export type RouteParamsByKey< | ||
TRoutes extends Routes, | ||
TKey extends string | ||
> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>> | ||
|
||
type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extends { | ||
host: { params: infer HostParams extends Record<string, Param> }, | ||
path: { params: infer PathParams extends Record<string, Param> }, | ||
query: { params: infer QueryParams extends Record<string, Param> }, | ||
hash: { params: infer HashParams extends Record<string, Param> }, | ||
} | ||
? ExtractParamTypesWithoutLosingOptional<HostParams & PathParams & QueryParams & HashParams> | ||
: Record<string, unknown> | ||
|
||
type ExtractParamTypesWithoutLosingOptional<TParams extends Record<string, Param>> = Identity<MakeOptional<{ | ||
[K in keyof TParams as ExtractParamName<K>]: ExtractParamTypeWithoutLosingOptional<TParams[K], K> | ||
}>> | ||
|
||
export type ExtractParamTypeWithoutLosingOptional<TParam extends Param, TParamKey extends PropertyKey> = | ||
TParam extends ParamGetSet<infer Type> | ||
? TParamKey extends `?${string}` | ||
? Type | undefined | ||
: Type | ||
: TParam extends ParamGetter | ||
? TParamKey extends `?${string}` | ||
? ReturnType<TParam> | undefined | ||
: ReturnType<TParam> | ||
: TParam extends ZodSchema<infer Type> | ||
? TParamKey extends `?${string}` | ||
? Type | undefined | ||
: Type | ||
: TParam extends LiteralParam | ||
? TParamKey extends `?${string}` | ||
? TParam | undefined | ||
: TParam | ||
: TParamKey extends `?${string}` | ||
? string | undefined | ||
: string | ||
> = ExtractRouteParamTypesWithOptional<RouteGetByKey<TRoutes, TKey>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters