/** * @jorvel/runtime — route middleware (`middleware.ts` convention) * * Runtime-agnostic middleware chain that runs before a route renders/responds. * Use it for auth gating, geo/locale redirects, A/B bucketing, and rewrites — * the same primitive works on the edge (SSR adapters), the Node server, and the * client router. * * A middleware receives a {@link MiddlewareContext} and returns one of: * - `next()` — continue to the next middleware / render the route * - `redirect(to)` — stop and send the user elsewhere (3xx on the server) * - `rewrite(to)` — render a different path without changing the URL * - `respond(res)` — short-circuit with a Response (server/edge only) * - `undefined` — treated as `next()` (lets you write guard-style fns) */ export type MiddlewareDecision = { type: 'next'; headers?: Record; } | { type: 'redirect'; to: string; status: 301 | 302 | 303 | 307 | 308; } | { type: 'rewrite'; to: string; } | { type: 'respond'; response: Response; }; export interface MiddlewareContext { /** Request pathname (no origin), e.g. `/dashboard/settings`. */ pathname: string; /** Parsed search params for the incoming URL. */ searchParams: URLSearchParams; /** Full request URL when available (server/edge). */ url?: URL; /** The raw request on server/edge runtimes. Absent on the client. */ request?: Request; /** Mutable bag for passing data between middlewares in one run. */ readonly state: Record; } export type Middleware = (ctx: MiddlewareContext) => MiddlewareDecision | void | Promise; /** Continue the chain. Optionally attach response headers (server/edge). */ export declare function next(headers?: Record): MiddlewareDecision; /** Stop and redirect. Default status 307 (preserves method, like Next.js). */ export declare function redirect(to: string, status?: 301 | 302 | 303 | 307 | 308): MiddlewareDecision; /** Render a different path while keeping the visible URL unchanged. */ export declare function rewrite(to: string): MiddlewareDecision; /** Short-circuit with a fully-formed Response (server/edge only). */ export declare function respond(response: Response): MiddlewareDecision; /** * Identity helper for authoring a `middleware.ts` default export with full type * inference and editor hints. `export default defineMiddleware(async (ctx) => …)`. */ export declare function defineMiddleware(fn: Middleware): Middleware; export interface MatchedMiddleware { /** Glob-ish matchers. `*` = one segment, `**` = any depth. Default: all. */ matcher?: string | string[]; handler: Middleware; } export interface RunMiddlewareInit { pathname: string; searchParams?: URLSearchParams; url?: URL; request?: Request; state?: Record; } /** * Run a middleware chain for one request. Returns the first terminal decision * (`redirect` / `rewrite` / `respond`), or a coalesced `next` (merging any * headers contributed along the way) when every middleware passes. * * Throws are NOT swallowed — let your server/error boundary handle them. */ export declare function runMiddleware(chain: Array, init: RunMiddlewareInit): Promise; //# sourceMappingURL=middleware.d.ts.map