@deliverd/sdk 0.1.0 → 0.3.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/README.md +106 -10
- package/dist/collections.d.ts +78 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +170 -0
- package/dist/dev.d.ts +1 -1
- package/dist/dev.d.ts.map +1 -1
- package/dist/dev.js +238 -2
- package/dist/index.d.ts +86 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +107 -0
- package/dist/reviews.d.ts +80 -0
- package/dist/reviews.d.ts.map +1 -0
- package/dist/reviews.js +165 -0
- package/dist/types.d.ts +182 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/collections.ts +214 -0
- package/src/dev.ts +275 -3
- package/src/index.ts +161 -0
- package/src/reviews.ts +208 -0
- package/src/types.ts +216 -1
package/dist/types.d.ts
CHANGED
|
@@ -168,7 +168,7 @@ export interface CommentThread {
|
|
|
168
168
|
}[];
|
|
169
169
|
[key: string]: unknown;
|
|
170
170
|
}
|
|
171
|
-
export type WebhookEventName = "report.published" | "report.updated" | "report.shared" | "report.viewed" | "comment.created" | "access.requested" | "schedule.due" | "publish.held" | "approval.requested" | "approval.approved" | "approval.rejected" | "approval.expired" | "approval.question" | "approval.answered";
|
|
171
|
+
export type WebhookEventName = "report.published" | "report.updated" | "report.shared" | "report.viewed" | "comment.created" | "access.requested" | "schedule.due" | "publish.held" | "approval.requested" | "approval.approved" | "approval.rejected" | "approval.expired" | "approval.question" | "approval.answered" | "review.requested" | "review.approved" | "review.changes_requested" | "collection.requested" | "collection.completed";
|
|
172
172
|
export interface WebhookEvent {
|
|
173
173
|
event: WebhookEventName;
|
|
174
174
|
sentAt: string;
|
|
@@ -176,4 +176,185 @@ export interface WebhookEvent {
|
|
|
176
176
|
test?: boolean;
|
|
177
177
|
data: Record<string, unknown>;
|
|
178
178
|
}
|
|
179
|
+
export type ReviewStatus = "pending" | "approved" | "changes_requested" | "expired" | "cancelled";
|
|
180
|
+
export interface CreateReviewInput {
|
|
181
|
+
/** What is being reviewed, as a headline the reviewer sees first. */
|
|
182
|
+
title: string;
|
|
183
|
+
/**
|
|
184
|
+
* What to check. Without this a reviewer decides what "review" means, and
|
|
185
|
+
* usually decides it means "looks fine".
|
|
186
|
+
*/
|
|
187
|
+
instructions?: string;
|
|
188
|
+
/** The published report under review; the reviewer reads it and comments on it. */
|
|
189
|
+
reportId?: string;
|
|
190
|
+
/** Pin a version, so publishing again mid-review does not move the ground. */
|
|
191
|
+
versionId?: string;
|
|
192
|
+
/**
|
|
193
|
+
* Emails or user ids. An active member, or a guest who can already open the
|
|
194
|
+
* report — anyone else is refused rather than emailed. You cannot name
|
|
195
|
+
* yourself.
|
|
196
|
+
*/
|
|
197
|
+
reviewers: string[];
|
|
198
|
+
/**
|
|
199
|
+
* How many approvals settle it; one when omitted. A single "changes
|
|
200
|
+
* requested" settles it regardless.
|
|
201
|
+
*/
|
|
202
|
+
requiredApprovals?: number;
|
|
203
|
+
/** After this the review expires unanswered. ISO 8601. */
|
|
204
|
+
expiresAt?: string;
|
|
205
|
+
/** Your own reference, returned unchanged and searchable. */
|
|
206
|
+
externalId?: string;
|
|
207
|
+
metadata?: Record<string, unknown>;
|
|
208
|
+
}
|
|
209
|
+
export interface ReviewerRef {
|
|
210
|
+
kind: "user" | "guest";
|
|
211
|
+
id: string;
|
|
212
|
+
label: string;
|
|
213
|
+
}
|
|
214
|
+
export interface ReviewVerdict {
|
|
215
|
+
verdict: "approved" | "changes_requested";
|
|
216
|
+
/** Who gave it, as a name rather than an id. */
|
|
217
|
+
by: string;
|
|
218
|
+
note: string | null;
|
|
219
|
+
at: string;
|
|
220
|
+
}
|
|
221
|
+
export interface ReviewData {
|
|
222
|
+
id: string;
|
|
223
|
+
status: ReviewStatus;
|
|
224
|
+
/** The page the reviewer reads and decides on. */
|
|
225
|
+
url: string;
|
|
226
|
+
title: string;
|
|
227
|
+
instructions: string | null;
|
|
228
|
+
reportId: string | null;
|
|
229
|
+
versionId: string | null;
|
|
230
|
+
externalId: string | null;
|
|
231
|
+
metadata: Record<string, unknown>;
|
|
232
|
+
requestedBy: string;
|
|
233
|
+
requiredApprovals: number;
|
|
234
|
+
reviewers: ReviewerRef[];
|
|
235
|
+
verdicts: ReviewVerdict[];
|
|
236
|
+
/** How many comment threads the reviewers opened while reading. */
|
|
237
|
+
threadCount: number;
|
|
238
|
+
settledAt: string | null;
|
|
239
|
+
expiresAt: string | null;
|
|
240
|
+
createdAt: string;
|
|
241
|
+
}
|
|
242
|
+
/** The answer, flattened — what `review()` hands back. */
|
|
243
|
+
export interface ReviewOutcome {
|
|
244
|
+
/** The one to branch on. False for changes requested, expired and cancelled. */
|
|
245
|
+
approved: boolean;
|
|
246
|
+
status: Exclude<ReviewStatus, "pending">;
|
|
247
|
+
/** Every verdict returned, in the order they arrived. */
|
|
248
|
+
verdicts: ReviewVerdict[];
|
|
249
|
+
/** How many comment threads to read before producing the next version. */
|
|
250
|
+
threadCount: number;
|
|
251
|
+
id: string;
|
|
252
|
+
url: string;
|
|
253
|
+
review: ReviewData;
|
|
254
|
+
}
|
|
255
|
+
export interface ListReviewsInput {
|
|
256
|
+
status?: ReviewStatus;
|
|
257
|
+
externalId?: string;
|
|
258
|
+
reportId?: string;
|
|
259
|
+
limit?: number;
|
|
260
|
+
}
|
|
261
|
+
export type CollectionStatus = "pending" | "complete" | "expired" | "cancelled";
|
|
262
|
+
export type QuestionKind = "text" | "long_text" | "number" | "date" | "choice" | "multi_choice" | "boolean";
|
|
263
|
+
/** What an answer can hold, shaped by its question's kind. */
|
|
264
|
+
export type AnswerValue = string | number | boolean | string[];
|
|
265
|
+
export interface QuestionInput {
|
|
266
|
+
/** The question, as you would ask it out loud. */
|
|
267
|
+
prompt: string;
|
|
268
|
+
/** The sentence under the field that stops the wrong answer being given. */
|
|
269
|
+
help?: string;
|
|
270
|
+
kind: QuestionKind;
|
|
271
|
+
/** Required unless you say otherwise; a required question blocks submission. */
|
|
272
|
+
required?: boolean;
|
|
273
|
+
/** For `choice` and `multi_choice`. At least two, or it is not a question. */
|
|
274
|
+
options?: string[];
|
|
275
|
+
}
|
|
276
|
+
export interface CreateCollectionInput {
|
|
277
|
+
/** What you need, as a headline the person sees first. */
|
|
278
|
+
title: string;
|
|
279
|
+
/** Why you are asking, shown above the questions. */
|
|
280
|
+
instructions?: string;
|
|
281
|
+
questions: QuestionInput[];
|
|
282
|
+
/**
|
|
283
|
+
* Emails or user ids. An active member, or a guest the organisation already
|
|
284
|
+
* knows — anyone else is refused rather than emailed.
|
|
285
|
+
*/
|
|
286
|
+
respondents: string[];
|
|
287
|
+
/** How many submitted replies complete it; everyone asked, when omitted. */
|
|
288
|
+
requiredResponses?: number;
|
|
289
|
+
/** A published report the request is about. */
|
|
290
|
+
reportId?: string;
|
|
291
|
+
/** Shown to the person, and what the single reminder is measured against. */
|
|
292
|
+
dueAt?: string;
|
|
293
|
+
/** After this the request closes unanswered. ISO 8601. */
|
|
294
|
+
expiresAt?: string;
|
|
295
|
+
externalId?: string;
|
|
296
|
+
metadata?: Record<string, unknown>;
|
|
297
|
+
}
|
|
298
|
+
export interface CollectionQuestion {
|
|
299
|
+
id: string;
|
|
300
|
+
prompt: string;
|
|
301
|
+
help: string | null;
|
|
302
|
+
kind: QuestionKind;
|
|
303
|
+
required: boolean;
|
|
304
|
+
options: string[];
|
|
305
|
+
}
|
|
306
|
+
export interface CollectionReply {
|
|
307
|
+
/** Who answered, as a name rather than an id. */
|
|
308
|
+
respondent: string;
|
|
309
|
+
status: "pending" | "submitted";
|
|
310
|
+
submittedAt: string | null;
|
|
311
|
+
/** Empty until they submit: a half-typed draft is not what they meant to say. */
|
|
312
|
+
answers: {
|
|
313
|
+
question: string;
|
|
314
|
+
kind: QuestionKind;
|
|
315
|
+
value: AnswerValue;
|
|
316
|
+
}[];
|
|
317
|
+
}
|
|
318
|
+
export interface CollectionData {
|
|
319
|
+
id: string;
|
|
320
|
+
status: CollectionStatus;
|
|
321
|
+
/** The page the person fills in. */
|
|
322
|
+
url: string;
|
|
323
|
+
title: string;
|
|
324
|
+
instructions: string | null;
|
|
325
|
+
reportId: string | null;
|
|
326
|
+
externalId: string | null;
|
|
327
|
+
metadata: Record<string, unknown>;
|
|
328
|
+
requestedBy: string;
|
|
329
|
+
requiredResponses: number;
|
|
330
|
+
questions: CollectionQuestion[];
|
|
331
|
+
responses: CollectionReply[];
|
|
332
|
+
submittedCount: number;
|
|
333
|
+
askedCount: number;
|
|
334
|
+
settledAt: string | null;
|
|
335
|
+
dueAt: string | null;
|
|
336
|
+
expiresAt: string | null;
|
|
337
|
+
createdAt: string;
|
|
338
|
+
}
|
|
339
|
+
/** The answers, flattened — what `collect()` hands back. */
|
|
340
|
+
export interface CollectedAnswers {
|
|
341
|
+
/** True when enough people replied; false if it expired or was withdrawn. */
|
|
342
|
+
complete: boolean;
|
|
343
|
+
status: Exclude<CollectionStatus, "pending">;
|
|
344
|
+
/** Every submitted reply, in the order they arrived. */
|
|
345
|
+
responses: CollectionReply[];
|
|
346
|
+
/**
|
|
347
|
+
* The first submitted answer to each question, by its prompt — the shape
|
|
348
|
+
* most callers want when they asked one person for three facts.
|
|
349
|
+
*/
|
|
350
|
+
answers: Record<string, AnswerValue>;
|
|
351
|
+
id: string;
|
|
352
|
+
url: string;
|
|
353
|
+
collection: CollectionData;
|
|
354
|
+
}
|
|
355
|
+
export interface ListCollectionsInput {
|
|
356
|
+
status?: CollectionStatus;
|
|
357
|
+
externalId?: string;
|
|
358
|
+
limit?: number;
|
|
359
|
+
}
|
|
179
360
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,MAAM,cAAc,GAAG,SAAS,GAAG,UAAU,GAAG,UAAU,GAAG,SAAS,GAAG,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C;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { DeliverdError, DeliverdTimeoutError } from "./errors.js";
|
|
2
|
+
import type { HttpClient } from "./http.js";
|
|
3
|
+
import { POLL_SCHEDULE } from "./approvals.js";
|
|
4
|
+
import type {
|
|
5
|
+
AnswerValue,
|
|
6
|
+
CollectedAnswers,
|
|
7
|
+
CollectionData,
|
|
8
|
+
CreateCollectionInput,
|
|
9
|
+
ListCollectionsInput,
|
|
10
|
+
} from "./types.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Ask people for what you do not have.
|
|
14
|
+
*
|
|
15
|
+
* The third of the three, and the one that goes the other way. `approve()`
|
|
16
|
+
* asks permission, `review()` asks judgement, and this asks for a fact: a
|
|
17
|
+
* figure, a date, a choice between options, the sentence only they can write.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface CollectWaitOptions {
|
|
21
|
+
/** Called after every poll, for a progress line in a CLI. */
|
|
22
|
+
onPoll?: (collection: CollectionData) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Give up after this many milliseconds and throw `DeliverdTimeoutError`.
|
|
25
|
+
* Omitted, the wait ends when the request does — which, with no `expiresAt`,
|
|
26
|
+
* may be never. Set one or the other.
|
|
27
|
+
*/
|
|
28
|
+
timeout?: number;
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
pollIntervals?: readonly number[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function pollDelay(poll: number, schedule: readonly number[]): number {
|
|
34
|
+
const base = schedule[Math.min(poll, schedule.length - 1)] ?? 60_000;
|
|
35
|
+
return base * 0.75 + Math.random() * base * 0.5;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A request for answers, and what you can do with it. */
|
|
39
|
+
export class Collection {
|
|
40
|
+
constructor(
|
|
41
|
+
private readonly http: HttpClient,
|
|
42
|
+
private data: CollectionData
|
|
43
|
+
) {}
|
|
44
|
+
|
|
45
|
+
get id(): string { return this.data.id; }
|
|
46
|
+
get status(): CollectionData["status"] { return this.data.status; }
|
|
47
|
+
/** The page the people you asked will fill in. */
|
|
48
|
+
get url(): string { return this.data.url; }
|
|
49
|
+
get title(): string { return this.data.title; }
|
|
50
|
+
get responses(): CollectionData["responses"] { return this.data.responses; }
|
|
51
|
+
get submittedCount(): number { return this.data.submittedCount; }
|
|
52
|
+
get askedCount(): number { return this.data.askedCount; }
|
|
53
|
+
|
|
54
|
+
/** The one to branch on. False for expired and cancelled. */
|
|
55
|
+
get complete(): boolean { return this.data.status === "complete"; }
|
|
56
|
+
get expired(): boolean { return this.data.status === "expired"; }
|
|
57
|
+
get cancelled(): boolean { return this.data.status === "cancelled"; }
|
|
58
|
+
get pending(): boolean { return this.data.status === "pending"; }
|
|
59
|
+
|
|
60
|
+
toJSON(): CollectionData { return this.data; }
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The first submitted answer to each question, keyed by its prompt.
|
|
64
|
+
*
|
|
65
|
+
* The shape most callers want, because most callers asked one person for
|
|
66
|
+
* three facts. When several people answered the same question, later
|
|
67
|
+
* replies do not overwrite the first — use `responses` when you need all of
|
|
68
|
+
* them, and note that this returns the earliest submission rather than a
|
|
69
|
+
* merge, since silently blending two people's answers would be worse than
|
|
70
|
+
* making you choose.
|
|
71
|
+
*/
|
|
72
|
+
get answers(): Record<string, AnswerValue> {
|
|
73
|
+
const out: Record<string, AnswerValue> = {};
|
|
74
|
+
for (const reply of this.data.responses) {
|
|
75
|
+
if (reply.status !== "submitted") continue;
|
|
76
|
+
for (const answer of reply.answers) {
|
|
77
|
+
if (!(answer.question in out)) out[answer.question] = answer.value;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
toCollected(): CollectedAnswers {
|
|
84
|
+
const status = this.data.status === "pending" ? "expired" : this.data.status;
|
|
85
|
+
return {
|
|
86
|
+
complete: this.data.status === "complete",
|
|
87
|
+
status,
|
|
88
|
+
responses: this.data.responses.filter((r) => r.status === "submitted"),
|
|
89
|
+
answers: this.answers,
|
|
90
|
+
id: this.data.id,
|
|
91
|
+
url: this.data.url,
|
|
92
|
+
collection: this.data,
|
|
93
|
+
} as CollectedAnswers;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async refresh(signal?: AbortSignal): Promise<this> {
|
|
97
|
+
this.data = await this.http.request<CollectionData>({
|
|
98
|
+
method: "GET",
|
|
99
|
+
path: `/collections/${this.data.id}`,
|
|
100
|
+
signal,
|
|
101
|
+
});
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Withdraw it. The row stays as evidence of what was asked and dropped. */
|
|
106
|
+
async cancel(signal?: AbortSignal): Promise<this> {
|
|
107
|
+
this.data = await this.http.request<CollectionData>({
|
|
108
|
+
method: "POST",
|
|
109
|
+
path: `/collections/${this.data.id}/cancel`,
|
|
110
|
+
signal,
|
|
111
|
+
});
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Block until enough people have answered, then return.
|
|
117
|
+
*
|
|
118
|
+
* Resolves on every ending, including expiry — which is an answer of a
|
|
119
|
+
* kind, and one you must handle rather than treat as an error, because the
|
|
120
|
+
* thing you do without the figure is usually different from the thing you
|
|
121
|
+
* do with it.
|
|
122
|
+
*/
|
|
123
|
+
async wait(options: CollectWaitOptions = {}): Promise<this> {
|
|
124
|
+
const deadline = options.timeout !== undefined ? Date.now() + options.timeout : undefined;
|
|
125
|
+
let poll = 0;
|
|
126
|
+
if (!this.pending) return this;
|
|
127
|
+
|
|
128
|
+
for (;;) {
|
|
129
|
+
if (options.signal?.aborted) {
|
|
130
|
+
throw new DeliverdError(0, "aborted", "The wait was aborted.", undefined, {
|
|
131
|
+
cause: options.signal.reason,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
if (deadline !== undefined && Date.now() >= deadline) {
|
|
135
|
+
throw new DeliverdTimeoutError(
|
|
136
|
+
`Nobody finished answering "${this.data.title}" within the timeout. It is still open at ${this.data.url}.`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const delay = pollDelay(poll++, options.pollIntervals ?? POLL_SCHEDULE);
|
|
140
|
+
const remaining = deadline === undefined ? delay : Math.min(delay, deadline - Date.now());
|
|
141
|
+
await sleep(Math.max(0, remaining), options.signal);
|
|
142
|
+
|
|
143
|
+
await this.refresh(options.signal);
|
|
144
|
+
options.onPoll?.(this.data);
|
|
145
|
+
if (!this.pending) return this;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
if (signal?.aborted) {
|
|
153
|
+
return reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
154
|
+
}
|
|
155
|
+
const timer = setTimeout(() => {
|
|
156
|
+
signal?.removeEventListener("abort", onAbort);
|
|
157
|
+
resolve();
|
|
158
|
+
}, ms);
|
|
159
|
+
const onAbort = () => {
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
162
|
+
};
|
|
163
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export class Collections {
|
|
168
|
+
constructor(private readonly http: HttpClient) {}
|
|
169
|
+
|
|
170
|
+
/** Ask. Returns as soon as the request is filed; call `wait()` next. */
|
|
171
|
+
async create(input: CreateCollectionInput, signal?: AbortSignal): Promise<Collection> {
|
|
172
|
+
const data = await this.http.request<CollectionData>({
|
|
173
|
+
method: "POST",
|
|
174
|
+
path: "/collections",
|
|
175
|
+
body: input,
|
|
176
|
+
signal,
|
|
177
|
+
});
|
|
178
|
+
return new Collection(this.http, data);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async get(id: string, signal?: AbortSignal): Promise<Collection> {
|
|
182
|
+
const data = await this.http.request<CollectionData>({
|
|
183
|
+
method: "GET",
|
|
184
|
+
path: `/collections/${id}`,
|
|
185
|
+
signal,
|
|
186
|
+
});
|
|
187
|
+
return new Collection(this.http, data);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async list(input: ListCollectionsInput = {}, signal?: AbortSignal): Promise<Collection[]> {
|
|
191
|
+
const { collections } = await this.http.request<{ collections: CollectionData[] }>({
|
|
192
|
+
method: "GET",
|
|
193
|
+
path: "/collections",
|
|
194
|
+
query: { status: input.status, externalId: input.externalId, limit: input.limit },
|
|
195
|
+
signal,
|
|
196
|
+
});
|
|
197
|
+
return collections.map((c) => new Collection(this.http, c));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Ask and wait, for the common case where there is nothing to do between. */
|
|
201
|
+
async request(
|
|
202
|
+
input: CreateCollectionInput,
|
|
203
|
+
options: CollectWaitOptions = {}
|
|
204
|
+
): Promise<Collection> {
|
|
205
|
+
const collection = await this.create(input, options.signal);
|
|
206
|
+
return collection.wait(options);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The same request as before, when there was one — so a restart does not ask twice. */
|
|
210
|
+
async findByExternalId(externalId: string, signal?: AbortSignal): Promise<Collection | null> {
|
|
211
|
+
const found = await this.list({ externalId, limit: 1 }, signal);
|
|
212
|
+
return found[0] ?? null;
|
|
213
|
+
}
|
|
214
|
+
}
|