import { t as Id } from "./types-hGBnCpJj.mjs"; import { a as IdVerifiableCodec, i as IdParamFailure, t as IdCodec } from "./adapter-types-B-GLKPOm.mjs"; import { HTTPException } from "hono/http-exception"; import { ContentfulStatusCode } from "hono/utils/http-status"; import { Context, MiddlewareHandler } from "hono"; //#region src/adapters/hono.d.ts /** * Typed error thrown into Hono's `app.onError` on validation failure. * Inspect `err.reason` and `err.status` in your error handler. */ declare class IdParamError extends HTTPException { readonly reason: "brand_mismatch" | "malformed"; constructor(reason: "brand_mismatch" | "malformed", status: ContentfulStatusCode); } /** * Options for `idParam` and `idQuery`. All fields are optional. * * **Default validation is structural** — `safeParse` checks prefix and base32 form but does not * verify any cryptographic tag. For IDs using a codec that satisfies {@link IdVerifiableCodec} * (the **Signed Timestamp codec** or **Wrapped key codec**), set `verify: true` to also authenticate the tag. */ type IdParamOptions = { /** * Called instead of throwing when provided. The hook owns the response entirely — * the adapter neither throws nor writes a body. */ onError?: (failure: IdParamFailure, c: Context) => Response | Promise; /** * Remap the default HTTP status for a failure reason without a full handler. * e.g. `{ brand_mismatch: 400 }` treats both failure kinds as 400. */ status?: { brand_mismatch?: ContentfulStatusCode; malformed?: ContentfulStatusCode; }; }; /** * Extends {@link IdParamOptions} with opt-in HMAC tag verification. * Only accepted when the codec satisfies {@link IdVerifiableCodec} (the **Signed Timestamp codec** or **Wrapped key codec**). * When `verify` is truthy, the adapter calls `codec.safeVerify(raw)` after structural parse succeeds; * a tag mismatch is routed through the existing `"malformed"` failure path. */ type IdParamVerifyOptions = IdParamOptions & { /** When truthy, calls `codec.safeVerify(raw)` after structural parse and treats a tag failure as `"malformed"`. Accepts any boolean so computed flags (`{ verify: cfg.verifyIds }`) compile without casting. */verify?: boolean; }; /** * Hono middleware that validates a named route param against a codec via `safeParse`. * * **Default (no options):** throws `IdParamError` (extends `HTTPException`) carrying both the HTTP * status and `reason` so the app's existing `onError` handler can discriminate by reason. The * adapter does not write a response body itself. * * **`options.onError`:** when provided, the hook owns the response entirely — the adapter neither * throws nor writes a response. * * **`options.status`:** remaps the default HTTP status for a reason without a full handler. * * - **Brand mismatch (`invalid_prefix`) → `reason: "brand_mismatch"`, default 404** * - **Malformed or missing ID → `reason: "malformed"`, default 400** * * On success, stores the canonical `Id` in the Hono context under `paramName` * and calls `next()`. * * @example * ```ts * import { idParam, IdParamError } from "@smonn/ids/hono"; * import { createTimestampId } from "@smonn/ids"; * * const usr = createTimestampId("usr"); * * // Default: throws IdParamError (extends HTTPException) → app.onError renders it * app.get("/users/:id", idParam("id", usr), (c) => { * const id = c.get("id"); // Id<"usr">, canonical * }); * * // Discriminate by reason in app.onError * app.onError((err, c) => { * if (err instanceof IdParamError) { * return c.json({ error: err.reason }, err.status); // err.reason: "brand_mismatch" | "malformed" * } * return c.json({ error: "internal" }, 500); * }); * * // Override: consumer fully owns the response * app.get("/orgs/:id", idParam("id", org, { * onError: (failure, c) => c.json({ error: failure.reason }, failure.status), * }), handler); * * // Or a lightweight status remap without a full handler * app.get("/things/:id", idParam("id", thing, { status: { brand_mismatch: 400 } }), handler); * ``` */ declare function idParam(paramName: ParamKey, codec: IdVerifiableCodec, options?: IdParamVerifyOptions): MiddlewareHandler<{ Variables: Record>; }>; declare function idParam(paramName: ParamKey, codec: IdCodec, options?: IdParamOptions): MiddlewareHandler<{ Variables: Record>; }>; /** * Hono middleware that validates a named query-string param against a codec via `safeParse`. * * Same failure contract as `idParam` — same `IdParamFailure` shape, same `onError` / `status` * options — but reads `c.req.query(queryName)` instead of `c.req.param(queryName)`. * * **Default (no options):** throws `IdParamError` (extends `HTTPException`) carrying both the HTTP * status and `reason` so the app's existing `onError` handler can discriminate by reason. The * adapter does not write a response body itself. * * **`options.onError`:** when provided, the hook owns the response entirely — the adapter neither * throws nor writes a response. * * **`options.status`:** remaps the default HTTP status for a reason without a full handler. * * - **Brand mismatch (`invalid_prefix`) → `reason: "brand_mismatch"`, default 404** * - **Malformed or missing query param → `reason: "malformed"`, default 400** * * On success, stores the canonical `Id` in the Hono context under `queryName` * and calls `next()`. * * @example * ```ts * import { idQuery, IdParamError } from "@smonn/ids/hono"; * import { createTimestampId } from "@smonn/ids"; * * const usr = createTimestampId("usr"); * * // Default: throws IdParamError (extends HTTPException) → app.onError renders it * // GET /users?userId=usr_... * app.get("/users", idQuery("userId", usr), (c) => { * const userId = c.get("userId"); // Id<"usr">, canonical * }); * * // Discriminate by reason in app.onError * app.onError((err, c) => { * if (err instanceof IdParamError) { * return c.json({ error: err.reason }, err.status); // err.reason: "brand_mismatch" | "malformed" * } * return c.json({ error: "internal" }, 500); * }); * * // Override: consumer fully owns the response * app.get("/search", idQuery("cursor", usr, { * onError: (failure, c) => c.json({ error: failure.reason }, failure.status), * }), handler); * ``` */ declare function idQuery(queryName: ParamKey, codec: IdVerifiableCodec, options?: IdParamVerifyOptions): MiddlewareHandler<{ Variables: Record>; }>; declare function idQuery(queryName: ParamKey, codec: IdCodec, options?: IdParamOptions): MiddlewareHandler<{ Variables: Record>; }>; //#endregion export { IdParamError, type IdParamFailure, IdParamOptions, IdParamVerifyOptions, idParam, idQuery }; //# sourceMappingURL=hono.d.mts.map