import { ApplyMiddleware, chain, ExtractMiddleware, MiddlewareChain, MiddlewareTypes, RequestHandler } from 'alien-middleware'; import { type HttpRouteTree } from '../http.js'; import { type RouterResponsePlugin } from '../response.js'; import type { RouteRequestHandlerMap } from '../types/server.js'; export { chain }; /** Configuration for `createRouter`. */ export type RouterConfig = { /** * Base path to prepend to all route patterns. * * @remarks Leading and trailing slashes are normalized so `api`, `/api`, and * `api/` all mount routes under `/api/`. * * @example * ```ts * createRouter({ basePath: 'api/' }) * ``` */ basePath?: string; /** * Enable debug behavior for local development. * * @remarks Debug mode adds an `X-Route-Name` response header for matched * routes, includes specific Zod error messages in `400` validation responses, * and logs missing route handlers to the console. */ debug?: boolean; /** Response codec plugins used for route handler results. */ plugins?: readonly RouterResponsePlugin[]; /** CORS configuration for requests with an `Origin` header. */ cors?: { /** * Allowed origins for CORS requests. * * @remarks Origins may contain wildcards for protocol and subdomain. The * protocol is optional and defaults to `https`. Requests with an `Origin` * header outside this list receive `403`. * * @example * ```ts * allowOrigins: ['example.net', 'https://*.example.com', '*://localhost:3000'] * ``` */ allowOrigins?: string[]; }; }; /** * Fetch-compatible Rouzer handler with chainable middleware and route * registration. */ export interface Router extends RequestHandler, MiddlewareChain { /** * Clone this router and add the given middleware to the end of the chain. * * @returns a new `Router` instance. */ use>(middleware: TMiddleware | null): Router>; /** * Clone this router and add the given HTTP route tree and handlers to the * chain. * * @remarks The handler object mirrors the resource tree. Resource nodes are * nested objects, and action nodes are direct handler functions. * * @returns a new `Router` instance. */ use(routes: TRoutes, handlers: RouteRequestHandlerMap): Router; } /** * Create a Rouzer router that can be mounted as a fetch-compatible handler. * * @param config Optional router configuration for base path, debug behavior, * response plugins, and CORS origin restrictions. * @returns A fetch-compatible handler with `.use(...)` methods for middleware * and route registration. */ export declare function createRouter(config?: RouterConfig): Router>;