Files
mtusi2026lab4/app/src/messages/idempotency/idempotency.ts
T

19 lines
764 B
TypeScript

import { createHash } from 'node:crypto';
import type { CreateMessageDto } from '../dto/create-message.dto.js';
// Contract: ASCII [A-Za-z0-9._:-]{1,128}. Anything else (including a duplicated
// header, which Fastify joins with ", ") is rejected as a client error.
export const IDEMPOTENCY_KEY_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/;
export function messagesPath(chatId: string): string {
return `/api/v1/chats/${chatId}/messages`;
}
// The fingerprint is derived from the validated payload with a fixed field
// order, so neither JSON key order nor an absent optional field can change it.
export function idempotencyFingerprint(input: CreateMessageDto): string {
return createHash('sha256')
.update(JSON.stringify({ text: input.text }))
.digest('hex');
}