-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathidempotencyKeys.ts
More file actions
69 lines (61 loc) · 2.38 KB
/
idempotencyKeys.ts
File metadata and controls
69 lines (61 loc) · 2.38 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { IdempotencyKeyOptionsSchema } from "../schemas/api.js";
/**
* Safely parses idempotencyKeyOptions from a database record and extracts the user-provided key.
* Returns the user-provided key if valid options exist, otherwise falls back to the hash.
*
* @param run - Object containing idempotencyKey (the hash) and idempotencyKeyOptions (JSON from DB)
* @returns The user-provided key, the hash as fallback, or null if neither exists
*/
export function getUserProvidedIdempotencyKey(run: {
idempotencyKey: string | null | undefined;
idempotencyKeyOptions: unknown;
}): string | undefined {
const parsed = IdempotencyKeyOptionsSchema.safeParse(run.idempotencyKeyOptions);
if (parsed.success) {
return parsed.data.key;
}
return run.idempotencyKey ?? undefined;
}
/**
* Safely parses idempotencyKeyOptions and extracts the scope.
*
* @param run - Object containing idempotencyKeyOptions (JSON from DB)
* @returns The scope if valid options exist, otherwise undefined
*/
export function extractIdempotencyKeyScope(run: {
idempotencyKeyOptions: unknown;
}): "run" | "attempt" | "global" | undefined {
const parsed = IdempotencyKeyOptionsSchema.safeParse(run.idempotencyKeyOptions);
if (parsed.success) {
return parsed.data.scope;
}
return undefined;
}
export function unsafeExtractIdempotencyKeyScope(run: {
idempotencyKeyOptions: unknown;
}): "run" | "attempt" | "global" | undefined {
const unsafe = run.idempotencyKeyOptions as { scope?: "run" | "attempt" | "global" } | undefined | null;
return unsafe?.scope ?? undefined;
}
/**
* Extracts just the user-provided key from idempotencyKeyOptions, without falling back to the hash.
* Useful for ClickHouse replication where we want to store only the explicit user key.
*
* @param run - Object containing idempotencyKeyOptions (JSON from DB)
* @returns The user-provided key if valid options exist, otherwise undefined
*/
export function extractIdempotencyKeyUser(run: {
idempotencyKeyOptions: unknown;
}): string | undefined {
const parsed = IdempotencyKeyOptionsSchema.safeParse(run.idempotencyKeyOptions);
if (parsed.success) {
return parsed.data.key;
}
return undefined;
}
export function unsafeExtractIdempotencyKeyUser(run: {
idempotencyKeyOptions: unknown;
}): string | undefined {
const unsafe = run.idempotencyKeyOptions as { key?: string } | undefined | null;
return unsafe?.key ?? undefined;
}