@deliverd/sdk 0.11.1 → 0.12.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 +693 -654
- package/dist/dev.d.ts.map +1 -1
- package/dist/dev.js +398 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +13 -0
- package/dist/released.d.ts +1 -1
- package/dist/released.d.ts.map +1 -1
- package/dist/released.js +729 -0
- package/dist/tasks.d.ts +95 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +254 -0
- package/dist/types.d.ts +341 -1
- 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 +1274 -868
- package/src/duration.ts +21 -21
- package/src/errors.ts +62 -62
- package/src/flows.ts +222 -222
- package/src/gates.ts +366 -366
- package/src/http.ts +217 -217
- package/src/index.ts +448 -444
- package/src/messages.ts +222 -209
- package/src/paging.ts +64 -64
- package/src/released.ts +4778 -4049
- package/src/reports.ts +490 -490
- package/src/reviews.ts +236 -236
- package/src/tasks.ts +312 -0
- package/src/types.ts +1504 -1107
- package/src/webhooks.ts +117 -117
package/src/reviews.ts
CHANGED
|
@@ -1,236 +1,236 @@
|
|
|
1
|
-
import { listOrCollect, type PageOf } from "./paging.js";
|
|
2
|
-
import { DeliverdError, DeliverdTimeoutError } from "./errors.js";
|
|
3
|
-
import type { HttpClient } from "./http.js";
|
|
4
|
-
import { POLL_SCHEDULE } from "./approvals.js";
|
|
5
|
-
import type {
|
|
6
|
-
CreateReviewInput,
|
|
7
|
-
ListReviewsInput,
|
|
8
|
-
ReviewData,
|
|
9
|
-
ReviewOutcome,
|
|
10
|
-
} from "./types.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Ask people to read what you made and say whether it is right.
|
|
14
|
-
*
|
|
15
|
-
* The sibling of approvals, and the difference is worth keeping straight: an
|
|
16
|
-
* approval asks permission for something that has not happened, a review asks
|
|
17
|
-
* judgement on something that has. The waiting is the same shape, so the
|
|
18
|
-
* polling is shared.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
export interface ReviewWaitOptions {
|
|
22
|
-
/** Called after every poll, for a progress line in a CLI. */
|
|
23
|
-
onPoll?: (review: ReviewData) => void;
|
|
24
|
-
/**
|
|
25
|
-
* Give up after this many milliseconds and throw `DeliverdTimeoutError`.
|
|
26
|
-
* Omitted, the wait ends when the review does — which, for a review with no
|
|
27
|
-
* `expiresAt`, may be never. Set one or the other.
|
|
28
|
-
*/
|
|
29
|
-
timeout?: number;
|
|
30
|
-
signal?: AbortSignal;
|
|
31
|
-
/** How long between polls, as the wait lengthens. Rarely worth changing. */
|
|
32
|
-
pollIntervals?: readonly number[];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function pollDelay(poll: number, schedule: readonly number[]): number {
|
|
36
|
-
const base = schedule[Math.min(poll, schedule.length - 1)] ?? 60_000;
|
|
37
|
-
// Jitter, so a hundred agents started by the same cron do not arrive together.
|
|
38
|
-
return base * 0.75 + Math.random() * base * 0.5;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** A review, and what you can do with it. */
|
|
42
|
-
export class Review {
|
|
43
|
-
constructor(
|
|
44
|
-
private readonly http: HttpClient,
|
|
45
|
-
private data: ReviewData
|
|
46
|
-
) {}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* The secret this request's callbacks are signed with, when you gave a
|
|
50
|
-
* `callbackUrl`. Only on the handle `create()` returned — the API hands it
|
|
51
|
-
* over once and never again — so keep it with whatever will receive the
|
|
52
|
-
* callback, and check each one with
|
|
53
|
-
* `deliverd.webhooks.constructCallback(callbackSecret, rawBody, signature)`.
|
|
54
|
-
*/
|
|
55
|
-
callbackSecret: string | null = null;
|
|
56
|
-
|
|
57
|
-
get id(): string { return this.data.id; }
|
|
58
|
-
get status(): ReviewData["status"] { return this.data.status; }
|
|
59
|
-
/** The page the reviewer reads and decides on. */
|
|
60
|
-
get url(): string { return this.data.url; }
|
|
61
|
-
get title(): string { return this.data.title; }
|
|
62
|
-
get verdicts(): ReviewData["verdicts"] { return this.data.verdicts; }
|
|
63
|
-
/** How many comment threads the reviewers opened while reading. */
|
|
64
|
-
get threadCount(): number { return this.data.threadCount; }
|
|
65
|
-
|
|
66
|
-
/** The one to branch on. False for changes requested, expired and cancelled. */
|
|
67
|
-
get approved(): boolean { return this.data.status === "approved"; }
|
|
68
|
-
get changesRequested(): boolean { return this.data.status === "changes_requested"; }
|
|
69
|
-
get expired(): boolean { return this.data.status === "expired"; }
|
|
70
|
-
get cancelled(): boolean { return this.data.status === "cancelled"; }
|
|
71
|
-
get pending(): boolean { return this.data.status === "pending"; }
|
|
72
|
-
|
|
73
|
-
/** Everything the API returned, if you want a field the getters do not name. */
|
|
74
|
-
toJSON(): ReviewData { return this.data; }
|
|
75
|
-
|
|
76
|
-
toOutcome(): ReviewOutcome {
|
|
77
|
-
const status = this.data.status === "pending" ? "expired" : this.data.status;
|
|
78
|
-
return {
|
|
79
|
-
approved: this.data.status === "approved",
|
|
80
|
-
status,
|
|
81
|
-
verdicts: this.data.verdicts,
|
|
82
|
-
threadCount: this.data.threadCount,
|
|
83
|
-
id: this.data.id,
|
|
84
|
-
url: this.data.url,
|
|
85
|
-
review: this.data,
|
|
86
|
-
} as ReviewOutcome;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async refresh(signal?: AbortSignal): Promise<this> {
|
|
90
|
-
this.data = await this.http.request<ReviewData>({
|
|
91
|
-
method: "GET",
|
|
92
|
-
path: `/reviews/${this.data.id}`,
|
|
93
|
-
signal,
|
|
94
|
-
});
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Withdraw it. The row stays as evidence of what was asked and dropped. */
|
|
99
|
-
async cancel(signal?: AbortSignal): Promise<this> {
|
|
100
|
-
this.data = await this.http.request<ReviewData>({
|
|
101
|
-
method: "POST",
|
|
102
|
-
path: `/reviews/${this.data.id}/cancel`,
|
|
103
|
-
signal,
|
|
104
|
-
});
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Block until the reviewers settle it, then return.
|
|
110
|
-
*
|
|
111
|
-
* Resolves on every ending, including changes requested — that is an answer
|
|
112
|
-
* and the most useful one, because it comes with the threads that say what
|
|
113
|
-
* to change. It throws only when the wait itself failed: a timeout, an
|
|
114
|
-
* abort, or an API that could not be reached.
|
|
115
|
-
*/
|
|
116
|
-
async wait(options: ReviewWaitOptions = {}): Promise<this> {
|
|
117
|
-
const deadline = options.timeout !== undefined ? Date.now() + options.timeout : undefined;
|
|
118
|
-
let poll = 0;
|
|
119
|
-
if (!this.pending) return this;
|
|
120
|
-
|
|
121
|
-
for (;;) {
|
|
122
|
-
if (options.signal?.aborted) {
|
|
123
|
-
throw new DeliverdError(0, "aborted", "The wait was aborted.", undefined, {
|
|
124
|
-
cause: options.signal.reason,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
if (deadline !== undefined && Date.now() >= deadline) {
|
|
128
|
-
throw new DeliverdTimeoutError(
|
|
129
|
-
`Nobody finished reviewing "${this.data.title}" within the timeout. It is still open at ${this.data.url}.`
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
const delay = pollDelay(poll++, options.pollIntervals ?? POLL_SCHEDULE);
|
|
133
|
-
const remaining = deadline === undefined ? delay : Math.min(delay, deadline - Date.now());
|
|
134
|
-
await sleep(Math.max(0, remaining), options.signal);
|
|
135
|
-
|
|
136
|
-
await this.refresh(options.signal);
|
|
137
|
-
options.onPoll?.(this.data);
|
|
138
|
-
if (!this.pending) return this;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
144
|
-
return new Promise((resolve, reject) => {
|
|
145
|
-
if (signal?.aborted) {
|
|
146
|
-
return reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
147
|
-
}
|
|
148
|
-
const timer = setTimeout(() => {
|
|
149
|
-
signal?.removeEventListener("abort", onAbort);
|
|
150
|
-
resolve();
|
|
151
|
-
}, ms);
|
|
152
|
-
const onAbort = () => {
|
|
153
|
-
clearTimeout(timer);
|
|
154
|
-
reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
155
|
-
};
|
|
156
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export class Reviews {
|
|
161
|
-
constructor(private readonly http: HttpClient) {}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Ask. Returns as soon as the request is filed — the reviewers are being
|
|
165
|
-
* told while this resolves — so call `wait()` next, or keep the id and poll
|
|
166
|
-
* later.
|
|
167
|
-
*/
|
|
168
|
-
async create(input: CreateReviewInput, signal?: AbortSignal): Promise<Review> {
|
|
169
|
-
const data = await this.http.request<ReviewData>({
|
|
170
|
-
method: "POST",
|
|
171
|
-
path: "/reviews",
|
|
172
|
-
body: input,
|
|
173
|
-
signal,
|
|
174
|
-
});
|
|
175
|
-
const created = new Review(this.http, data);
|
|
176
|
-
created.callbackSecret = (data as ReviewData & { callbackSecret?: string }).callbackSecret ?? null;
|
|
177
|
-
return created;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async get(id: string, signal?: AbortSignal): Promise<Review> {
|
|
181
|
-
const data = await this.http.request<ReviewData>({
|
|
182
|
-
method: "GET",
|
|
183
|
-
path: `/reviews/${id}`,
|
|
184
|
-
signal,
|
|
185
|
-
});
|
|
186
|
-
return new Review(this.http, data);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* The organisation's reviews, newest first.
|
|
191
|
-
*
|
|
192
|
-
* With no `limit` this is every one of them: the API says where each page
|
|
193
|
-
* stopped and this follows the cursor to the end. With a `limit` it is one
|
|
194
|
-
* page of that size, and `listPage` hands back the cursor to continue from.
|
|
195
|
-
*/
|
|
196
|
-
async list(input: ListReviewsInput = {}, signal?: AbortSignal): Promise<Review[]> {
|
|
197
|
-
return listOrCollect(input, async (q: { limit?: number; cursor?: string }) => {
|
|
198
|
-
const page = await this.listPage({ ...input, ...q }, signal);
|
|
199
|
-
return { items: page.items, nextCursor: page.nextCursor };
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** One page, and the cursor to continue from — null when it is the end. */
|
|
204
|
-
async listPage(
|
|
205
|
-
input: ListReviewsInput = {},
|
|
206
|
-
signal?: AbortSignal
|
|
207
|
-
): Promise<PageOf<Review>> {
|
|
208
|
-
const { reviews, nextCursor } = await this.http.request<{
|
|
209
|
-
reviews: ReviewData[];
|
|
210
|
-
nextCursor: string | null;
|
|
211
|
-
}>({
|
|
212
|
-
method: "GET",
|
|
213
|
-
path: "/reviews",
|
|
214
|
-
query: { status: input.status, externalId: input.externalId, reportId: input.reportId, limit: input.limit, cursor: input.cursor },
|
|
215
|
-
signal,
|
|
216
|
-
});
|
|
217
|
-
return { items: reviews.map((r) => new Review(this.http, r)), nextCursor };
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/** Ask and wait, for the common case where there is nothing to do between. */
|
|
221
|
-
async request(input: CreateReviewInput, options: ReviewWaitOptions = {}): Promise<Review> {
|
|
222
|
-
const review = await this.create(input, options.signal);
|
|
223
|
-
return review.wait(options);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* The same request as before, when there was one — so restarting a crashed
|
|
228
|
-
* agent does not ask the same people twice. The caveat is the one
|
|
229
|
-
* `approvals.findByExternalId` carries: two concurrent calls can still both
|
|
230
|
-
* find nothing and both create.
|
|
231
|
-
*/
|
|
232
|
-
async findByExternalId(externalId: string, signal?: AbortSignal): Promise<Review | null> {
|
|
233
|
-
const found = await this.list({ externalId, limit: 1 }, signal);
|
|
234
|
-
return found[0] ?? null;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
1
|
+
import { listOrCollect, type PageOf } from "./paging.js";
|
|
2
|
+
import { DeliverdError, DeliverdTimeoutError } from "./errors.js";
|
|
3
|
+
import type { HttpClient } from "./http.js";
|
|
4
|
+
import { POLL_SCHEDULE } from "./approvals.js";
|
|
5
|
+
import type {
|
|
6
|
+
CreateReviewInput,
|
|
7
|
+
ListReviewsInput,
|
|
8
|
+
ReviewData,
|
|
9
|
+
ReviewOutcome,
|
|
10
|
+
} from "./types.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Ask people to read what you made and say whether it is right.
|
|
14
|
+
*
|
|
15
|
+
* The sibling of approvals, and the difference is worth keeping straight: an
|
|
16
|
+
* approval asks permission for something that has not happened, a review asks
|
|
17
|
+
* judgement on something that has. The waiting is the same shape, so the
|
|
18
|
+
* polling is shared.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export interface ReviewWaitOptions {
|
|
22
|
+
/** Called after every poll, for a progress line in a CLI. */
|
|
23
|
+
onPoll?: (review: ReviewData) => void;
|
|
24
|
+
/**
|
|
25
|
+
* Give up after this many milliseconds and throw `DeliverdTimeoutError`.
|
|
26
|
+
* Omitted, the wait ends when the review does — which, for a review with no
|
|
27
|
+
* `expiresAt`, may be never. Set one or the other.
|
|
28
|
+
*/
|
|
29
|
+
timeout?: number;
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
/** How long between polls, as the wait lengthens. Rarely worth changing. */
|
|
32
|
+
pollIntervals?: readonly number[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function pollDelay(poll: number, schedule: readonly number[]): number {
|
|
36
|
+
const base = schedule[Math.min(poll, schedule.length - 1)] ?? 60_000;
|
|
37
|
+
// Jitter, so a hundred agents started by the same cron do not arrive together.
|
|
38
|
+
return base * 0.75 + Math.random() * base * 0.5;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A review, and what you can do with it. */
|
|
42
|
+
export class Review {
|
|
43
|
+
constructor(
|
|
44
|
+
private readonly http: HttpClient,
|
|
45
|
+
private data: ReviewData
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The secret this request's callbacks are signed with, when you gave a
|
|
50
|
+
* `callbackUrl`. Only on the handle `create()` returned — the API hands it
|
|
51
|
+
* over once and never again — so keep it with whatever will receive the
|
|
52
|
+
* callback, and check each one with
|
|
53
|
+
* `deliverd.webhooks.constructCallback(callbackSecret, rawBody, signature)`.
|
|
54
|
+
*/
|
|
55
|
+
callbackSecret: string | null = null;
|
|
56
|
+
|
|
57
|
+
get id(): string { return this.data.id; }
|
|
58
|
+
get status(): ReviewData["status"] { return this.data.status; }
|
|
59
|
+
/** The page the reviewer reads and decides on. */
|
|
60
|
+
get url(): string { return this.data.url; }
|
|
61
|
+
get title(): string { return this.data.title; }
|
|
62
|
+
get verdicts(): ReviewData["verdicts"] { return this.data.verdicts; }
|
|
63
|
+
/** How many comment threads the reviewers opened while reading. */
|
|
64
|
+
get threadCount(): number { return this.data.threadCount; }
|
|
65
|
+
|
|
66
|
+
/** The one to branch on. False for changes requested, expired and cancelled. */
|
|
67
|
+
get approved(): boolean { return this.data.status === "approved"; }
|
|
68
|
+
get changesRequested(): boolean { return this.data.status === "changes_requested"; }
|
|
69
|
+
get expired(): boolean { return this.data.status === "expired"; }
|
|
70
|
+
get cancelled(): boolean { return this.data.status === "cancelled"; }
|
|
71
|
+
get pending(): boolean { return this.data.status === "pending"; }
|
|
72
|
+
|
|
73
|
+
/** Everything the API returned, if you want a field the getters do not name. */
|
|
74
|
+
toJSON(): ReviewData { return this.data; }
|
|
75
|
+
|
|
76
|
+
toOutcome(): ReviewOutcome {
|
|
77
|
+
const status = this.data.status === "pending" ? "expired" : this.data.status;
|
|
78
|
+
return {
|
|
79
|
+
approved: this.data.status === "approved",
|
|
80
|
+
status,
|
|
81
|
+
verdicts: this.data.verdicts,
|
|
82
|
+
threadCount: this.data.threadCount,
|
|
83
|
+
id: this.data.id,
|
|
84
|
+
url: this.data.url,
|
|
85
|
+
review: this.data,
|
|
86
|
+
} as ReviewOutcome;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async refresh(signal?: AbortSignal): Promise<this> {
|
|
90
|
+
this.data = await this.http.request<ReviewData>({
|
|
91
|
+
method: "GET",
|
|
92
|
+
path: `/reviews/${this.data.id}`,
|
|
93
|
+
signal,
|
|
94
|
+
});
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Withdraw it. The row stays as evidence of what was asked and dropped. */
|
|
99
|
+
async cancel(signal?: AbortSignal): Promise<this> {
|
|
100
|
+
this.data = await this.http.request<ReviewData>({
|
|
101
|
+
method: "POST",
|
|
102
|
+
path: `/reviews/${this.data.id}/cancel`,
|
|
103
|
+
signal,
|
|
104
|
+
});
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Block until the reviewers settle it, then return.
|
|
110
|
+
*
|
|
111
|
+
* Resolves on every ending, including changes requested — that is an answer
|
|
112
|
+
* and the most useful one, because it comes with the threads that say what
|
|
113
|
+
* to change. It throws only when the wait itself failed: a timeout, an
|
|
114
|
+
* abort, or an API that could not be reached.
|
|
115
|
+
*/
|
|
116
|
+
async wait(options: ReviewWaitOptions = {}): Promise<this> {
|
|
117
|
+
const deadline = options.timeout !== undefined ? Date.now() + options.timeout : undefined;
|
|
118
|
+
let poll = 0;
|
|
119
|
+
if (!this.pending) return this;
|
|
120
|
+
|
|
121
|
+
for (;;) {
|
|
122
|
+
if (options.signal?.aborted) {
|
|
123
|
+
throw new DeliverdError(0, "aborted", "The wait was aborted.", undefined, {
|
|
124
|
+
cause: options.signal.reason,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (deadline !== undefined && Date.now() >= deadline) {
|
|
128
|
+
throw new DeliverdTimeoutError(
|
|
129
|
+
`Nobody finished reviewing "${this.data.title}" within the timeout. It is still open at ${this.data.url}.`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
const delay = pollDelay(poll++, options.pollIntervals ?? POLL_SCHEDULE);
|
|
133
|
+
const remaining = deadline === undefined ? delay : Math.min(delay, deadline - Date.now());
|
|
134
|
+
await sleep(Math.max(0, remaining), options.signal);
|
|
135
|
+
|
|
136
|
+
await this.refresh(options.signal);
|
|
137
|
+
options.onPoll?.(this.data);
|
|
138
|
+
if (!this.pending) return this;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
if (signal?.aborted) {
|
|
146
|
+
return reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
147
|
+
}
|
|
148
|
+
const timer = setTimeout(() => {
|
|
149
|
+
signal?.removeEventListener("abort", onAbort);
|
|
150
|
+
resolve();
|
|
151
|
+
}, ms);
|
|
152
|
+
const onAbort = () => {
|
|
153
|
+
clearTimeout(timer);
|
|
154
|
+
reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
155
|
+
};
|
|
156
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export class Reviews {
|
|
161
|
+
constructor(private readonly http: HttpClient) {}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Ask. Returns as soon as the request is filed — the reviewers are being
|
|
165
|
+
* told while this resolves — so call `wait()` next, or keep the id and poll
|
|
166
|
+
* later.
|
|
167
|
+
*/
|
|
168
|
+
async create(input: CreateReviewInput, signal?: AbortSignal): Promise<Review> {
|
|
169
|
+
const data = await this.http.request<ReviewData>({
|
|
170
|
+
method: "POST",
|
|
171
|
+
path: "/reviews",
|
|
172
|
+
body: input,
|
|
173
|
+
signal,
|
|
174
|
+
});
|
|
175
|
+
const created = new Review(this.http, data);
|
|
176
|
+
created.callbackSecret = (data as ReviewData & { callbackSecret?: string }).callbackSecret ?? null;
|
|
177
|
+
return created;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async get(id: string, signal?: AbortSignal): Promise<Review> {
|
|
181
|
+
const data = await this.http.request<ReviewData>({
|
|
182
|
+
method: "GET",
|
|
183
|
+
path: `/reviews/${id}`,
|
|
184
|
+
signal,
|
|
185
|
+
});
|
|
186
|
+
return new Review(this.http, data);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The organisation's reviews, newest first.
|
|
191
|
+
*
|
|
192
|
+
* With no `limit` this is every one of them: the API says where each page
|
|
193
|
+
* stopped and this follows the cursor to the end. With a `limit` it is one
|
|
194
|
+
* page of that size, and `listPage` hands back the cursor to continue from.
|
|
195
|
+
*/
|
|
196
|
+
async list(input: ListReviewsInput = {}, signal?: AbortSignal): Promise<Review[]> {
|
|
197
|
+
return listOrCollect(input, async (q: { limit?: number; cursor?: string }) => {
|
|
198
|
+
const page = await this.listPage({ ...input, ...q }, signal);
|
|
199
|
+
return { items: page.items, nextCursor: page.nextCursor };
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** One page, and the cursor to continue from — null when it is the end. */
|
|
204
|
+
async listPage(
|
|
205
|
+
input: ListReviewsInput = {},
|
|
206
|
+
signal?: AbortSignal
|
|
207
|
+
): Promise<PageOf<Review>> {
|
|
208
|
+
const { reviews, nextCursor } = await this.http.request<{
|
|
209
|
+
reviews: ReviewData[];
|
|
210
|
+
nextCursor: string | null;
|
|
211
|
+
}>({
|
|
212
|
+
method: "GET",
|
|
213
|
+
path: "/reviews",
|
|
214
|
+
query: { status: input.status, externalId: input.externalId, reportId: input.reportId, limit: input.limit, cursor: input.cursor },
|
|
215
|
+
signal,
|
|
216
|
+
});
|
|
217
|
+
return { items: reviews.map((r) => new Review(this.http, r)), nextCursor };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Ask and wait, for the common case where there is nothing to do between. */
|
|
221
|
+
async request(input: CreateReviewInput, options: ReviewWaitOptions = {}): Promise<Review> {
|
|
222
|
+
const review = await this.create(input, options.signal);
|
|
223
|
+
return review.wait(options);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The same request as before, when there was one — so restarting a crashed
|
|
228
|
+
* agent does not ask the same people twice. The caveat is the one
|
|
229
|
+
* `approvals.findByExternalId` carries: two concurrent calls can still both
|
|
230
|
+
* find nothing and both create.
|
|
231
|
+
*/
|
|
232
|
+
async findByExternalId(externalId: string, signal?: AbortSignal): Promise<Review | null> {
|
|
233
|
+
const found = await this.list({ externalId, limit: 1 }, signal);
|
|
234
|
+
return found[0] ?? null;
|
|
235
|
+
}
|
|
236
|
+
}
|