import { RouteRuleConfig } from "./_chunks/types.mjs"; import { MatcherMemoizeOptions } from "./_chunks/match.mjs"; /** * Default `runtimeRules` preset: every built-in rule handler imported from * `h3-rules` under its own name — except the opt-in subpath handlers `cache` * (`h3-rules/cache`) and `proxy` (`h3-rules/proxy`), so their dependencies * (ocache, h3's `proxyRequest`) only enter a compiled bundle when the rule is * used. A caller's `runtimeRules` is merged **over** this (see * {@link resolveRuntimeRules}), so consumers only list additions and overrides — * they never need to re-declare the built-ins. Keep the key set in sync with * `ruleHandlers` (src/rules/index.ts) plus the two subpath handlers. */ declare const DEFAULT_RUNTIME_RULES: Readonly>; /** * Rule names with a built-in runtime handler (the {@link DEFAULT_RUNTIME_RULES} * key set). Data-only / custom rules are serialized without a `handler` * reference. */ declare const RUNTIME_RULE_NAMES: readonly string[]; /** * Where a runtime rule's handler is imported from in generated code: either a * bare module id or `{ source, export }`. With the bare-string form the * `source` module **must have a named export whose identifier equals the rule * key** (`cache: "#nitro/cache"` ⇒ `import { cache } from "#nitro/cache"`); use * the object form's `export` when the export is named something else. The * `source` is always explicit — there is no ambient default module. * * ```ts * // merged over the built-in preset — only list additions/overrides: * runtimeRules: { * cache: "#nitro/cache", // module exports `cache` * isr: { source: "#nitro/rules", export: "handleISR" }, // export named differently * } * ``` */ type RuntimeRuleImport = string | RuntimeRuleImportSpec; interface RuntimeRuleImportSpec { /** Module the handler is imported from. */ source: string; /** * Named export of the handler within `source`. Must be a valid JS identifier * (it becomes an import binding in generated code). Omit it only when the * export is named exactly as the rule key — the default is the rule key * itself, so `source` must then export a member under that name. * @default the rule key */ export?: string; } interface CompileRouteRulesOptions { /** Base URL prefix for all rule patterns (trailing slash trimmed). */ baseURL?: string; /** * Identifier prefix for imported handlers in generated code (handler `name` * binds as `$`). * @default "__ruleHandlers__" */ handlersImportName?: string; /** * Runtime rules that reference a handler (bound `$`) in generated * code, keyed by rule name. Each value is a {@link RuntimeRuleImport} — a * module id, or `{ source, export }`. Merged **over** * {@link DEFAULT_RUNTIME_RULES}, so you only list custom handlers and * built-in source overrides; the built-ins stay registered otherwise. Keys * bind as JS identifiers in generated code, so they must be valid identifiers. * @default DEFAULT_RUNTIME_RULES */ runtimeRules?: Record; /** * Pre-merge each pattern's subsumption chain at compile time so per-request * resolution takes only the most specific matched layer instead of merging * all layers. Exact — but requires a **chain-clean** rule set. Pre-merge is a * throughput optimization, not a correctness requirement, so — unlike the * runtime matcher, which throws — the compiler is **fail-safe**: if the rule * set is not chain-clean (two patterns partially overlap or cannot be * analyzed), it emits a `console.warn` and falls back to plain compilation * instead of failing the build. */ preMerge?: boolean; } /** * Controls the optional ready-to-use matcher export {@link compileRouteRules} * appends alongside `findRouteRules`. `false`/omitted emits no matcher (the * default — take `findRouteRules` and wrap it yourself). Otherwise the module * also exports a matcher wrapping the compiled `findRouteRules`: * * - `true` — `export const matcher = createMatcherFromFind(findRouteRules)`. * - a string — same, but named after the string (e.g. `"routeRulesMatcher"`). * - `{ name?, memoize? }` — rename the export and/or bake in memoization * (`memoizeRouteRulesMatcher(createMatcherFromFind(findRouteRules))`; pass * `memoize: { max }` to tune the cap). `memoizeRouteRulesMatcher` is imported * **only** when `memoize` is set, so an un-memoized matcher export still * tree-shakes it away. * * `createMatcherFromFind` (and, with `memoize`, `memoizeRouteRulesMatcher`) is * imported from `h3-rules` and counts toward {@link CompiledRouteRules.imports}. */ type MatcherExport = boolean | string | { name?: string; memoize?: boolean | MatcherMemoizeOptions; }; /** * {@link compileRouteRules} options — {@link CompileRouteRulesOptions} plus the * whole-module-only `matcher` knob. The lower-level `compileFindRouteRules` / * `compileHandlersImport` entrypoints emit only their one fragment, so they take * the base options; `matcher` is meaningful only when assembling the full module. */ interface CompileModuleOptions extends CompileRouteRulesOptions { /** * Also emit a ready-to-use matcher export wrapping the compiled * `findRouteRules`, so the generated module is directly usable without a * hand-written `createMatcherFromFind` wrapper. See {@link MatcherExport}. * @default false */ matcher?: MatcherExport; } /** * Compiled `findRouteRules` module, split into its two composable parts so a * caller can either take the whole module ({@link code}, or interpolate the * result as a string via `toString()`) or weave it into a larger module — * hoisting {@link imports} alongside its own and inlining {@link body} — without * re-parsing the source. */ interface CompiledRouteRules { /** * Handler import statements the generated code references — one per distinct * source, in deterministic order (the {@link compileHandlersImport} output). * Empty string for a data-only rule set that references no runtime handler. * When a matcher export is requested ({@link CompileModuleOptions.matcher}), * the `createMatcherFromFind` / `memoizeRouteRulesMatcher` import is appended. */ imports: string; /** * The `export const findRouteRules = …;` declaration on its own (no imports), * followed by the `export const = …;` matcher declaration when a * matcher export is requested. References the handler bindings {@link imports} * brings into scope. */ body: string; /** The complete module source — {@link imports} then {@link body}. Same as `toString()`. */ code: string; /** The complete module source ({@link code}), so the result interpolates as a string. */ toString(): string; } /** * Compile a rule set into a complete ESM module exporting `findRouteRules` * (and, with {@link CompileModuleOptions.matcher}, a ready-to-use matcher). * Input is normalized internally — pass authored config (shortcuts included) * or an already-normalized rule set. Returns a {@link CompiledRouteRules}: * `code` (or `String(…)`) is the whole module; `imports`/`body` are its two * halves for callers that compose the codegen into a larger module. */ declare function compileRouteRules(config: Record, opts?: CompileModuleOptions): CompiledRouteRules; /** * Compile a rule set into the source of a `findRouteRules(method, pathname)` * function expression (rou3/compiler `matchAll` output). Input is normalized * internally, identically to {@link compileRouteRules} — pass authored config * directly. Rule entries reference handler constructors as * `$` local bindings — pair with * {@link compileHandlersImport} (which imports exactly those names), and wrap * with `createMatcherFromFind` at runtime: * * ```js * // generated module * import { headers as __ruleHandlers__$headers } from "h3-rules"; * export const findRouteRules = ; * ``` */ declare function compileFindRouteRules(config: Record, opts?: CompileRouteRulesOptions): string; /** * Import statement for the rule handlers used by compiled output: imports * **exactly** the handlers the rule set references (empty string if none), so * unused handlers — and their dependencies (e.g. ocache for `cache`) — stay * tree-shakeable. Each handler's source comes from its `runtimeRules` entry * (`h3-rules` for the built-ins via {@link DEFAULT_RUNTIME_RULES}, except * `cache` from `h3-rules/cache`; consumers like Nitro point individual rules * at their own module to add/override handlers), and each source's module must * have a named export per handler. * Input is normalized internally, identically to {@link compileFindRouteRules}, * so the import reflects the handlers the normalized rules actually reference * (e.g. an `swr` shortcut counts as `cache`). */ declare function compileHandlersImport(config: Record, opts?: CompileRouteRulesOptions): string; export { type CompileModuleOptions, type CompileRouteRulesOptions, type CompiledRouteRules, DEFAULT_RUNTIME_RULES, type MatcherExport, RUNTIME_RULE_NAMES, type RuntimeRuleImport, type RuntimeRuleImportSpec, compileFindRouteRules, compileHandlersImport, compileRouteRules };