Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,121 @@
import { getDbClient } from "./client.server.js";
import type { IdempotencyKey, Prisma } from "../generated/prisma/client/client.js";
export const DEFAULT_IDEMPOTENCY_SCOPE = "default";
export type IdempotencyResponse = {
status: number;
body?: Prisma.JsonValue;
headers?: Record<string, string>;
};
export type IdempotencyKeyLookup = {
scope?: string | null;
key: string;
};
export type StoredIdempotencyResponse = IdempotencyResponse & {
requestHash: string;
expiresAt: Date | null;
};
export type CreateStoredIdempotencyResponseInput = IdempotencyKeyLookup & {
requestHash: string;
response: IdempotencyResponse;
expiresAt?: Date | null;
};
export type IdempotencyKeyReaderClient = {
idempotencyKey: {
findUnique(args: {
where: {
idempotency_key_scope_key: {
scope: string;
key: string;
};
};
}): Promise<IdempotencyKey | null>;
};
};
export type IdempotencyKeyWriterClient = {
idempotencyKey: {
create(args: {
data: {
scope: string;
key: string;
requestHash: string;
responseStatus: number;
responseBody: Prisma.JsonValue | null;
responseHeaders: Record<string, string> | null;
expiresAt: Date | null;
};
}): Promise<IdempotencyKey>;
};
};
export type IdempotencyKeyClient = IdempotencyKeyReaderClient & IdempotencyKeyWriterClient;
export function normalizeIdempotencyScope(scope?: string | null): string {
return scope?.trim() || DEFAULT_IDEMPOTENCY_SCOPE;
}
export function mapIdempotencyKeyToResponse(row: IdempotencyKey): StoredIdempotencyResponse {
return {
requestHash: row.requestHash,
status: row.responseStatus,
body: row.responseBody ?? undefined,
headers: mapResponseHeaders(row.responseHeaders),
expiresAt: row.expiresAt ?? null,
};
}
export async function findIdempotencyKeyByScopeAndKey(
input: IdempotencyKeyLookup,
client: IdempotencyKeyReaderClient = getDbClient() as unknown as IdempotencyKeyReaderClient
): Promise<IdempotencyKey | null> {
return client.idempotencyKey.findUnique({
where: {
idempotency_key_scope_key: {
scope: normalizeIdempotencyScope(input.scope),
key: input.key,
},
},
});
}
export async function findStoredIdempotencyResponse(
input: IdempotencyKeyLookup,
client: IdempotencyKeyReaderClient = getDbClient() as unknown as IdempotencyKeyReaderClient
): Promise<StoredIdempotencyResponse | null> {
const row = await findIdempotencyKeyByScopeAndKey(input, client);
return row ? mapIdempotencyKeyToResponse(row) : null;
}
export async function createStoredIdempotencyResponse(
input: CreateStoredIdempotencyResponseInput,
client: IdempotencyKeyWriterClient = getDbClient() as unknown as IdempotencyKeyWriterClient
): Promise<IdempotencyKey> {
return client.idempotencyKey.create({
data: {
scope: normalizeIdempotencyScope(input.scope),
key: input.key,
requestHash: input.requestHash,
responseStatus: input.response.status,
responseBody: input.response.body ?? null,
responseHeaders: input.response.headers ?? null,
expiresAt: input.expiresAt ?? null,
},
});
}
export const createIdempotencyKeyResponse = createStoredIdempotencyResponse;
function mapResponseHeaders(headers: Prisma.JsonValue | null): Record<string, string> | undefined {
if (!headers || typeof headers !== "object" || Array.isArray(headers)) {
return undefined;
}
const entries = Object.entries(headers).filter((entry): entry is [string, string] => typeof entry[1] === "string");
return entries.length > 0 ? Object.fromEntries(entries) : undefined;
}