{"version":3,"sources":["../../src/nest/index.ts","../../src/core/errors.ts","../../src/core/request-hash.ts"],"sourcesContent":["import {\n  type CallHandler,\n  type ExecutionContext,\n  Injectable,\n  type NestInterceptor,\n} from \"@nestjs/common\";\nimport type { Observable } from \"rxjs\";\nimport { from, switchMap } from \"rxjs\";\nimport { IdempotencyConflictError, IdempotencyRequestMismatchError } from \"../core/errors.js\";\nimport { hashIdempotencyRequest } from \"../core/request-hash.js\";\nimport type { IdempotencyGuard, IdempotencyScope } from \"../core/types.js\";\n\nexport type IdempotencyInterceptorOptions = {\n  guard: IdempotencyGuard;\n  header?: string;\n  scopeFromContext?: (ctx: ExecutionContext) => IdempotencyScope;\n};\n\n@Injectable()\nexport class IdempotencyInterceptor implements NestInterceptor {\n  constructor(private readonly options: IdempotencyInterceptorOptions) {}\n\n  intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {\n    const req = context.switchToHttp().getRequest<{\n      headers: Record<string, string | string[] | undefined>;\n      body: unknown;\n      method: string;\n      url: string;\n    }>();\n    const header = this.options.header ?? \"Idempotency-Key\";\n    const raw = req.headers[header.toLowerCase()];\n    const key = (Array.isArray(raw) ? raw[0] : raw)?.trim();\n    if (!key) return next.handle();\n\n    const scope =\n      this.options.scopeFromContext?.(context) ??\n      ({ scope: `${req.method} ${req.url}` } satisfies IdempotencyScope);\n\n    return from(hashIdempotencyRequest(req.body)).pipe(\n      switchMap((requestHash) =>\n        from(\n          this.options.guard.runScoped({\n            scope,\n            key,\n            requestHash,\n            fn: () =>\n              new Promise((resolve, reject) => {\n                next.handle().subscribe({\n                  next: (value) => resolve(value),\n                  error: (err) => reject(err),\n                });\n              }),\n          }),\n        ),\n      ),\n    );\n  }\n}\n\nexport function mapIdempotencyError(err: unknown): { status: number; body: unknown } | null {\n  if (err instanceof IdempotencyConflictError) {\n    return { status: 409, body: { error: err.code, message: err.message, key: err.key } };\n  }\n  if (err instanceof IdempotencyRequestMismatchError) {\n    return { status: 409, body: { error: err.code, message: err.message, key: err.key } };\n  }\n  return null;\n}\n","export class IdempotencyConflictError extends Error {\n  readonly code = \"IDEMPOTENCY_CONFLICT\" as const;\n\n  constructor(readonly key: string) {\n    super(`Idempotency key already in progress: ${key}`);\n    this.name = \"IdempotencyConflictError\";\n  }\n}\n\nexport class IdempotencyRequestMismatchError extends Error {\n  readonly code = \"IDEMPOTENCY_REQUEST_MISMATCH\" as const;\n\n  constructor(readonly key: string) {\n    super(`Idempotency key reused with different request body: ${key}`);\n    this.name = \"IdempotencyRequestMismatchError\";\n  }\n}\n","export async function hashIdempotencyRequest(body: unknown): Promise<string> {\n  const text =\n    body === undefined || body === null\n      ? \"\"\n      : typeof body === \"string\"\n        ? body\n        : JSON.stringify(body);\n  const bytes = new TextEncoder().encode(text);\n  const digest = await crypto.subtle.digest(\"SHA-256\", bytes);\n  return [...new Uint8Array(digest)]\n    .map((b) => b.toString(16).padStart(2, \"0\"))\n    .join(\"\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAKO;AAEP,kBAAgC;;;ACPzB,IAAM,2BAAN,cAAuC,MAAM;AAAA,EAGlD,YAAqB,KAAa;AAChC,UAAM,wCAAwC,GAAG,EAAE;AADhC;AAEnB,SAAK,OAAO;AAAA,EACd;AAAA,EAHqB;AAAA,EAFZ,OAAO;AAMlB;AAEO,IAAM,kCAAN,cAA8C,MAAM;AAAA,EAGzD,YAAqB,KAAa;AAChC,UAAM,uDAAuD,GAAG,EAAE;AAD/C;AAEnB,SAAK,OAAO;AAAA,EACd;AAAA,EAHqB;AAAA,EAFZ,OAAO;AAMlB;;;AChBA,eAAsB,uBAAuB,MAAgC;AAC3E,QAAM,OACJ,SAAS,UAAa,SAAS,OAC3B,KACA,OAAO,SAAS,WACd,OACA,KAAK,UAAU,IAAI;AAC3B,QAAM,QAAQ,IAAI,YAAY,EAAE,OAA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