@powership/server
Version:
23 lines (22 loc) • 1.25 kB
TypeScript
import { IsKnown } from '@powership/utils';
/**
* A wrapper around UrlPattern with improved typings
* @param path
* see: https://github.com/snd/url-pattern
*/
export declare function createRouteMatcher<Path extends string>(path: Path): RouteMatcher<Path>;
export interface RouteMatcher<Path extends string> {
stringify: (params: GetRouteParams<Path>) => string;
match(route: string): GetRouteParams<Path> | null;
}
export type AlphaNumeric = string | number;
export type ExtractRouteParams<Path extends string> = Path extends `:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<Rest> : Path extends `:${infer Param}` ? Param : Path extends `${string}:${infer Rest}` ? ExtractRouteParams<`:${Rest}`> : never;
export type GetRouteParams<Path extends string> = IsKnown<Path> extends 0 ? {} : [ExtractRouteParams<Path>] extends [never] ? {} : {
[K in ExtractRouteParams<Path>]: K extends `${string}?` ? AlphaNumeric | undefined : AlphaNumeric;
} extends infer Parsed ? {
[K in ExcludeOptionalSymbol<keyof Parsed>]: Parsed extends {
[KK in `${K}?`]: any;
} ? AlphaNumeric | undefined : AlphaNumeric;
} : never;
type ExcludeOptionalSymbol<T> = Extract<T extends `${infer Value}?` ? Value : T, string>;
export {};