import { CodecMap, CodecsToRecord, PathLike, RouteParams } from './helpers/common';
import { Codec } from './Codecs';
/**
* Recursively creates a union of string literals from a {@link PathLike}
* string xtracting the path variables sections. I.e. anything that starts with
* `:` on the path.
*
* @param P the {@link PathLike} string
*/
type PathVarsCapture
= P extends `${string}/:${infer P1}/${string}:${infer P2}` ? P1 | PathVarsCapture<`/:${P2}`> : P extends `${string}/:${infer P3}/${string}` ? P3 : P extends `${string}/:${infer P4}` ? P4 : never;
/**
* Conditional constraint for path variables based on the result of capturing
* the literals on the {@link PathLike} string.
*
* @param P the {@link PathLike} string
*/
type PathVars
= PathVarsCapture
extends never ? Record : Record, Codec>;
/**
* Conditionally create the `makeUrl` function based on the codec map of path
* variables and query parameters.
*
* @param V the path vars codec record
* @param Q the query param codec record
*/
type MakeUrl = keyof V extends never ? {
/**
* Creates a raw string URL for the route using the provided
* parameters.
*
* @param params the parameters used to build the route
* @returns the built URL of the route
*/
makeUrl(params?: RouteParams): string;
} : {
/**
* Creates a raw string URL for the route using the provided
* parameters.
*
* @param params the parameters used to build the route
* @returns the built URL of the route
*/
makeUrl(params: RouteParams): string;
};
/**
* A `Routeway` route instance. It may contain more keys with other subroutes.
*
* @param P the {@link PathLike} string
* @param V the path vars codec record
* @param Q the query param codec record
* @param S the record of `Routeway` subroutes
*/
export type Routeway>, Q extends CodecMap = Record>, S extends Record = Record> = MakeUrl & {
/**
* Convenience method that returns the configuration of the route.
*
* @returns an object with all the configuration of the route
*/
$config(): {
/**
* A record of the path variables configuration. The key refers to the name
* of the path variable and the value is the specific codec for the
* variable.
*/
pathVars: V;
/**
* A record of the query parameters configuration. The key refers to the
* name of the query parameters and the value is the specific codec for the
* parameter.
*/
queryParams: Q;
/**
* The template of this route segment. Differently from the `.template()`
* method, this property does not contain the template of the full path,
* but only of the specific route.
*/
segment: P;
/**
* A record of the nested `Routeway` instances of the route (if any).
*/
subRoutes: S;
};
/**
* Parse a raw URL to get the path variables and query parameters from it.
*
* @param uri the raw URL to parse the params from
* @returns an object with the parsed path variables and query parameters
*/
parseUrl(uri: string): {
pathVars: CodecsToRecord;
queryParams: Partial>;
};
/**
* Creates the complete template of the route. Useful when working with other
* routing libraries that need the context of the path with its variables.
*
* @returns the route template
*/
template(): string;
} & S;
type DefinedSubRoutes>> = B extends RoutewaysBuilder ? M extends Record ? {
[K in keyof M]: GetDefinedRoute;
} : never : never;
type GetDefinedRoute = S extends Routeway ? Routeway;
}> : never;
type ResultSubRoutes>, V extends Record> = B extends RoutewaysBuilder ? M extends Record ? {
[K in keyof M]: GetResultRoute;
} : never : never;
type GetResultRoute> = S extends Routeway ? Routeway;
}> : never;
/**
* Conditionally create a route configuratiion based on the `path` property
* string. If the path contains path variables, the `pathVars` property is
* required and it must be defined with path variables names as its keys.
*
* @param N the name of the route
* @param P the {@link PathLike} string
* @param V the path vars codec record
* @param Q the query param codec record
*/
export type PathConfig, Q extends CodecMap> = PathVarsCapture extends never ? {
name: N;
path: P;
queryParams?: Q;
} : {
name: N;
path: P;
pathVars: V;
queryParams?: Q;
};
/**
* Conditionally create a route configuratiion based on the `path` property
* string. If the path contains path variables, the `pathVars` property is
* required and it must be defined with path variables names as its keys.
* Aditionally, this configuration requires a `subRoutes` property.
*
* @param N the name of the route
* @param P the {@link PathLike} string
* @param V the path vars codec record
* @param Q the query param codec record
* @param S the `RoutewaysBuilder` for the subroutes
*/
export type NestConfig, Q extends CodecMap, S extends RoutewaysBuilder>> = PathVarsCapture extends never ? {
name: N;
path: P;
queryParams?: Q;
subRoutes: S;
} : {
name: N;
path: P;
pathVars: V;
queryParams?: Q;
subRoutes: S;
};
/**
* The Routeways builder API.
*/
export declare class RoutewaysBuilder> {
private readonly routes;
constructor(routes: M);
/**
* Create a single path on the route under construction. Single paths do not
* allow nesting and can be considered the latest point of a branch in the
* router.
*
* If you need to nest routes use {@link RoutewaysBuilder.nest() .nest(..)}
* instead.
*
* @param config a configuration object for the route
* @returns the Routeways instance to continue building
*/
path, Codec>, Q extends CodecMap>(config: PathConfig): RoutewaysBuilder<{
[K in keyof M]: M[K];
} & {
[K in N]: Routeway;
}>;
/**
* Create a path on the route under construction that allows creating nested
* routes under it. The `subRoutes` are required in this method, if you need
* to create a terminal route, use {@link RoutewaysBuilder.path() .path(..)}
* instead.
*
* @param config a configuration object for the route
* @returns the Routeways instance to continue building
*/
nest, Q extends CodecMap, S extends RoutewaysBuilder>>(config: NestConfig): RoutewaysBuilder<{
[K in keyof M]: M[K];
} & {
[K in N]: Routeway>;
}>;
/**
* Builds the routes defined by the API and returns a `Routeways` instance
* shaped by the names of the paths.
*
* @returns the built `Routeways` instance
*/
build(): M;
}
export {};