@deliverd/sdk 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +654 -654
- package/dist/dev.d.ts.map +1 -1
- package/dist/dev.js +2 -0
- package/dist/released.d.ts +1 -1
- package/dist/released.d.ts.map +1 -1
- package/dist/released.js +459 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +59 -59
- package/src/adapters/ai-sdk.ts +59 -59
- package/src/adapters/claude-agent-sdk.ts +90 -90
- package/src/adapters/index.ts +23 -23
- package/src/adapters/langgraph.ts +202 -202
- package/src/adapters/openai-agents.ts +112 -112
- package/src/adapters/shared.ts +179 -179
- package/src/approvals.ts +342 -342
- package/src/collections.ts +247 -247
- package/src/dev.ts +782 -780
- package/src/duration.ts +21 -21
- package/src/errors.ts +62 -62
- package/src/flows.ts +222 -222
- package/src/gates.ts +231 -231
- package/src/http.ts +217 -217
- package/src/index.ts +444 -444
- package/src/messages.ts +161 -161
- package/src/paging.ts +64 -64
- package/src/released.ts +2982 -2523
- package/src/reports.ts +490 -490
- package/src/reviews.ts +236 -236
- package/src/types.ts +933 -921
- package/src/webhooks.ts +117 -117
package/src/gates.ts
CHANGED
|
@@ -1,231 +1,231 @@
|
|
|
1
|
-
import { DeliverdError, DeliverdTimeoutError } from "./errors.js";
|
|
2
|
-
import type { HttpClient } from "./http.js";
|
|
3
|
-
import type { CreateGateInput, GateData, GateDecision } from "./types.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Ask before you act.
|
|
7
|
-
*
|
|
8
|
-
* The other primitives in this package are an agent asking a person to look at
|
|
9
|
-
* work it has already done. This one asks first, and the answer comes from
|
|
10
|
-
* policy the developer does not own — which is the point: what needs a human is
|
|
11
|
-
* an organisation's decision, not a line in the agent's code.
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* const decision = await deliverd.gate({
|
|
15
|
-
* action: "finance.refund",
|
|
16
|
-
* externalId: `refund:${order.id}`,
|
|
17
|
-
* input: { customer_id: order.customerId, amount: 1240, currency: "GBP" },
|
|
18
|
-
* });
|
|
19
|
-
* if (!decision.allowed) return;
|
|
20
|
-
* await stripe.refunds.create(decision.input);
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* **Use `decision.input`, not your own object.** It is what the action's schema
|
|
24
|
-
* validated and what an approver actually saw — and once decision tokens land
|
|
25
|
-
* it is what execution will be checked against. Passing the original is right
|
|
26
|
-
* until the day somebody edits a value before approving, and then it is a
|
|
27
|
-
* silent bug that spends an approval for £1200 on a refund of £1240.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
/** How long between polls while waiting on a person. */
|
|
31
|
-
const POLL_SCHEDULE = [250, 750, 2_000, 4_000, 8_000, 15_000, 30_000] as const;
|
|
32
|
-
|
|
33
|
-
export interface GateWaitOptions {
|
|
34
|
-
/** Give up after this many milliseconds. Without one, waits indefinitely. */
|
|
35
|
-
timeout?: number;
|
|
36
|
-
signal?: AbortSignal;
|
|
37
|
-
/** Called on every poll, for a progress line. */
|
|
38
|
-
onPoll?: (gate: GateData) => void;
|
|
39
|
-
pollIntervals?: readonly number[];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface GateOptions extends GateWaitOptions {
|
|
43
|
-
/** The registered action: `finance.refund`. */
|
|
44
|
-
action: string;
|
|
45
|
-
input: Record<string, unknown>;
|
|
46
|
-
externalId: string;
|
|
47
|
-
environment?: string;
|
|
48
|
-
reason?: string;
|
|
49
|
-
flowId?: string;
|
|
50
|
-
/**
|
|
51
|
-
* Whether to block when policy sends it to a person. Default true.
|
|
52
|
-
*
|
|
53
|
-
* False returns the pending decision immediately, which is what you want
|
|
54
|
-
* inside a request handler that must not hold a connection open for an hour.
|
|
55
|
-
* `deliverd.gates.create()` is the same thing said more plainly.
|
|
56
|
-
*/
|
|
57
|
-
wait?: boolean;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* A decision, and what you can do with it.
|
|
62
|
-
*
|
|
63
|
-
* The data is a snapshot; `wait()` and `refresh()` replace it in place, so a
|
|
64
|
-
* handle held across a wait reads the settled decision rather than the pending
|
|
65
|
-
* one you created.
|
|
66
|
-
*/
|
|
67
|
-
export class Gate {
|
|
68
|
-
constructor(
|
|
69
|
-
private readonly http: HttpClient,
|
|
70
|
-
private data: GateData
|
|
71
|
-
) {}
|
|
72
|
-
|
|
73
|
-
get id(): string { return this.data.id; }
|
|
74
|
-
get status(): GateData["status"] { return this.data.status; }
|
|
75
|
-
/**
|
|
76
|
-
* The one to branch on. True when a rule permitted it **or** a person agreed
|
|
77
|
-
* to it; false for denied, rejected, expired, cancelled and while pending.
|
|
78
|
-
* An expiry is not permission — nobody said yes.
|
|
79
|
-
*/
|
|
80
|
-
get allowed(): boolean { return this.data.allowed; }
|
|
81
|
-
/** Still waiting on a person. */
|
|
82
|
-
get pending(): boolean { return this.data.status === "pending"; }
|
|
83
|
-
/** Refused by a rule, as against rejected by a person. */
|
|
84
|
-
get denied(): boolean { return this.data.status === "denied"; }
|
|
85
|
-
/** What to execute with: the input as the action's schema validated it. */
|
|
86
|
-
get input(): Record<string, unknown> { return this.data.input; }
|
|
87
|
-
/** Why, in the words of whoever wrote the rule. */
|
|
88
|
-
get reason(): string | null { return this.data.reason; }
|
|
89
|
-
/** The rule that decided, by name. Null when nothing matched it. */
|
|
90
|
-
get rule(): string | null { return this.data.rule; }
|
|
91
|
-
/** Where a person decides, when one was asked. */
|
|
92
|
-
get approvalUrl(): string | null { return this.data.approvalUrl; }
|
|
93
|
-
get approvalId(): string | null { return this.data.approvalId; }
|
|
94
|
-
/** True when this call read back a decision already made under your id. */
|
|
95
|
-
get reused(): boolean { return this.data.reused; }
|
|
96
|
-
|
|
97
|
-
/** Everything the API returned, if you want a field the getters do not name. */
|
|
98
|
-
toJSON(): GateData { return this.data; }
|
|
99
|
-
|
|
100
|
-
/** The answer, flattened — what `gate()` hands back. */
|
|
101
|
-
toDecision(): GateDecision {
|
|
102
|
-
return {
|
|
103
|
-
allowed: this.data.allowed,
|
|
104
|
-
status: this.data.status,
|
|
105
|
-
reason: this.data.reason,
|
|
106
|
-
rule: this.data.rule,
|
|
107
|
-
input: this.data.input,
|
|
108
|
-
changes: this.data.changes ?? [],
|
|
109
|
-
id: this.data.id,
|
|
110
|
-
approvalUrl: this.data.approvalUrl,
|
|
111
|
-
gate: this,
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async refresh(signal?: AbortSignal): Promise<this> {
|
|
116
|
-
this.data = await this.http.request<GateData>({
|
|
117
|
-
method: "GET",
|
|
118
|
-
path: `/gates/${this.data.id}`,
|
|
119
|
-
signal,
|
|
120
|
-
});
|
|
121
|
-
return this;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Block until a person decides, then return.
|
|
126
|
-
*
|
|
127
|
-
* Resolves on every settled state — it does not throw on a refusal, because
|
|
128
|
-
* a no is an answer and `if (gate.allowed)` is the line that should follow.
|
|
129
|
-
* It throws only when the wait itself failed: a timeout, an abort, or the
|
|
130
|
-
* API being unreachable.
|
|
131
|
-
*
|
|
132
|
-
* Returns immediately when policy already settled it, which is the common
|
|
133
|
-
* case: most gated calls are allowed or denied by a rule and never reach a
|
|
134
|
-
* person at all.
|
|
135
|
-
*/
|
|
136
|
-
async wait(options: GateWaitOptions = {}): Promise<this> {
|
|
137
|
-
const deadline = options.timeout !== undefined ? Date.now() + options.timeout : undefined;
|
|
138
|
-
let poll = 0;
|
|
139
|
-
|
|
140
|
-
if (!this.pending) return this;
|
|
141
|
-
|
|
142
|
-
for (;;) {
|
|
143
|
-
if (options.signal?.aborted) {
|
|
144
|
-
throw new DeliverdError(0, "aborted", "The wait was aborted.", undefined, {
|
|
145
|
-
cause: options.signal.reason,
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
if (deadline !== undefined && Date.now() >= deadline) {
|
|
149
|
-
throw new DeliverdTimeoutError(
|
|
150
|
-
`Nobody decided the ${this.data.rule ? `"${this.data.rule}" ` : ""}gate on ` +
|
|
151
|
-
`"${this.data.input.action ?? this.id}" within the timeout. ` +
|
|
152
|
-
(this.data.approvalUrl
|
|
153
|
-
? `It is still open at ${this.data.approvalUrl}.`
|
|
154
|
-
: "It is still open.")
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const delay = pollDelay(poll++, options.pollIntervals ?? POLL_SCHEDULE);
|
|
159
|
-
const remaining = deadline === undefined ? delay : Math.min(delay, deadline - Date.now());
|
|
160
|
-
await sleep(Math.max(0, remaining), options.signal);
|
|
161
|
-
|
|
162
|
-
await this.refresh(options.signal);
|
|
163
|
-
options.onPoll?.(this.data);
|
|
164
|
-
|
|
165
|
-
if (!this.pending) return this;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/** ±25% jitter, so a fleet of agents does not poll in lockstep. */
|
|
171
|
-
function pollDelay(attempt: number, schedule: readonly number[]): number {
|
|
172
|
-
const base = schedule[Math.min(attempt, schedule.length - 1)] ?? 30_000;
|
|
173
|
-
return Math.round(base * (0.75 + Math.random() * 0.5));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
177
|
-
return new Promise((resolve, reject) => {
|
|
178
|
-
if (signal?.aborted) {
|
|
179
|
-
return reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
180
|
-
}
|
|
181
|
-
const timer = setTimeout(() => {
|
|
182
|
-
signal?.removeEventListener("abort", onAbort);
|
|
183
|
-
resolve();
|
|
184
|
-
}, ms);
|
|
185
|
-
const onAbort = () => {
|
|
186
|
-
clearTimeout(timer);
|
|
187
|
-
reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
188
|
-
};
|
|
189
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export class Gates {
|
|
194
|
-
constructor(private readonly http: HttpClient) {}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Raise a decision and return straight away, even if it went to a person.
|
|
198
|
-
*
|
|
199
|
-
* The non-blocking half of `gate()`. Use it when the work resumes on a
|
|
200
|
-
* webhook or a later poll rather than inside this call.
|
|
201
|
-
*/
|
|
202
|
-
async create(input: CreateGateInput, signal?: AbortSignal): Promise<Gate> {
|
|
203
|
-
const data = await this.http.request<GateData>({
|
|
204
|
-
method: "POST",
|
|
205
|
-
path: "/gates",
|
|
206
|
-
body: input,
|
|
207
|
-
/*
|
|
208
|
-
* The caller's own id, as the HTTP idempotency key too.
|
|
209
|
-
*
|
|
210
|
-
* The server enforces reuse against `gate_requests.external_id`, so this
|
|
211
|
-
* is belt and braces rather than the mechanism — but it means a retry
|
|
212
|
-
* inside the transport layer and a retry by the caller's own code are the
|
|
213
|
-
* same key, instead of one guarding the request and the other the
|
|
214
|
-
* decision.
|
|
215
|
-
*/
|
|
216
|
-
idempotencyKey: input.externalId,
|
|
217
|
-
signal,
|
|
218
|
-
});
|
|
219
|
-
return new Gate(this.http, data);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/** One decision by its id. */
|
|
223
|
-
async get(id: string, signal?: AbortSignal): Promise<Gate> {
|
|
224
|
-
const data = await this.http.request<GateData>({
|
|
225
|
-
method: "GET",
|
|
226
|
-
path: `/gates/${id}`,
|
|
227
|
-
signal,
|
|
228
|
-
});
|
|
229
|
-
return new Gate(this.http, data);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
1
|
+
import { DeliverdError, DeliverdTimeoutError } from "./errors.js";
|
|
2
|
+
import type { HttpClient } from "./http.js";
|
|
3
|
+
import type { CreateGateInput, GateData, GateDecision } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Ask before you act.
|
|
7
|
+
*
|
|
8
|
+
* The other primitives in this package are an agent asking a person to look at
|
|
9
|
+
* work it has already done. This one asks first, and the answer comes from
|
|
10
|
+
* policy the developer does not own — which is the point: what needs a human is
|
|
11
|
+
* an organisation's decision, not a line in the agent's code.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const decision = await deliverd.gate({
|
|
15
|
+
* action: "finance.refund",
|
|
16
|
+
* externalId: `refund:${order.id}`,
|
|
17
|
+
* input: { customer_id: order.customerId, amount: 1240, currency: "GBP" },
|
|
18
|
+
* });
|
|
19
|
+
* if (!decision.allowed) return;
|
|
20
|
+
* await stripe.refunds.create(decision.input);
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* **Use `decision.input`, not your own object.** It is what the action's schema
|
|
24
|
+
* validated and what an approver actually saw — and once decision tokens land
|
|
25
|
+
* it is what execution will be checked against. Passing the original is right
|
|
26
|
+
* until the day somebody edits a value before approving, and then it is a
|
|
27
|
+
* silent bug that spends an approval for £1200 on a refund of £1240.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** How long between polls while waiting on a person. */
|
|
31
|
+
const POLL_SCHEDULE = [250, 750, 2_000, 4_000, 8_000, 15_000, 30_000] as const;
|
|
32
|
+
|
|
33
|
+
export interface GateWaitOptions {
|
|
34
|
+
/** Give up after this many milliseconds. Without one, waits indefinitely. */
|
|
35
|
+
timeout?: number;
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
/** Called on every poll, for a progress line. */
|
|
38
|
+
onPoll?: (gate: GateData) => void;
|
|
39
|
+
pollIntervals?: readonly number[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface GateOptions extends GateWaitOptions {
|
|
43
|
+
/** The registered action: `finance.refund`. */
|
|
44
|
+
action: string;
|
|
45
|
+
input: Record<string, unknown>;
|
|
46
|
+
externalId: string;
|
|
47
|
+
environment?: string;
|
|
48
|
+
reason?: string;
|
|
49
|
+
flowId?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Whether to block when policy sends it to a person. Default true.
|
|
52
|
+
*
|
|
53
|
+
* False returns the pending decision immediately, which is what you want
|
|
54
|
+
* inside a request handler that must not hold a connection open for an hour.
|
|
55
|
+
* `deliverd.gates.create()` is the same thing said more plainly.
|
|
56
|
+
*/
|
|
57
|
+
wait?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A decision, and what you can do with it.
|
|
62
|
+
*
|
|
63
|
+
* The data is a snapshot; `wait()` and `refresh()` replace it in place, so a
|
|
64
|
+
* handle held across a wait reads the settled decision rather than the pending
|
|
65
|
+
* one you created.
|
|
66
|
+
*/
|
|
67
|
+
export class Gate {
|
|
68
|
+
constructor(
|
|
69
|
+
private readonly http: HttpClient,
|
|
70
|
+
private data: GateData
|
|
71
|
+
) {}
|
|
72
|
+
|
|
73
|
+
get id(): string { return this.data.id; }
|
|
74
|
+
get status(): GateData["status"] { return this.data.status; }
|
|
75
|
+
/**
|
|
76
|
+
* The one to branch on. True when a rule permitted it **or** a person agreed
|
|
77
|
+
* to it; false for denied, rejected, expired, cancelled and while pending.
|
|
78
|
+
* An expiry is not permission — nobody said yes.
|
|
79
|
+
*/
|
|
80
|
+
get allowed(): boolean { return this.data.allowed; }
|
|
81
|
+
/** Still waiting on a person. */
|
|
82
|
+
get pending(): boolean { return this.data.status === "pending"; }
|
|
83
|
+
/** Refused by a rule, as against rejected by a person. */
|
|
84
|
+
get denied(): boolean { return this.data.status === "denied"; }
|
|
85
|
+
/** What to execute with: the input as the action's schema validated it. */
|
|
86
|
+
get input(): Record<string, unknown> { return this.data.input; }
|
|
87
|
+
/** Why, in the words of whoever wrote the rule. */
|
|
88
|
+
get reason(): string | null { return this.data.reason; }
|
|
89
|
+
/** The rule that decided, by name. Null when nothing matched it. */
|
|
90
|
+
get rule(): string | null { return this.data.rule; }
|
|
91
|
+
/** Where a person decides, when one was asked. */
|
|
92
|
+
get approvalUrl(): string | null { return this.data.approvalUrl; }
|
|
93
|
+
get approvalId(): string | null { return this.data.approvalId; }
|
|
94
|
+
/** True when this call read back a decision already made under your id. */
|
|
95
|
+
get reused(): boolean { return this.data.reused; }
|
|
96
|
+
|
|
97
|
+
/** Everything the API returned, if you want a field the getters do not name. */
|
|
98
|
+
toJSON(): GateData { return this.data; }
|
|
99
|
+
|
|
100
|
+
/** The answer, flattened — what `gate()` hands back. */
|
|
101
|
+
toDecision(): GateDecision {
|
|
102
|
+
return {
|
|
103
|
+
allowed: this.data.allowed,
|
|
104
|
+
status: this.data.status,
|
|
105
|
+
reason: this.data.reason,
|
|
106
|
+
rule: this.data.rule,
|
|
107
|
+
input: this.data.input,
|
|
108
|
+
changes: this.data.changes ?? [],
|
|
109
|
+
id: this.data.id,
|
|
110
|
+
approvalUrl: this.data.approvalUrl,
|
|
111
|
+
gate: this,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async refresh(signal?: AbortSignal): Promise<this> {
|
|
116
|
+
this.data = await this.http.request<GateData>({
|
|
117
|
+
method: "GET",
|
|
118
|
+
path: `/gates/${this.data.id}`,
|
|
119
|
+
signal,
|
|
120
|
+
});
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Block until a person decides, then return.
|
|
126
|
+
*
|
|
127
|
+
* Resolves on every settled state — it does not throw on a refusal, because
|
|
128
|
+
* a no is an answer and `if (gate.allowed)` is the line that should follow.
|
|
129
|
+
* It throws only when the wait itself failed: a timeout, an abort, or the
|
|
130
|
+
* API being unreachable.
|
|
131
|
+
*
|
|
132
|
+
* Returns immediately when policy already settled it, which is the common
|
|
133
|
+
* case: most gated calls are allowed or denied by a rule and never reach a
|
|
134
|
+
* person at all.
|
|
135
|
+
*/
|
|
136
|
+
async wait(options: GateWaitOptions = {}): Promise<this> {
|
|
137
|
+
const deadline = options.timeout !== undefined ? Date.now() + options.timeout : undefined;
|
|
138
|
+
let poll = 0;
|
|
139
|
+
|
|
140
|
+
if (!this.pending) return this;
|
|
141
|
+
|
|
142
|
+
for (;;) {
|
|
143
|
+
if (options.signal?.aborted) {
|
|
144
|
+
throw new DeliverdError(0, "aborted", "The wait was aborted.", undefined, {
|
|
145
|
+
cause: options.signal.reason,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
if (deadline !== undefined && Date.now() >= deadline) {
|
|
149
|
+
throw new DeliverdTimeoutError(
|
|
150
|
+
`Nobody decided the ${this.data.rule ? `"${this.data.rule}" ` : ""}gate on ` +
|
|
151
|
+
`"${this.data.input.action ?? this.id}" within the timeout. ` +
|
|
152
|
+
(this.data.approvalUrl
|
|
153
|
+
? `It is still open at ${this.data.approvalUrl}.`
|
|
154
|
+
: "It is still open.")
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const delay = pollDelay(poll++, options.pollIntervals ?? POLL_SCHEDULE);
|
|
159
|
+
const remaining = deadline === undefined ? delay : Math.min(delay, deadline - Date.now());
|
|
160
|
+
await sleep(Math.max(0, remaining), options.signal);
|
|
161
|
+
|
|
162
|
+
await this.refresh(options.signal);
|
|
163
|
+
options.onPoll?.(this.data);
|
|
164
|
+
|
|
165
|
+
if (!this.pending) return this;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** ±25% jitter, so a fleet of agents does not poll in lockstep. */
|
|
171
|
+
function pollDelay(attempt: number, schedule: readonly number[]): number {
|
|
172
|
+
const base = schedule[Math.min(attempt, schedule.length - 1)] ?? 30_000;
|
|
173
|
+
return Math.round(base * (0.75 + Math.random() * 0.5));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
177
|
+
return new Promise((resolve, reject) => {
|
|
178
|
+
if (signal?.aborted) {
|
|
179
|
+
return reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
180
|
+
}
|
|
181
|
+
const timer = setTimeout(() => {
|
|
182
|
+
signal?.removeEventListener("abort", onAbort);
|
|
183
|
+
resolve();
|
|
184
|
+
}, ms);
|
|
185
|
+
const onAbort = () => {
|
|
186
|
+
clearTimeout(timer);
|
|
187
|
+
reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
188
|
+
};
|
|
189
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export class Gates {
|
|
194
|
+
constructor(private readonly http: HttpClient) {}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Raise a decision and return straight away, even if it went to a person.
|
|
198
|
+
*
|
|
199
|
+
* The non-blocking half of `gate()`. Use it when the work resumes on a
|
|
200
|
+
* webhook or a later poll rather than inside this call.
|
|
201
|
+
*/
|
|
202
|
+
async create(input: CreateGateInput, signal?: AbortSignal): Promise<Gate> {
|
|
203
|
+
const data = await this.http.request<GateData>({
|
|
204
|
+
method: "POST",
|
|
205
|
+
path: "/gates",
|
|
206
|
+
body: input,
|
|
207
|
+
/*
|
|
208
|
+
* The caller's own id, as the HTTP idempotency key too.
|
|
209
|
+
*
|
|
210
|
+
* The server enforces reuse against `gate_requests.external_id`, so this
|
|
211
|
+
* is belt and braces rather than the mechanism — but it means a retry
|
|
212
|
+
* inside the transport layer and a retry by the caller's own code are the
|
|
213
|
+
* same key, instead of one guarding the request and the other the
|
|
214
|
+
* decision.
|
|
215
|
+
*/
|
|
216
|
+
idempotencyKey: input.externalId,
|
|
217
|
+
signal,
|
|
218
|
+
});
|
|
219
|
+
return new Gate(this.http, data);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** One decision by its id. */
|
|
223
|
+
async get(id: string, signal?: AbortSignal): Promise<Gate> {
|
|
224
|
+
const data = await this.http.request<GateData>({
|
|
225
|
+
method: "GET",
|
|
226
|
+
path: `/gates/${id}`,
|
|
227
|
+
signal,
|
|
228
|
+
});
|
|
229
|
+
return new Gate(this.http, data);
|
|
230
|
+
}
|
|
231
|
+
}
|