{"version":3,"file":"nestjs.mjs","names":[],"sources":["../src/adapters/nestjs.ts"],"sourcesContent":["import { BadRequestException, HttpException, Injectable, NotFoundException } from \"@nestjs/common\";\nimport type { ArgumentMetadata, PipeTransform } from \"@nestjs/common\";\nimport {\n  type IdCodec,\n  type IdParamFailure,\n  type IdVerifiableCodec,\n  resolveIdParamFailure,\n  resolveVerifyFailure,\n} from \"./adapter-types.js\";\nimport type { Id } from \"../types.js\";\n\nexport type { IdParamFailure };\n\n/**\n * Options for `ParseIdPipe`. All fields are optional.\n *\n * **`onError` constraint:** NestJS `transform()` receives only `value` and `ArgumentMetadata`\n * — there is no HTTP context object. The `onError` hook must throw (or re-throw); it cannot\n * write a response inline the way Hono/Express hooks can.\n *\n * **Default validation is structural** — `safeParse` checks prefix and base32 form but does not\n * verify any cryptographic tag. For IDs using a codec that satisfies {@link IdVerifiableCodec}\n * (the **Signed Timestamp codec** or **Wrapped key codec**), pass `verify: true` via\n * {@link IdParamVerifyOptions} to also authenticate the tag.\n */\nexport type IdParamOptions = {\n  /**\n   * Called instead of throwing when provided. The hook **must** throw or re-throw — it cannot\n   * return a response because `PipeTransform.transform` has no HTTP context.\n   */\n  onError?: (failure: IdParamFailure) => never;\n  /**\n   * Remap the default HTTP status for a failure reason without a full handler.\n   * e.g. `{ brand_mismatch: 400 }` treats both failure kinds as 400.\n   */\n  status?: { brand_mismatch?: number; malformed?: number };\n};\n\n/**\n * Extends {@link IdParamOptions} with opt-in HMAC tag verification.\n * Only accepted when the codec satisfies {@link IdVerifiableCodec} (the **Signed Timestamp codec** or **Wrapped key codec**).\n * When `verify` is truthy, `ParseIdPipe.transform` returns a `Promise` that awaits\n * `codec.safeVerify(raw)`; a tag mismatch is treated as a `\"malformed\"` failure.\n * NestJS supports async pipes natively — the resolved `Id<Brand>` reaches the handler.\n */\nexport type IdParamVerifyOptions = IdParamOptions & {\n  /** When truthy, awaits `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. */\n  verify?: boolean;\n};\n\n/**\n * NestJS pipe that validates an untrusted route param against a codec via `safeParse`.\n *\n * Marked `@Injectable()` via `Injectable()(ParseIdPipe)` at module load time, making it\n * available for NestJS DI.\n *\n * **Default (no options):** throws `NotFoundException` (404) for brand mismatches and\n * `BadRequestException` (400) for malformed IDs.\n *\n * **`options.status`:** remaps the default HTTP status for a reason; when the resolved status\n * differs from the default, the pipe throws `HttpException(reason, status)`.\n *\n * **`options.onError`:** escape hatch for custom error handling. The hook must throw — it\n * cannot return a response because `PipeTransform.transform` has no HTTP context.\n *\n * - **Brand mismatch (`invalid_prefix`) → `reason: \"brand_mismatch\"`, default 404**\n * - **Malformed or missing ID → `reason: \"malformed\"`, default 400**\n *\n * @example\n * ```ts\n * import { ParseIdPipe } from \"@smonn/ids/nestjs\";\n * import { createTimestampId } from \"@smonn/ids\";\n *\n * const usr = createTimestampId(\"usr\");\n *\n * @Controller(\"users\")\n * class UsersController {\n *   @Get(\":id\")\n *   findOne(@Param(\"id\", new ParseIdPipe(usr)) id: Id<\"usr\">) {\n *     return { id }; // Id<\"usr\">, canonical\n *   }\n * }\n * ```\n */\nexport class ParseIdPipe<Brand extends string> implements PipeTransform<\n  unknown,\n  Id<Brand> | Promise<Id<Brand>>\n> {\n  private readonly codec: IdCodec<Brand>;\n  private readonly options: IdParamVerifyOptions | undefined;\n\n  constructor(codec: IdVerifiableCodec<Brand>, options?: IdParamVerifyOptions);\n  constructor(codec: IdCodec<Brand>, options?: IdParamOptions);\n  constructor(codec: IdCodec<Brand>, options?: IdParamVerifyOptions) {\n    this.codec = codec;\n    this.options = options;\n  }\n\n  transform(value: unknown, _metadata: ArgumentMetadata): Id<Brand> | Promise<Id<Brand>> {\n    const result = this.codec.safeParse(value);\n    if (!result.ok) {\n      const failure = resolveIdParamFailure(result.error, this.options);\n      if (this.options?.onError) {\n        this.options.onError(failure);\n      }\n      if (failure.reason === \"brand_mismatch\" && failure.status === 404) {\n        throw new NotFoundException();\n      }\n      if (failure.reason === \"malformed\" && failure.status === 400) {\n        throw new BadRequestException();\n      }\n      throw new HttpException(\n        { statusCode: failure.status, message: failure.reason },\n        failure.status,\n      );\n    }\n    if (this.options?.verify) {\n      return (this.codec as IdVerifiableCodec<Brand>).safeVerify(value).then((verifyResult) => {\n        if (!verifyResult.ok) {\n          const failure = resolveVerifyFailure(this.options);\n          if (this.options?.onError) {\n            this.options.onError(failure);\n          }\n          if (failure.status === 400) {\n            throw new BadRequestException();\n          }\n          throw new HttpException(\n            { statusCode: failure.status, message: failure.reason },\n            failure.status,\n          );\n        }\n        return result.id;\n      });\n    }\n    return result.id;\n  }\n}\n\n// Apply @Injectable() metadata so ParseIdPipe participates in NestJS DI when provided as a class.\n// Using a call instead of the @Injectable() decorator syntax to remain compatible with\n// TypeScript projects that do not enable experimentalDecorators.\nInjectable()(ParseIdPipe as unknown as new (...args: unknown[]) => unknown);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoFA,IAAa,cAAb,MAGE;CACA;CACA;CAIA,YAAY,OAAuB,SAAgC;EACjE,KAAK,QAAQ;EACb,KAAK,UAAU;CACjB;CAEA,UAAU,OAAgB,WAA6D;EACrF,MAAM,SAAS,KAAK,MAAM,UAAU,KAAK;EACzC,IAAI,CAAC,OAAO,IAAI;GACd,MAAM,UAAU,sBAAsB,OAAO,OAAO,KAAK,OAAO;GAChE,IAAI,KAAK,SAAS,SAChB,KAAK,QAAQ,QAAQ,OAAO;GAE9B,IAAI,QAAQ,WAAW,oBAAoB,QAAQ,WAAW,KAC5D,MAAM,IAAI,kBAAkB;GAE9B,IAAI,QAAQ,WAAW,eAAe,QAAQ,WAAW,KACvD,MAAM,IAAI,oBAAoB;GAEhC,MAAM,IAAI,cACR;IAAE,YAAY,QAAQ;IAAQ,SAAS,QAAQ;GAAO,GACtD,QAAQ,MACV;EACF;EACA,IAAI,KAAK,SAAS,QAChB,OAAQ,KAAK,MAAmC,WAAW,KAAK,CAAC,CAAC,MAAM,iBAAiB;GACvF,IAAI,CAAC,aAAa,IAAI;IACpB,MAAM,UAAU,qBAAqB,KAAK,OAAO;IACjD,IAAI,KAAK,SAAS,SAChB,KAAK,QAAQ,QAAQ,OAAO;IAE9B,IAAI,QAAQ,WAAW,KACrB,MAAM,IAAI,oBAAoB;IAEhC,MAAM,IAAI,cACR;KAAE,YAAY,QAAQ;KAAQ,SAAS,QAAQ;IAAO,GACtD,QAAQ,MACV;GACF;GACA,OAAO,OAAO;EAChB,CAAC;EAEH,OAAO,OAAO;CAChB;AACF;AAKA,WAAW,CAAC,CAAC,WAA6D"}