-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidempotency.ts
More file actions
36 lines (28 loc) · 928 Bytes
/
Copy pathidempotency.ts
File metadata and controls
36 lines (28 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export type IdempotencyRecord = {
seq: number;
at: string;
};
type State = { store: Map<string, IdempotencyRecord> };
const GLOBAL_KEY = Symbol.for('super.webhooks.idempotency');
type GlobalWithState = typeof globalThis & { [GLOBAL_KEY]?: State };
function getState(): State {
const g = globalThis as GlobalWithState;
let existing = g[GLOBAL_KEY];
if (!existing) {
existing = { store: new Map() };
g[GLOBAL_KEY] = existing;
}
return existing;
}
export function compositeKey(partnerSlug: string, idempotencyKey: string): string {
return `${partnerSlug}:${idempotencyKey}`;
}
export function checkIdempotency(key: string): IdempotencyRecord | null {
return getState().store.get(key) ?? null;
}
export function markIdempotent(key: string, record: IdempotencyRecord): void {
getState().store.set(key, record);
}
export function __resetIdempotencyStore(): void {
getState().store = new Map();
}