Skip to main content

apps/gateway/src/middleware/idempotency.ts

Metadata

Indexed Symbols

No indexed functions/methods detected in this file.

Markdown Headings (if applicable)

No markdown headings detected.

Source Preview

import type { FastifyPluginAsync } from "fastify";
import { getIdempotencyRecord, putIdempotencyRecord, type IdempotencyRecord } from "../services/idempotency-repository.js";

type IdempotencyContext = {
workspaceId: string;
key: string;
route: string;
method: string;
};

export const idempotencyPlugin: FastifyPluginAsync = async (app) => {
app.decorate("idempotencyStore", {
get: (context: IdempotencyContext) =>
getIdempotencyRecord(context.workspaceId, context.key, context.route, context.method),
set: (context: IdempotencyContext, value: IdempotencyRecord) =>
putIdempotencyRecord(context.workspaceId, context.key, context.route, context.method, value)
});
};

declare module "fastify" {
interface FastifyInstance {
idempotencyStore: {
get(context: IdempotencyContext): Promise<IdempotencyRecord | null>;
set(context: IdempotencyContext, value: IdempotencyRecord): Promise<void>;
};