apps/gateway/src/utils/idempotency.ts
Metadata
- Purpose: Source artifact in the anchor-msp system.
- Domain:
applications - Language:
ts - Bytes: 858
- Lines: 26
- Content hash (short):
78126076 - Source (start): apps/gateway/src/utils/idempotency.ts:1
- Source (end): apps/gateway/src/utils/idempotency.ts:26
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;
}