/** * Inference helpers used by the Router/Client overloads to derive precise * `params` types from the route path + the `params:` schema at compile time. * * The runtime is unchanged JS; these types only exist for IDE / tsc. */ // Client uses `export = Client`, so we have to import it via the // `import('...')` type-only form to get the class. type Client = import('./_generated/lib/client/Client'); // ============================================================================ // Path parameter inference // ============================================================================ /** * Extracts named segments from a route string. * Supports both `:name` and `$name` syntaxes (restnio supports both). * * @example * type T = PathParams<'/dog/:name/feed'>; // { name: string } * type T = PathParams<'/u/:userId/post/:postId'>; // { userId: string; postId: string } * type T = PathParams<'/$name/hi'>; // { name: string } * type T = PathParams<'/'>; // {} */ export type PathParams
=
P extends `${infer _Pre}:${infer Param}/${infer Rest}`
? { [K in Param | keyof PathParams<`/${Rest}`>]: string }
: P extends `${infer _Pre}:${infer Param}`
? { [K in Param]: string }
: P extends `${infer _Pre}$${infer Param}/${infer Rest}`
? { [K in Param | keyof PathParams<`/${Rest}`>]: string }
: P extends `${infer _Pre}$${infer Param}`
? { [K in Param]: string }
: {};
// ============================================================================
// Body / query parameter schema inference
// ============================================================================
/**
* Brand applied to built-in restnio param helpers (`rnio.params.string`,
* `rnio.params.integer`, `rnio.params.email`, etc.) so that the inference layer
* can read the runtime TS type they parse to. Library users can also tag their
* own `ParamDef`s by widening with `as ParamDef & TypedAs = PathParams & InferSchema =
(params: HandlerParams , client: Client) => R | Promise {
func: SmartRouteFunc ;
params?: S;
permissions?: readonly string[];
isActive?: boolean;
}
/** Either a plain function or the full smart def. */
export type SmartRouteArg =
| SmartRouteFunc
| SmartRouteDef ;
= {
[K in keyof S]: S[K] extends { required: true } ? K : never;
}[keyof S];
/** Keys in S whose ParamDef is `{ required: false }` or omitted. */
export type OptionalKeys = Exclude = S extends ParamSchema
? { [K in RequiredKeys]: TypeOfDef }
& { [K in OptionalKeys]?: TypeOfDef }
: {};
// ============================================================================
// Combined params type for a handler
// ============================================================================
/** All params a handler receives: path params + schema-defined body/query params. */
export type HandlerParams;
/**
* Smart `RouteFunc` whose `params` arg is precisely typed from the route path
* and the optional schema. Return type is what the handler returns (or `void`).
*/
export type SmartRouteFunc