import { CacheRuleOptions, HTTPStatus, MatchResult, MatchedRouteRule, MatchedRouteRules, ProxyRuleOptions, RedirectRuleOptions, RouteRuleConfig, RouteRules, RuleHandler, RuleHandlers } from "./_chunks/types.mjs"; import { FindRouteRules, MatcherMemoizeOptions, RouteRuleEntry, RouteRuleLayer, RouteRulesMatcher, RouteRulesMatcherOptions, createMatcherFromFind, createRouteRulesMatcher, memoizeRouteRulesMatcher, mergeMatchedRouteRules } from "./_chunks/match.mjs"; import { EventHandler, Middleware } from "h3"; declare module "h3" { interface H3EventContext { /** Merged route rules matched for the current request (set by `routeRules()`). */ routeRules?: MatchedRouteRules; } } /** Options for the plug-and-play {@link routeRules} middleware. */ interface RouteRulesOptions extends RouteRulesMatcherOptions { /** * Memoize match results per `method + pathname` (composes * {@link memoizeRouteRulesMatcher}) — **enabled by default**, FIFO-capped at * 1024 entries. Memoized results are shared across requests: treat * `event.context.routeRules` and its rule options as read-only. Pass `false` * to resolve every request from scratch (each request gets fresh result * objects), or an options object to tune the entry cap. * @default true */ memoize?: boolean | MatcherMemoizeOptions; } /** * Plug-and-play H3 middleware: matches route rules for each request, exposes the * merged rule map as `event.context.routeRules`, and runs matched rule * middleware (redirect, proxy, headers, basic auth, cache, …) before the route * handler. * * Match results are memoized by default (see {@link RouteRulesOptions.memoize}); * treat `event.context.routeRules` as read-only, or pass `memoize: false`. * * @example * ```ts * import { H3, serve } from "h3"; * import { routeRules } from "h3-rules"; * import { cache } from "h3-rules/cache"; // needed for cache/swr rules (ocache peer) * * const app = new H3(); * app.use( * routeRules( * { * "/blog/**": { swr: 60 }, * "/old/**": { redirect: { to: "/new/**", status: 301 } }, * "/api/**": { cors: true }, * }, * { handlers: { cache } }, * ), * ); * ``` */ declare function routeRules(config: Record, opts?: RouteRulesOptions): Middleware; /** * Normalize user route-rule config into runtime rules. * * Expands the `swr` shortcut, normalizes `cors` (`true` → permissive options * object) and `redirect`/`proxy` string forms, * and attaches a first-class `base` field to `/**` redirect/proxy rules. Keys * may carry a `"METHOD /path"` prefix (see {@link parseRouteKey}); the returned * map is re-keyed in canonical `"METHOD /path"` / `"/path"` form with the path * leading-slash coerced. Unknown/custom keys pass through untouched (data-only * rules). */ declare function normalizeRouteRules(config: Record): Record; /** * Default rule handler registry (base for runtime matchers). Two built-ins are * deliberately absent — each is an opt-in subpath export so its dependency stays * out of bundles that don't use it: * - `cache`: needs a caching implementation — register the ocache-backed one * from `h3-rules/cache` (`handlers: { cache }`) or build your own with * `createCacheRuleHandler` (see `src/rules/cache.ts`). * - `proxy`: pulls in h3's `proxyRequest` — register it from `h3-rules/proxy` * (`handlers: { proxy }`, see `src/proxy.ts`). * * `createRouteRulesMatcher` throws when a rule set uses either without a * registered handler (pass `handlers: { cache: undefined }` / `{ proxy: undefined }` * to opt into data-only). */ declare const ruleHandlers: RuleHandlers; declare const headers: RuleHandler<"headers">; declare const redirect: RuleHandler<"redirect">; declare const basicAuth: RuleHandler<"basicAuth">; declare const cors: RuleHandler<"cors">; /** * Wraps an event handler so its responses are cached. This is the core * injection point: `h3-rules` itself ships no caching implementation — the * ocache-backed one lives in `h3-rules/cache` (optional `ocache` peer), and * consumers with their own cache conventions (e.g. Nitro's unstorage / * `useStorage()` wiring) inject theirs here instead. * * `opts` is the merged rule options plus the generated `group`/`name` key — * advanced implementation options passed through `defaults` reach it as extra * properties (typed at the call site, e.g. ocache's in `h3-rules/cache`). */ type DefineCachedHandler = (handler: EventHandler, opts: CacheRuleOptions) => EventHandler; /** * Options for {@link createCacheRuleHandler}. `defineCachedHandler` is * required — the core has no default caching implementation. */ interface CacheRuleHandlerOptions { /** Creates the cached wrapper for a matched route handler. */ defineCachedHandler: DefineCachedHandler; /** Default options merged into every cache rule (rule options win). */ defaults?: CacheRuleOptions; } /** * Create the `cache` rule handler for a matcher instance from an injected * `defineCachedHandler`. Memoization of wrapped handlers is **instance-scoped** * (a closure `Map`, not a `globalThis` map), so each matcher wraps a given * route exactly once across requests. * * For the ready-made ocache-backed handler, use `h3-rules/cache` instead: * its `cache` export / `createOcacheRuleHandler(opts)` wire ocache with h3's * `toResponse` / `handleCacheHeaders` glue. */ declare function createCacheRuleHandler(opts: CacheRuleHandlerOptions): RuleHandler<"cache">; export { type CacheRuleHandlerOptions, type CacheRuleOptions, type DefineCachedHandler, type FindRouteRules, type HTTPStatus, type MatchResult, type MatchedRouteRule, type MatchedRouteRules, type MatcherMemoizeOptions, type ProxyRuleOptions, type RedirectRuleOptions, type RouteRuleConfig, type RouteRuleEntry, type RouteRuleLayer, type RouteRules, type RouteRulesMatcher, type RouteRulesMatcherOptions, type RouteRulesOptions, type RuleHandler, type RuleHandlers, basicAuth, cors, createCacheRuleHandler, createMatcherFromFind, createRouteRulesMatcher, headers, memoizeRouteRulesMatcher, mergeMatchedRouteRules, normalizeRouteRules, redirect, routeRules, ruleHandlers };