{"version":3,"file":"adapter-types-PYYcu5Qa.mjs","names":[],"sources":["../src/adapters/adapter-types.ts"],"sourcesContent":["import { IdsError } from \"../error.js\";\nimport type { Id, ParseError, ParseResult } from \"../types.js\";\n\n/** Discriminated failure value passed to `onError` and emitted to the framework's error handler. */\nexport type IdParamFailure =\n  | { readonly reason: \"brand_mismatch\"; readonly status: number }\n  | { readonly reason: \"malformed\"; readonly status: number };\n\n/** Minimum structural type required by web and ORM adapters. Any codec variant satisfies this — all expose `safeParse`. Adapters only ever call `safeParse` — never key-dependent methods like `extractTimestamp`, `wrap`, or `unwrap`. */\nexport type IdCodec<Brand extends string> = {\n  safeParse(value: unknown): ParseResult<Brand>;\n};\n\n/** Extends {@link IdCodec} with an async `safeVerify` — satisfied by the **Signed Timestamp codec** (native `safeVerify`) and the **Wrapped key codec** (a `safeVerify` alias over `safeUnwrap`; see ADR-0036). HTTP adapters accept `verify: true` only when the codec satisfies this interface (structural type check at the call site). The `safeVerify` method structurally parses `input` first, then verifies the tag; it returns `{ ok: false }` on either failure without throwing. */\nexport type IdVerifiableCodec<Brand extends string> = IdCodec<Brand> & {\n  safeVerify(input: unknown): Promise<{ ok: true; id: Id<Brand> } | { ok: false; error: unknown }>;\n};\n\n/** Re-exported from ORM adapter subpaths (`@smonn/ids/drizzle`, `@smonn/ids/prisma`, `@smonn/ids/kysely`) under the public name; structurally identical to {@link IdCodec}. */\nexport type IdColumnCodec<Brand extends string> = IdCodec<Brand>;\n\n/** Structural interface required by `idField()` (Prisma, MikroORM), the `generatedIdColumn` family (Drizzle), `insertId` (Kysely), and `beforeInsertHook` (TypeORM) — extends {@link IdColumnCodec} with a synchronous `generate()`. Only the **Timestamp codec** and **Reverse Timestamp codec** satisfy this; async-generate codecs (Opaque, Signed, Wrapped, Digest) do not. Re-exported from all five ORM adapter subpaths (`@smonn/ids/drizzle`, `@smonn/ids/kysely`, `@smonn/ids/mikro-orm`, `@smonn/ids/prisma`, `@smonn/ids/typeorm`) as a public type. */\nexport type IdGeneratingCodec<Brand extends string> = IdColumnCodec<Brand> & {\n  generate(): Id<Brand>;\n};\n\n/**\n * Parses `value` as `Id<Brand>` via `codec.safeParse`; throws `IdsError(\"invalid_id\")` on failure. Shared read helper for ORM adapters.\n *\n * **Message body includes the `ParseError` reason** (e.g. `\"invalid ID from database: invalid_base32\"`).\n * This intentionally diverges from the GraphQL adapter's coarsening posture (commit d225ba6): the ORM boundary\n * is a **server-side internal failure** (malformed data already at rest in the database), not a user-facing\n * surface. The reason string is diagnostic information for the developer or operator — it never flows to an\n * HTTP response body. The underlying `ParseError` is also preserved on `cause` for programmatic access.\n */\nexport function readIdColumn<Brand extends string>(\n  codec: IdCodec<Brand>,\n  value: unknown,\n): Id<Brand> {\n  const result = codec.safeParse(value);\n  if (!result.ok) {\n    throw new IdsError(\"invalid_id\", `invalid ID from database: ${result.error}`, {\n      cause: result.error,\n    });\n  }\n  return result.id;\n}\n\n/** Like {@link readIdColumn} but returns `null` when `value` is `null` or `undefined`. Delegates to `readIdColumn` for all other values. */\nexport function readIdColumnNullable<Brand extends string>(\n  codec: IdCodec<Brand>,\n  value: unknown,\n): Id<Brand> | null {\n  if (value === null || value === undefined) {\n    return null;\n  }\n  return readIdColumn(codec, value);\n}\n\n/** Validates `value` via `codec.safeParse` and returns it as a canonical ID string. Throws `IdsError(\"invalid_id\")` on any failure — including null, undefined, or a cast-smuggled arbitrary string — so invalid values are rejected at the write site rather than stored. Shared write helper for ORM adapters. */\nexport function writeIdColumn<Brand extends string>(\n  codec: IdCodec<Brand>,\n  value: Id<Brand>,\n): string {\n  const result = codec.safeParse(value);\n  if (!result.ok) {\n    throw new IdsError(\"invalid_id\", `invalid ID to database: ${result.error}`, {\n      cause: result.error,\n    });\n  }\n  return result.id;\n}\n\n/** Like {@link writeIdColumn} but returns `null` for `null` or `undefined`. Delegates to `writeIdColumn(codec, value)` for all other values. Use for nullable foreign key columns. */\nexport function writeIdColumnNullable<Brand extends string>(\n  codec: IdCodec<Brand>,\n  value: Id<Brand> | null | undefined,\n): string | null {\n  if (value == null) {\n    return null;\n  }\n  return writeIdColumn(codec, value);\n}\n\n/**\n * Maps a `ParseError` to `{ reason, status }` for web adapter failure handling.\n *\n * - `invalid_prefix` → `brand_mismatch` / default 404\n * - anything else → `malformed` / default 400\n * - `options.status[reason]` overrides the default for that reason\n */\nexport function resolveIdParamFailure(\n  error: ParseError,\n  options?: { status?: { brand_mismatch?: number; malformed?: number } },\n): IdParamFailure {\n  const reason = error === \"invalid_prefix\" ? (\"brand_mismatch\" as const) : (\"malformed\" as const);\n  const defaultStatus = reason === \"brand_mismatch\" ? 404 : 400;\n  const status = options?.status?.[reason] ?? defaultStatus;\n  return { reason, status };\n}\n\n/**\n * Maps a `safeVerify` failure to `{ reason: \"malformed\", status }` for web adapter failure handling.\n *\n * - always returns `reason: \"malformed\"`\n * - `options.status.malformed` overrides the default `400`\n */\nexport function resolveVerifyFailure(options?: {\n  status?: { malformed?: number };\n}): IdParamFailure {\n  return { reason: \"malformed\", status: options?.status?.malformed ?? 400 };\n}\n"],"mappings":";;;;;;;;;;;AAmCA,SAAgB,aACd,OACA,OACW;CACX,MAAM,SAAS,MAAM,UAAU,KAAK;CACpC,IAAI,CAAC,OAAO,IACV,MAAM,IAAI,SAAS,cAAc,6BAA6B,OAAO,SAAS,EAC5E,OAAO,OAAO,MAChB,CAAC;CAEH,OAAO,OAAO;AAChB;;AAGA,SAAgB,qBACd,OACA,OACkB;CAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAC9B,OAAO;CAET,OAAO,aAAa,OAAO,KAAK;AAClC;;AAGA,SAAgB,cACd,OACA,OACQ;CACR,MAAM,SAAS,MAAM,UAAU,KAAK;CACpC,IAAI,CAAC,OAAO,IACV,MAAM,IAAI,SAAS,cAAc,2BAA2B,OAAO,SAAS,EAC1E,OAAO,OAAO,MAChB,CAAC;CAEH,OAAO,OAAO;AAChB;;AAGA,SAAgB,sBACd,OACA,OACe;CACf,IAAI,SAAS,MACX,OAAO;CAET,OAAO,cAAc,OAAO,KAAK;AACnC;;;;;;;;AASA,SAAgB,sBACd,OACA,SACgB;CAChB,MAAM,SAAS,UAAU,mBAAoB,mBAA8B;CAC3E,MAAM,gBAAgB,WAAW,mBAAmB,MAAM;CAE1D,OAAO;EAAE;EAAQ,QADF,SAAS,SAAS,WAAW;CACpB;AAC1B;;;;;;;AAQA,SAAgB,qBAAqB,SAElB;CACjB,OAAO;EAAE,QAAQ;EAAa,QAAQ,SAAS,QAAQ,aAAa;CAAI;AAC1E"}