/** * The API dispatch adapter: matches requests against the flat manifest's * handler refs (`$GET`, `$POST`, …) and serves them as fetch-style * middleware, so API routes compose into any `(request, next)` chain — an * SSR handler's middleware option, a framework's request pipeline — with no * bundler or server involvement. * * Ported from SolidStart's `server/routes.ts` (the radix3 matcher) and the * method dispatch in its `server/handler.ts`. */ /** * The event a handler receives. Structurally the core request event — * `getRequestEvent()` inside the handler sees the same object — with the * matched route params written onto it before dispatch. */ export interface APIEvent { request: Request; params?: Record; [key: string]: unknown; } /** * An API handler: an uppercase method export of a route module. Return a * `Response` to answer the request; strings and JSON values are coerced * (`new Response(text)` / `Response.json(value)`). A `GET` handler may * return `undefined` to decline, falling through to the rest of the chain * when the module is also a page. */ export type APIHandler = (event: APIEvent) => unknown; /** A manifest entry as the delivery adapter serves it. */ export interface FileRouteHandlers { path: string; page?: boolean; $component?: unknown; $HEAD?: APIHandlerRef; $GET?: APIHandlerRef; $POST?: APIHandlerRef; $PUT?: APIHandlerRef; $PATCH?: APIHandlerRef; $DELETE?: APIHandlerRef; [key: string]: unknown; } /** * A handler module ref as delivered: code-split (`import()`) by default, * eager (`require()`) when the delivery adapter runs with code splitting off. */ export type APIHandlerRef = { import(): Promise>; } | { require(): Record; }; export interface APIMatch { handler: APIHandlerRef; params?: Record; /** `true` when the module also renders a page at this path. */ isPage: boolean; } /** * Builds a matcher over the flat manifest's handler-carrying entries. Route * paths translate from the neutral pattern language to the radix tree — * `(group)` segments stripped, `*rest` catch-alls, static segments * URL-encoded. Optional parameters are rejected (a radix tree cannot * represent them) and duplicate paths are a configuration error. */ export declare function createAPIMatcher(routes: readonly FileRouteHandlers[]): (path: string, method: string) => APIMatch | undefined; /** Strips a leading path base: `stripPathBase("/app/x", "/app")` → `/x`. */ export declare function stripPathBase(path: string, base: string): string; export interface APIHandlerOptions { /** A path base stripped from the URL before matching. Defaults to `/`. */ base?: string; /** * How the adapter reaches the request event it hands to handlers. Defaults * to the core request-event scope (the storage `provideRequestEvent` * establishes), which is where a middleware chain runs — override it when * dispatching outside one, or to hand handlers a richer event. */ getEvent?: () => APIEvent; } /** * Fetch-style middleware serving the manifest's API routes: * * ```ts * import routes from "virtual:file-routes"; * import { createAPIHandler } from "filesystem-routing/api"; * * export default [createAPIHandler(routes)]; * ``` * * Dispatch preserves SolidStart's semantics: unmatched requests (no route, * or no handler for the method) advance the chain; `HEAD` falls back to the * `GET` handler; matched params are written to `event.params` before the * handler runs. A handler returning `undefined` declines the request — * allowed for `GET` only (anything else throws): a page module falls * through to the chain (its component renders instead), a handler-only * module answers 404. */ export declare function createAPIHandler(routes: readonly FileRouteHandlers[], options?: APIHandlerOptions): (request: Request, next: (request?: Request) => Response | Promise) => Promise;