Skip to main content

apps/gateway/src/utils/idempotency.ts

Metadata

Indexed Symbols

  • getIdempotencyKey (line 3, function) - Implements get idempotency key for module behavior.
  • getIdempotencyRouteScope (line 18, function) - Implements get idempotency route scope for module behavior.

Markdown Headings (if applicable)

No markdown headings detected.

Source Preview

import type { FastifyRequest } from "fastify";

export function getIdempotencyKey(request: FastifyRequest): string {
const key = request.headers["idempotency-key"];
if (!key || typeof key !== "string") {
throw new Error("Idempotency-Key header is required");
}
const normalized = key.trim();
if (normalized.length < 8 || normalized.length > 128) {
throw new Error("Idempotency-Key must be between 8 and 128 characters");
}
if (!/^[A-Za-z0-9._:-]+$/.test(normalized)) {
throw new Error("Idempotency-Key contains unsupported characters");
}
return normalized;
}

export function getIdempotencyRouteScope(request: FastifyRequest): string {
const routePath = request.routeOptions.url;
if (routePath && typeof routePath === "string") {
return routePath;
}
const pathOnly = request.url.split("?")[0];
return pathOnly;
}