@deliverd/sdk 0.2.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 +54 -8
- 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 +141 -2
- package/dist/index.d.ts +44 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +53 -0
- package/dist/types.d.ts +100 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/collections.ts +214 -0
- package/src/dev.ts +160 -3
- package/src/index.ts +80 -0
- package/src/types.ts +117 -1
package/dist/index.js
CHANGED
|
@@ -2,12 +2,14 @@ import { HttpClient } from "./http.js";
|
|
|
2
2
|
import { Approvals, FAST_POLL_SCHEDULE } from "./approvals.js";
|
|
3
3
|
import { Comments, Reports } from "./reports.js";
|
|
4
4
|
import { Reviews } from "./reviews.js";
|
|
5
|
+
import { Collections } from "./collections.js";
|
|
5
6
|
import { Webhooks } from "./webhooks.js";
|
|
6
7
|
import { developmentFetch } from "./dev.js";
|
|
7
8
|
import { durationMs } from "./duration.js";
|
|
8
9
|
export { Approval, Approvals, POLL_SCHEDULE } from "./approvals.js";
|
|
9
10
|
export { Comments, Reports } from "./reports.js";
|
|
10
11
|
export { Review, Reviews } from "./reviews.js";
|
|
12
|
+
export { Collection, Collections } from "./collections.js";
|
|
11
13
|
export { constructEvent, DELIVERY_HEADER, EVENT_HEADER, SIGNATURE_HEADER, TOLERANCE_SECONDS, verifySignature, Webhooks, } from "./webhooks.js";
|
|
12
14
|
export { DeliverdError, DeliverdTimeoutError } from "./errors.js";
|
|
13
15
|
export { DEFAULT_BASE_URL } from "./http.js";
|
|
@@ -36,6 +38,7 @@ export const DEFAULT_EXPIRY = "24h";
|
|
|
36
38
|
export class Deliverd {
|
|
37
39
|
approvals;
|
|
38
40
|
reviews;
|
|
41
|
+
collections;
|
|
39
42
|
reports;
|
|
40
43
|
comments;
|
|
41
44
|
webhooks;
|
|
@@ -54,6 +57,7 @@ export class Deliverd {
|
|
|
54
57
|
});
|
|
55
58
|
this.approvals = new Approvals(this.http);
|
|
56
59
|
this.reviews = new Reviews(this.http);
|
|
60
|
+
this.collections = new Collections(this.http);
|
|
57
61
|
this.reports = new Reports(this.http);
|
|
58
62
|
this.comments = new Comments(this.http);
|
|
59
63
|
this.webhooks = new Webhooks();
|
|
@@ -166,6 +170,55 @@ export class Deliverd {
|
|
|
166
170
|
});
|
|
167
171
|
return settled.toOutcome();
|
|
168
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Ask people for what you do not have, and wait for it.
|
|
175
|
+
*
|
|
176
|
+
* `approve()` asks permission, `review()` asks judgement, and this asks for
|
|
177
|
+
* a fact — a figure, a date, a choice between options, the sentence only
|
|
178
|
+
* they can write.
|
|
179
|
+
*
|
|
180
|
+
* ```ts
|
|
181
|
+
* const { answers } = await deliverd.collect({
|
|
182
|
+
* title: "Before I file the Q3 return",
|
|
183
|
+
* respondents: ["finance@firm.example"],
|
|
184
|
+
* questions: [
|
|
185
|
+
* { prompt: "Headcount at 30 September", kind: "number" },
|
|
186
|
+
* { prompt: "Any disposals in the quarter?", kind: "boolean" },
|
|
187
|
+
* ],
|
|
188
|
+
* });
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* `answers` is keyed by the question you asked. Check `complete` before you
|
|
192
|
+
* use it: a request that expired has whatever arrived and no more, and the
|
|
193
|
+
* thing you do without the figure is usually not the thing you do with it.
|
|
194
|
+
*/
|
|
195
|
+
async collect(options) {
|
|
196
|
+
const { expiresIn, timeout, onPoll, signal, reuse, ...input } = options;
|
|
197
|
+
if (input.externalId && reuse !== false) {
|
|
198
|
+
const existing = await this.collections.findByExternalId(input.externalId, signal);
|
|
199
|
+
if (existing) {
|
|
200
|
+
const settled = await existing.wait({
|
|
201
|
+
onPoll,
|
|
202
|
+
signal,
|
|
203
|
+
timeout: durationOrUndefined(timeout),
|
|
204
|
+
...(this.development ? { pollIntervals: FAST_POLL_SCHEDULE } : {}),
|
|
205
|
+
});
|
|
206
|
+
return settled.toCollected();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const collection = await this.collections.create({
|
|
210
|
+
...input,
|
|
211
|
+
expiresAt: options.expiresAt ??
|
|
212
|
+
new Date(Date.now() + durationMs(expiresIn ?? DEFAULT_EXPIRY)).toISOString(),
|
|
213
|
+
}, signal);
|
|
214
|
+
const settled = await collection.wait({
|
|
215
|
+
onPoll,
|
|
216
|
+
signal,
|
|
217
|
+
timeout: durationOrUndefined(timeout) ?? (this.development ? 2_000 : undefined),
|
|
218
|
+
...(this.development ? { pollIntervals: FAST_POLL_SCHEDULE } : {}),
|
|
219
|
+
});
|
|
220
|
+
return settled.toCollected();
|
|
221
|
+
}
|
|
169
222
|
/** Publish HTML to a secure URL. The other half of the product, one call in. */
|
|
170
223
|
publish(input, signal) {
|
|
171
224
|
return this.reports.publish(input, signal);
|
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" | "review.requested" | "review.approved" | "review.changes_requested";
|
|
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;
|
|
@@ -258,4 +258,103 @@ export interface ListReviewsInput {
|
|
|
258
258
|
reportId?: string;
|
|
259
259
|
limit?: number;
|
|
260
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
|
+
}
|
|
261
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,WAAW,CAAC;AAC3F,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;AAElE,2DAA2D;AAC3D,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,cAAc,CAAC;IACvB,wCAAwC;IACxC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,GAAG,IAAI,CAA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AI,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,4DAA4D;AAC5D,MAAM,WAAW,gBAAgB;IAC/B,6EAA6E;IAC7E,QAAQ,EAAE,OAAO,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAC;IAC7C,wDAAwD;IACxD,SAAS,EAAE,eAAe,EAAE,CAAC;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACrC,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,cAAc,CAAC;CAC5B;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,gBAAgB,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;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
|
+
}
|
package/src/dev.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ApprovalData, ReviewData } from "./types.js";
|
|
1
|
+
import type { ApprovalData, CollectionData, ReviewData } from "./types.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Development mode: the whole loop, on your machine, with nobody in it.
|
|
@@ -52,7 +52,7 @@ interface Stored {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
|
-
* A `fetch` that serves the approvals and
|
|
55
|
+
* A `fetch` that serves the approvals, reviews and collections APIs from memory.
|
|
56
56
|
*
|
|
57
57
|
* Deliberately narrow: it answers the calls the primitives make and 501s the
|
|
58
58
|
* rest, with a message saying so, rather than pretending to be the whole API
|
|
@@ -61,6 +61,7 @@ interface Stored {
|
|
|
61
61
|
export function developmentFetch(options: DevelopmentOptions = {}): typeof globalThis.fetch {
|
|
62
62
|
const store = new Map<string, Stored>();
|
|
63
63
|
const reviewStore = new Map<string, StoredReview>();
|
|
64
|
+
const collectionStore = new Map<string, StoredCollection>();
|
|
64
65
|
|
|
65
66
|
return (async (input: string | URL | Request, init?: RequestInit) => {
|
|
66
67
|
const url = new URL(String(input));
|
|
@@ -217,11 +218,90 @@ export function developmentFetch(options: DevelopmentOptions = {}): typeof globa
|
|
|
217
218
|
});
|
|
218
219
|
}
|
|
219
220
|
|
|
221
|
+
if (method === "POST" && path === "/collections") {
|
|
222
|
+
const id = `col_dev_${++counter}`;
|
|
223
|
+
const outcome = resolveOutcome(options.outcome, body);
|
|
224
|
+
const asked = ((body.respondents as string[] | undefined) ?? []).map(String);
|
|
225
|
+
const questions = ((body.questions as Record<string, unknown>[] | undefined) ?? []).map(
|
|
226
|
+
(q, i) => ({
|
|
227
|
+
id: `q_dev_${i + 1}`,
|
|
228
|
+
prompt: String(q.prompt ?? "Question"),
|
|
229
|
+
help: (q.help as string | undefined) ?? null,
|
|
230
|
+
kind: (q.kind as CollectionData["questions"][number]["kind"]) ?? "text",
|
|
231
|
+
required: q.required !== false,
|
|
232
|
+
options: (q.options as string[] | undefined) ?? [],
|
|
233
|
+
})
|
|
234
|
+
);
|
|
235
|
+
const data: CollectionData = {
|
|
236
|
+
id,
|
|
237
|
+
status: "pending",
|
|
238
|
+
url: `https://deliverd.dev/collect/${id}`,
|
|
239
|
+
title: String(body.title ?? "Untitled"),
|
|
240
|
+
instructions: (body.instructions as string | undefined) ?? null,
|
|
241
|
+
reportId: (body.reportId as string | undefined) ?? null,
|
|
242
|
+
externalId: (body.externalId as string | undefined) ?? null,
|
|
243
|
+
metadata: (body.metadata as Record<string, unknown>) ?? {},
|
|
244
|
+
requestedBy: "You (development mode)",
|
|
245
|
+
requiredResponses: (body.requiredResponses as number | undefined) ?? (asked.length || 1),
|
|
246
|
+
questions,
|
|
247
|
+
responses: asked.map((who) => ({
|
|
248
|
+
respondent: who,
|
|
249
|
+
status: "pending" as const,
|
|
250
|
+
submittedAt: null,
|
|
251
|
+
answers: [],
|
|
252
|
+
})),
|
|
253
|
+
submittedCount: 0,
|
|
254
|
+
askedCount: asked.length,
|
|
255
|
+
settledAt: null,
|
|
256
|
+
dueAt: (body.dueAt as string | undefined) ?? null,
|
|
257
|
+
expiresAt: (body.expiresAt as string | undefined) ?? null,
|
|
258
|
+
createdAt: new Date().toISOString(),
|
|
259
|
+
};
|
|
260
|
+
collectionStore.set(id, {
|
|
261
|
+
data,
|
|
262
|
+
settleAt: Date.now() + (options.delay ?? 0),
|
|
263
|
+
/*
|
|
264
|
+
* A simulated rejection becomes an expiry here: there is no way to
|
|
265
|
+
* say no to a question, only to leave it unanswered, and that is the
|
|
266
|
+
* ending a caller most needs to have thought about.
|
|
267
|
+
*/
|
|
268
|
+
expire: outcome === "rejected",
|
|
269
|
+
timeout: outcome === "timeout",
|
|
270
|
+
});
|
|
271
|
+
if (options.log !== false) announceCollection(data, outcome);
|
|
272
|
+
return json(data, 201);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const collectionMatch = /^\/collections\/([^/]+)(\/cancel)?$/.exec(path);
|
|
276
|
+
if (collectionMatch) {
|
|
277
|
+
const stored = collectionStore.get(collectionMatch[1]!);
|
|
278
|
+
if (!stored) {
|
|
279
|
+
return json(
|
|
280
|
+
{ error: { code: "not_found", message: "No such request in this development session." } },
|
|
281
|
+
404
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
if (collectionMatch[2] === "/cancel") {
|
|
285
|
+
stored.data.status = "cancelled";
|
|
286
|
+
return json(stored.data);
|
|
287
|
+
}
|
|
288
|
+
settleCollection(stored);
|
|
289
|
+
return json(stored.data);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (method === "GET" && path === "/collections") {
|
|
293
|
+
const externalId = url.searchParams.get("externalId");
|
|
294
|
+
const all = [...collectionStore.values()].map((c) => c.data);
|
|
295
|
+
return json({
|
|
296
|
+
collections: externalId ? all.filter((c) => c.externalId === externalId) : all,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
220
300
|
return json(
|
|
221
301
|
{
|
|
222
302
|
error: {
|
|
223
303
|
code: "not_in_development_mode",
|
|
224
|
-
message: `Development mode simulates approvals and
|
|
304
|
+
message: `Development mode simulates approvals, reviews and collections only, and something called ${method} ${path}. Use a real API key for the rest of the API.`,
|
|
225
305
|
},
|
|
226
306
|
},
|
|
227
307
|
501
|
|
@@ -326,3 +406,80 @@ function announceReview(
|
|
|
326
406
|
];
|
|
327
407
|
console.log(lines.join("\n"));
|
|
328
408
|
}
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
interface StoredCollection {
|
|
412
|
+
data: CollectionData;
|
|
413
|
+
settleAt: number;
|
|
414
|
+
expire: boolean;
|
|
415
|
+
timeout: boolean;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Everyone answers, at the same moment, with a plausible value for the kind
|
|
420
|
+
* of question asked. Plausible rather than empty, because a caller writing
|
|
421
|
+
* `answers["Headcount at 30 September"] + 1` should find out here that it is
|
|
422
|
+
* a number, not in production.
|
|
423
|
+
*/
|
|
424
|
+
function settleCollection(stored: StoredCollection): void {
|
|
425
|
+
if (stored.timeout || stored.data.status !== "pending") return;
|
|
426
|
+
if (Date.now() < stored.settleAt) return;
|
|
427
|
+
if (stored.expire) {
|
|
428
|
+
stored.data.status = "expired";
|
|
429
|
+
stored.data.settledAt = new Date().toISOString();
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const at = new Date().toISOString();
|
|
433
|
+
stored.data.responses = stored.data.responses.map((r) => ({
|
|
434
|
+
...r,
|
|
435
|
+
status: "submitted" as const,
|
|
436
|
+
submittedAt: at,
|
|
437
|
+
answers: stored.data.questions.map((q) => ({
|
|
438
|
+
question: q.prompt,
|
|
439
|
+
kind: q.kind,
|
|
440
|
+
value: sampleAnswer(q),
|
|
441
|
+
})),
|
|
442
|
+
}));
|
|
443
|
+
stored.data.submittedCount = stored.data.responses.length;
|
|
444
|
+
stored.data.status = "complete";
|
|
445
|
+
stored.data.settledAt = at;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function sampleAnswer(q: CollectionData["questions"][number]): string | number | boolean | string[] {
|
|
449
|
+
switch (q.kind) {
|
|
450
|
+
case "number":
|
|
451
|
+
return 42;
|
|
452
|
+
case "boolean":
|
|
453
|
+
return true;
|
|
454
|
+
case "date":
|
|
455
|
+
return new Date().toISOString().slice(0, 10);
|
|
456
|
+
case "choice":
|
|
457
|
+
return q.options[0] ?? "";
|
|
458
|
+
case "multi_choice":
|
|
459
|
+
return q.options.slice(0, 1);
|
|
460
|
+
case "long_text":
|
|
461
|
+
return "Simulated: a longer answer than a line.";
|
|
462
|
+
default:
|
|
463
|
+
return "Simulated answer";
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function announceCollection(data: CollectionData, outcome: string): void {
|
|
468
|
+
const lines = [
|
|
469
|
+
` ┌─ Deliverd · development mode ─────────────────────────`,
|
|
470
|
+
` │ ${data.title}`,
|
|
471
|
+
` │ Asking: ${data.questions.map((q) => q.prompt).join(" · ") || "nothing"}`,
|
|
472
|
+
` │ Of: ${data.responses.map((r) => r.respondent).join(", ") || "nobody named"}`,
|
|
473
|
+
` │`,
|
|
474
|
+
` │ Simulating: ${
|
|
475
|
+
outcome === "timeout"
|
|
476
|
+
? "nobody answers"
|
|
477
|
+
: outcome === "rejected"
|
|
478
|
+
? "it expires unanswered"
|
|
479
|
+
: "everyone answers"
|
|
480
|
+
}`,
|
|
481
|
+
` └───────────────────────────────────────────────────────`,
|
|
482
|
+
"",
|
|
483
|
+
];
|
|
484
|
+
console.log(lines.join("\n"));
|
|
485
|
+
}
|