Skip to content

feat(api): add idempotency middleware for POST mutations#272

Open
220nightmore-spec wants to merge 1 commit into
Creditra:mainfrom
220nightmore-spec:feature/idempotency-middleware-179
Open

feat(api): add idempotency middleware for POST mutations#272
220nightmore-spec wants to merge 1 commit into
Creditra:mainfrom
220nightmore-spec:feature/idempotency-middleware-179

Conversation

@220nightmore-spec

@220nightmore-spec 220nightmore-spec commented Jul 9, 2026

Copy link
Copy Markdown

Summary

Closes #179.

  • add an Idempotency-Key middleware for POST mutations with route/principal/request-body scoping
  • add in-memory and Postgres-backed idempotency stores plus the idempotency_keys migration
  • document replay/conflict/concurrency behavior and expose the header in OpenAPI
  • cover pass-through, replay, body-conflict, and concurrent duplicate handling with focused tests

Security notes

  • raw idempotency keys and auth headers are hashed before storage
  • 5xx responses are not cached, so transient failures can be retried
  • same-key/different-body reuse returns 409 Conflict
  • in-memory dev/test store waits for in-flight duplicates; Postgres store returns 409 + Retry-After for in-flight duplicates across instances to avoid duplicate mutation execution

Validation

  • vitest run src/middleware/__tests__/idempotency.test.ts --pool=forks --maxWorkers=1
  • eslint src/middleware/idempotency.ts src/services/idempotencyStore.ts src/middleware/__tests__/idempotency.test.ts
  • node --input-type=module -e "import fs from 'node:fs'; import yaml from 'yaml'; yaml.parse(fs.readFileSync('src/openapi.yaml', 'utf8')); console.log('OpenAPI spec valid');"
  • git diff --check

tsc --noEmit --pretty false is currently blocked by existing unrelated repo errors in files such as src/app.ts, src/container/Container.ts, src/middleware/requestLogger.ts, src/routes/creditBulk.ts, src/routes/simulation.ts, and src/services/drawWebhookService.ts; no idempotency files were reported in that failure list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backend: Add request idempotency middleware (Idempotency-Key) for all POST mutations

1 participant