import { C as MiddlewareInput, I as RouteOptions, N as Route, a as MiddlewareResponseDeclaration, f as ContextFromMiddlewareInput, g as HandlerResult, h as HandlerContext, l as AddedContextFromMiddlewareInput, n as DeclaredMiddleware, r as DefineMiddlewareOptions, t as Router } from "./router-Cwb7ak0J.mjs"; import { n as RoutekitResponse } from "./core-CzUCxvGk.mjs"; import * as v from "valibot"; //#region src/router/routes/valibot/valibot.d.ts /** * Validation error component identifiers for Valibot-backed routes. */ declare const enum ValibotValidationError { REQUEST_BODY = 0, URL_PATH = 1, QUERY_PARAMETERS = 2 } type AnyValibotSchema = v.GenericSchema; type ValibotSchema = AnyValibotSchema | undefined; type ValibotObjectSchema = v.ObjectSchema | undefined>; type ValibotPathSchema = ValibotObjectSchema | undefined; type AnyValibotResponseBodySchemas = Partial>; type ValibotResponseBodySchemas = AnyValibotResponseBodySchemas | undefined; type ResponseValidationMode = false | 'strict' | 'parse'; type ResponseValidationOption = boolean | ResponseValidationMode; type ValibotRouteContext = Ctx & AddedContextFromMiddlewareInput & AddedContextFromMiddlewareInput; type ValibotBuilderMiddlewareInput, RouteMiddleware extends MiddlewareInput> = [BuilderMiddleware] extends [undefined] ? RouteMiddleware : [RouteMiddleware] extends [undefined] ? BuilderMiddleware : MiddlewareInput; /** * Default validation error payload returned by Valibot-backed routes. */ type ValibotValidationErrorBody = { component: 'request_body' | 'url_path' | 'query_parameters'; issues: v.BaseIssue[]; message: string; }; /** * Valibot schema input that mirrors the core route `schema` shape. */ type ValibotRouteSchemaInput = { request?: { query?: QuerySchema; path?: PathSchema; body?: BodySchema; }; response?: { body?: ResponseBodySchemas; }; }; type AnyValibotRouteSchemaInput = ValibotRouteSchemaInput; type InferSchema = Schema extends v.GenericSchema ? v.InferOutput : unknown; type InferResponseBodySchemaInputUnion = [keyof ResponseBodySchemas] extends [never] ? unknown : { [Status in keyof ResponseBodySchemas]: NonNullable extends AnyValibotSchema ? v.InferInput> : never }[keyof ResponseBodySchemas]; type InferResponseBodySchemaOutputUnion = [keyof ResponseBodySchemas] extends [never] ? unknown : { [Status in keyof ResponseBodySchemas]: NonNullable extends AnyValibotSchema ? v.InferOutput> : never }[keyof ResponseBodySchemas]; type InferResponseBodyInput = ResponseBodySchemas extends AnyValibotResponseBodySchemas ? 200 extends keyof ResponseBodySchemas ? InferResponseBodySchemaInputUnion : 'default' extends keyof ResponseBodySchemas ? InferResponseBodySchemaInputUnion : unknown : unknown; type InferResponseBodyOutput = ResponseBodySchemas extends AnyValibotResponseBodySchemas ? 200 extends keyof ResponseBodySchemas ? InferResponseBodySchemaOutputUnion : 'default' extends keyof ResponseBodySchemas ? InferResponseBodySchemaOutputUnion : unknown : unknown; type InferResponseBodyAccepted = InferResponseBodyInput | InferResponseBodyOutput; type NormalizeSchema = Schema extends AnyValibotRouteSchemaInput ? Schema : ValibotRouteSchemaInput; type ExtractBodySchema = NormalizeSchema extends ValibotRouteSchemaInput ? BodySchema : undefined; type ExtractPathSchema = NormalizeSchema extends ValibotRouteSchemaInput ? PathSchema : undefined; type ExtractQuerySchema = NormalizeSchema extends ValibotRouteSchemaInput ? QuerySchema : undefined; type ExtractResponseBodySchemas = NormalizeSchema extends ValibotRouteSchemaInput ? ResponseBodySchemas : undefined; type ValibotMiddlewareDeclarations = { [Status in keyof Schemas]: NonNullable extends AnyValibotSchema ? MiddlewareResponseDeclaration>> : never }; type ValibotDeclaredResponse = { [Status in keyof Schemas]-?: NonNullable extends AnyValibotSchema ? RoutekitResponse>, Status extends number ? Status : number> : never }[keyof Schemas]; type ValibotSchemaMiddlewareContext = ValibotHandlerParams, ExtractPathSchema, ExtractQuerySchema> & { params: ValibotHandlerParams, ExtractPathSchema, ExtractQuerySchema>; }; /** * Validated request inputs exposed to a Valibot-backed handler. */ type ValibotHandlerParams = { path: InferSchema; query: InferSchema; body: InferSchema; }; /** * Context object exposed to a Valibot-backed handler. */ type ValibotHandlerContext = Omit, 'path'> & ValibotHandlerParams & { params: ValibotHandlerParams; }; /** * Validation error handler used when request parsing fails. * * @param component - Request component that failed validation. * @param issues - The Valibot validation issues. * @returns A handler result that should be returned to the client. */ type ValibotValidationErrorHandler = (component: ValibotValidationError, issues: [v.BaseIssue, ...v.BaseIssue[]]) => ValibotDeclaredResponse; /** * Shared defaults that can be applied to Valibot handler helpers. */ type ValibotHandlerDefaults = { /** * Route schema fragments that should be merged into every built route. * Route-level schemas override matching request fields and response status codes. */ schema?: AnyValibotRouteSchemaInput; /** * Whether and how to apply `schema.response.body` to handler responses. * `false` disables response validation, `true` and `'strict'` validate without changing the * response body, and `'parse'` returns the parsed response body. Defaults to `'parse'`. */ validateResponse?: ResponseValidationOption; } & ({ onRequestValidationError?: undefined; validationResponses?: undefined; } | { /** * Override the default request validation error response. */ onRequestValidationError: ValibotValidationErrorHandler; /** * Response schemas returned by `onRequestValidationError`. */ validationResponses: AnyValibotResponseBodySchemas; }); /** * Shared defaults that can be applied to Valibot route helpers. */ type ValibotRouteHelperDefaults = undefined> = ValibotHandlerDefaults & { /** * Middleware applied to every route built by the helper. */ middleware?: BuilderMiddleware; }; /** * Handler signature used by Valibot schema boundaries and route builders. */ type ValibotRouteHandler = (this: Router, ctx: ValibotHandlerContext) => HandlerResult>; /** * Shared options used by Valibot route builders. */ type ValibotHandlerOptions = ValibotHandlerDefaults & { schema?: Schema; handler: ValibotRouteHandler, ExtractPathSchema, ExtractQuerySchema, ExtractResponseBodySchemas, Ctx>; }; /** * Full route options used by Valibot route builders. */ type ValibotRouteOptions = undefined, RouteMiddleware extends MiddlewareInput = undefined> = Omit>, 'handler' | 'schema' | 'middleware'> & ValibotHandlerOptions> & { middleware?: RouteMiddleware; }; /** * Method-specific route options used by Valibot route builders. */ type WithValibotOptions = undefined, RouteMiddleware extends MiddlewareInput = undefined> = Omit>, 'handler' | 'schema' | 'middleware'> & ValibotHandlerOptions> & { middleware?: RouteMiddleware; }; /** * Valibot route builder created by [`createValibotRouteBuilder`]{@link createValibotRouteBuilder}. * * @example * ```ts * const route = createValibotRouteBuilder() * * router.get('/users/:id', route({ * schema: { * request: { * path: v.object({id: v.string()}), * }, * }, * handler: ({path}) => ({id: path.id}), * })) * * router.add(route({ * method: HttpMethod.GET, * path: '/health', * handler: () => ({ok: true}), * })) * ``` */ type ValibotRouteBuilder = undefined> = { /** * Build a full route definition that includes its own path. * * @param options - Full route options including `path`. * @returns A full route definition compatible with [`Router.add`]{@link Router#add}. */ = undefined>(options: ValibotRouteOptions): Route, ValibotRouteContext>; /** * Build method-specific route options that leave the path to the registering router. * * @param options - Method route options without a route path. * @returns Route options compatible with helpers like [`Router.get`]{@link Router#get}. */ = undefined>(options: WithValibotOptions): RouteOptions, ValibotRouteContext>; }; /** * Options for [`defineValibotMiddleware`]{@link defineValibotMiddleware}. * * @typeParam AddedCtx - Context made available to downstream handlers. * @typeParam Ctx - Context required by the middleware. * @typeParam Responses - Status-keyed Valibot schemas for locally originated responses. */ type DefineValibotMiddlewareOptions = { /** * Responses that this middleware may originate. */ responses: Responses; /** * Execute middleware behavior using typed `respond(...)` and `forward(...)` controls. */ run: DefineMiddlewareOptions>['run']; }; /** * Create response-declaring middleware backed by Valibot response schemas. * * @example * ```ts * const requireAuth = defineValibotMiddleware({ * responses: { 401: v.object({ message: v.string() }) }, * run: (_ctx, { respond }) => respond(response({ message: 'Sign in' }, { status: 401 })), * }) * ``` * * @param options - Locally originated response schemas and middleware implementation. * @returns Declared middleware with inferred, runtime-validated terminal responses. * @typeParam AddedCtx - Context made available to downstream handlers. * @typeParam Ctx - Context required by the middleware. * @typeParam Responses - Status-keyed Valibot schemas for locally originated responses. */ declare function defineValibotMiddleware(options: DefineValibotMiddlewareOptions): DeclaredMiddleware; /** * Options for [`valibotSchemaMiddleware`]{@link valibotSchemaMiddleware}. * * @typeParam Schema - Valibot request and downstream response schema declaration. */ type ValibotSchemaMiddlewareBaseOptions = { schema?: Schema; validateResponse?: ResponseValidationOption; }; /** * Options for a Valibot request/response schema boundary. * * A custom request-validation callback must declare the response schemas it can originate. * * @typeParam Schema - Valibot request and downstream response schema declaration. * @typeParam ValidationResponses - Schemas owned by a custom validation-error callback. */ type ValibotSchemaMiddlewareOptions = ValibotSchemaMiddlewareBaseOptions & ([ValidationResponses] extends [undefined] ? { onRequestValidationError?: undefined; validationResponses?: undefined; } : { onRequestValidationError: ValibotValidationErrorHandler>; validationResponses: ValidationResponses; }); /** * Create Valibot middleware that parses request inputs and validates downstream responses. * * @example * ```ts * const parseUserId = valibotSchemaMiddleware({ * schema: { request: { path: v.object({ id: v.string() }) } }, * }) * ``` * * @param options - Request, response, and validation-error schema behavior. * @returns Declared middleware exposing parsed request values to downstream handlers. * @typeParam Schema - Valibot request and downstream response schema declaration. * @typeParam Ctx - Context required before this middleware executes. */ declare function valibotSchemaMiddleware(options: ValibotSchemaMiddlewareOptions): DeclaredMiddleware, Ctx>; /** * Create a Valibot route builder with shared defaults. * * @example * ```ts * const route = createValibotRouteBuilder({ * validateResponse: false, * schema: { * response: { * body: { * 400: v.object({message: v.string()}), * }, * }, * }, * }) * * router.post('/users/:id', route({ * name: 'user.update', * schema: { * request: { * path: v.object({id: v.string()}), * }, * }, * handler: ({path}) => ({id: path.id}), * })) * ``` * * @param defaults - Default schema fragments, response validation, and request validation error handling. * @returns A route builder compatible with method-specific router helpers and full route definitions. */ declare function createValibotRouteBuilder, BuilderCtx extends object = ContextFromMiddlewareInput>(defaults: ValibotRouteHelperDefaults & { middleware: BuilderMiddleware; }): ValibotRouteBuilder; declare function createValibotRouteBuilder = undefined>(defaults?: ValibotRouteHelperDefaults): ValibotRouteBuilder; //#endregion export { ValibotRouteHandler as a, ValibotSchemaMiddlewareOptions as c, ValibotValidationErrorHandler as d, createValibotRouteBuilder as f, ValibotRouteBuilder as i, ValibotValidationError as l, valibotSchemaMiddleware as m, ValibotHandlerContext as n, ValibotRouteHelperDefaults as o, defineValibotMiddleware as p, ValibotHandlerParams as r, ValibotRouteSchemaInput as s, DefineValibotMiddlewareOptions as t, ValibotValidationErrorBody as u };