@deliverd/sdk 0.2.0 → 0.4.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 +98 -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 +195 -2
- package/dist/flows.d.ts +98 -0
- package/dist/flows.d.ts.map +1 -0
- package/dist/flows.js +136 -0
- package/dist/index.d.ts +47 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +62 -0
- package/dist/types.d.ts +174 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/collections.ts +214 -0
- package/src/dev.ts +223 -3
- package/src/flows.ts +185 -0
- package/src/index.ts +89 -0
- package/src/types.ts +200 -1
package/README.md
CHANGED
|
@@ -107,8 +107,6 @@ request it already made rather than asking a second time.
|
|
|
107
107
|
|
|
108
108
|
## Answering the approver
|
|
109
109
|
|
|
110
|
-
## Answering the approver
|
|
111
|
-
|
|
112
110
|
The thing that makes this more than a pause button: a person can ask your
|
|
113
111
|
agent a question from the approval page, and your agent can answer it without
|
|
114
112
|
anyone leaving what they are doing.
|
|
@@ -156,16 +154,108 @@ neither can be waited on for ever, which is what `await` means.
|
|
|
156
154
|
```ts
|
|
157
155
|
await deliverd.approve({ title }); // ask permission, and wait for the answer
|
|
158
156
|
await deliverd.review({ title, reportId, reviewers }); // ask whether it is right
|
|
157
|
+
await deliverd.collect({ title, questions, respondents }); // ask for what you lack
|
|
159
158
|
await deliverd.confirm({ title }); // a yes or no on something small → boolean
|
|
160
159
|
await deliverd.publish({ title, content }); // get the work to the people who need it
|
|
161
160
|
```
|
|
162
161
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
162
|
+
All five. Each one is a person your agent needs and cannot be.
|
|
163
|
+
|
|
164
|
+
## Flows
|
|
165
|
+
|
|
166
|
+
A job that takes more than one of them is one job, and a firm asking about it
|
|
167
|
+
afterwards asks about the job — not about approvals in October. A flow is the
|
|
168
|
+
thread that ties them together.
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const flow = await deliverd.flows.resume({
|
|
172
|
+
title: "Q3 close",
|
|
173
|
+
externalId: "close_2026_q3", // so a restart extends this one
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const { answers } = await flow.collect({ title: "Before I start", questions, respondents });
|
|
177
|
+
const outcome = await flow.review({ title: "Draft pack", reportId, reviewers });
|
|
178
|
+
if (outcome.approved) await deliverd.publish({ title, content, flowId: flow.id });
|
|
179
|
+
|
|
180
|
+
await flow.close();
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
`flow.approve()`, `flow.review()` and `flow.collect()` are the same calls as
|
|
184
|
+
the ones above with the flow already named. That is the whole point of the
|
|
185
|
+
handle: a `flowId` passed by hand at four call sites is one that gets left off
|
|
186
|
+
the fifth, and the step is then missing from the history with nothing to say
|
|
187
|
+
so. Publishing takes `flowId` directly, because a report joins a flow when it
|
|
188
|
+
is first created rather than through a handle.
|
|
189
|
+
|
|
190
|
+
It groups and does nothing else. Nothing waits on a flow, nothing is blocked
|
|
191
|
+
by one, and there are no steps to declare in advance — do the work in the
|
|
192
|
+
order you want it done and name the flow as you go.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
flow.allSettled // nothing in it is waiting on anybody right now
|
|
196
|
+
flow.openCount // how many still are
|
|
197
|
+
flow.timeline // every audited step across all of them, in order
|
|
198
|
+
await flow.refresh()
|
|
199
|
+
await flow.close() // say the work is done; it stops taking new members
|
|
200
|
+
await flow.cancel() // call it off; nothing inside it is cancelled
|
|
201
|
+
await deliverd.flows.list({ status: "open" });
|
|
202
|
+
await deliverd.flows.get(id);
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
`allSettled` is a fact about this moment, not a verdict. You may be about to
|
|
206
|
+
add the next step, so nothing closes a flow but you.
|
|
207
|
+
|
|
208
|
+
## Collecting
|
|
209
|
+
|
|
210
|
+
`approve()` asks permission, `review()` asks judgement, and this asks for a
|
|
211
|
+
fact — a figure, a date, a choice between options, the sentence only they can
|
|
212
|
+
write.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const { complete, answers } = await deliverd.collect({
|
|
216
|
+
title: "Before I file the Q3 return",
|
|
217
|
+
respondents: ["finance@firm.example"],
|
|
218
|
+
questions: [
|
|
219
|
+
{ prompt: "Headcount at 30 September", kind: "number" },
|
|
220
|
+
{ prompt: "Any disposals in the quarter?", kind: "boolean" },
|
|
221
|
+
{ prompt: "Which basis?", kind: "choice", options: ["Accrual", "Cash"] },
|
|
222
|
+
],
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
if (!complete) return escalate();
|
|
226
|
+
file(answers["Headcount at 30 September"]);
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
`answers` is keyed by the question you asked, and carries the first submitted
|
|
230
|
+
reply to each. Check `complete` before using it: a request that expired has
|
|
231
|
+
whatever arrived and no more, and what you do without the figure is usually
|
|
232
|
+
not what you do with it. Use `responses` when several people were asked and
|
|
233
|
+
you need all of them rather than the first.
|
|
234
|
+
|
|
235
|
+
Seven kinds of question: `text`, `long_text`, `number`, `date`, `choice`,
|
|
236
|
+
`multi_choice` and `boolean`. Every answer is checked against the kind that
|
|
237
|
+
asked for it before it is stored, and a required question blocks submission —
|
|
238
|
+
so a number field gives you a number, not the string a person typed.
|
|
239
|
+
|
|
240
|
+
There are no file questions. Answers are typed values, and a `file` kind with
|
|
241
|
+
no storage behind it would be a promise the page could not keep.
|
|
242
|
+
|
|
243
|
+
A respondent must be **an active member of your organisation, or a guest the
|
|
244
|
+
organisation already knows**. Anyone else is refused rather than emailed,
|
|
245
|
+
which is what stops an agent reaching an address of its choosing.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
await deliverd.collections.create(input); // ask without waiting
|
|
249
|
+
await deliverd.collections.get(id);
|
|
250
|
+
await deliverd.collections.list({ status: "pending" });
|
|
251
|
+
await deliverd.collections.findByExternalId("q3_close");
|
|
252
|
+
await collection.wait({ onPoll });
|
|
253
|
+
await collection.cancel();
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
There is no method that submits an answer, and no endpoint behind one. People
|
|
257
|
+
answer on the page. They get one reminder before the due date and no more,
|
|
258
|
+
because a nudge that arrives twice teaches people to ignore the first.
|
|
169
259
|
|
|
170
260
|
## Reviewing
|
|
171
261
|
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { HttpClient } from "./http.js";
|
|
2
|
+
import type { AnswerValue, CollectedAnswers, CollectionData, CreateCollectionInput, ListCollectionsInput } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Ask people for what you do not have.
|
|
5
|
+
*
|
|
6
|
+
* The third of the three, and the one that goes the other way. `approve()`
|
|
7
|
+
* asks permission, `review()` asks judgement, and this asks for a fact: a
|
|
8
|
+
* figure, a date, a choice between options, the sentence only they can write.
|
|
9
|
+
*/
|
|
10
|
+
export interface CollectWaitOptions {
|
|
11
|
+
/** Called after every poll, for a progress line in a CLI. */
|
|
12
|
+
onPoll?: (collection: CollectionData) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Give up after this many milliseconds and throw `DeliverdTimeoutError`.
|
|
15
|
+
* Omitted, the wait ends when the request does — which, with no `expiresAt`,
|
|
16
|
+
* may be never. Set one or the other.
|
|
17
|
+
*/
|
|
18
|
+
timeout?: number;
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
pollIntervals?: readonly number[];
|
|
21
|
+
}
|
|
22
|
+
/** A request for answers, and what you can do with it. */
|
|
23
|
+
export declare class Collection {
|
|
24
|
+
private readonly http;
|
|
25
|
+
private data;
|
|
26
|
+
constructor(http: HttpClient, data: CollectionData);
|
|
27
|
+
get id(): string;
|
|
28
|
+
get status(): CollectionData["status"];
|
|
29
|
+
/** The page the people you asked will fill in. */
|
|
30
|
+
get url(): string;
|
|
31
|
+
get title(): string;
|
|
32
|
+
get responses(): CollectionData["responses"];
|
|
33
|
+
get submittedCount(): number;
|
|
34
|
+
get askedCount(): number;
|
|
35
|
+
/** The one to branch on. False for expired and cancelled. */
|
|
36
|
+
get complete(): boolean;
|
|
37
|
+
get expired(): boolean;
|
|
38
|
+
get cancelled(): boolean;
|
|
39
|
+
get pending(): boolean;
|
|
40
|
+
toJSON(): CollectionData;
|
|
41
|
+
/**
|
|
42
|
+
* The first submitted answer to each question, keyed by its prompt.
|
|
43
|
+
*
|
|
44
|
+
* The shape most callers want, because most callers asked one person for
|
|
45
|
+
* three facts. When several people answered the same question, later
|
|
46
|
+
* replies do not overwrite the first — use `responses` when you need all of
|
|
47
|
+
* them, and note that this returns the earliest submission rather than a
|
|
48
|
+
* merge, since silently blending two people's answers would be worse than
|
|
49
|
+
* making you choose.
|
|
50
|
+
*/
|
|
51
|
+
get answers(): Record<string, AnswerValue>;
|
|
52
|
+
toCollected(): CollectedAnswers;
|
|
53
|
+
refresh(signal?: AbortSignal): Promise<this>;
|
|
54
|
+
/** Withdraw it. The row stays as evidence of what was asked and dropped. */
|
|
55
|
+
cancel(signal?: AbortSignal): Promise<this>;
|
|
56
|
+
/**
|
|
57
|
+
* Block until enough people have answered, then return.
|
|
58
|
+
*
|
|
59
|
+
* Resolves on every ending, including expiry — which is an answer of a
|
|
60
|
+
* kind, and one you must handle rather than treat as an error, because the
|
|
61
|
+
* thing you do without the figure is usually different from the thing you
|
|
62
|
+
* do with it.
|
|
63
|
+
*/
|
|
64
|
+
wait(options?: CollectWaitOptions): Promise<this>;
|
|
65
|
+
}
|
|
66
|
+
export declare class Collections {
|
|
67
|
+
private readonly http;
|
|
68
|
+
constructor(http: HttpClient);
|
|
69
|
+
/** Ask. Returns as soon as the request is filed; call `wait()` next. */
|
|
70
|
+
create(input: CreateCollectionInput, signal?: AbortSignal): Promise<Collection>;
|
|
71
|
+
get(id: string, signal?: AbortSignal): Promise<Collection>;
|
|
72
|
+
list(input?: ListCollectionsInput, signal?: AbortSignal): Promise<Collection[]>;
|
|
73
|
+
/** Ask and wait, for the common case where there is nothing to do between. */
|
|
74
|
+
request(input: CreateCollectionInput, options?: CollectWaitOptions): Promise<Collection>;
|
|
75
|
+
/** The same request as before, when there was one — so a restart does not ask twice. */
|
|
76
|
+
findByExternalId(externalId: string, signal?: AbortSignal): Promise<Collection | null>;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=collections.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.d.ts","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE5C,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB;;;;;;GAMG;AAEH,MAAM,WAAW,kBAAkB;IACjC,6DAA6D;IAC7D,MAAM,CAAC,EAAE,CAAC,UAAU,EAAE,cAAc,KAAK,IAAI,CAAC;IAC9C;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAOD,0DAA0D;AAC1D,qBAAa,UAAU;IAEnB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,IAAI;gBADK,IAAI,EAAE,UAAU,EACzB,IAAI,EAAE,cAAc;IAG9B,IAAI,EAAE,IAAI,MAAM,CAAyB;IACzC,IAAI,MAAM,IAAI,cAAc,CAAC,QAAQ,CAAC,CAA6B;IACnE,kDAAkD;IAClD,IAAI,GAAG,IAAI,MAAM,CAA0B;IAC3C,IAAI,KAAK,IAAI,MAAM,CAA4B;IAC/C,IAAI,SAAS,IAAI,cAAc,CAAC,WAAW,CAAC,CAAgC;IAC5E,IAAI,cAAc,IAAI,MAAM,CAAqC;IACjE,IAAI,UAAU,IAAI,MAAM,CAAiC;IAEzD,6DAA6D;IAC7D,IAAI,QAAQ,IAAI,OAAO,CAA4C;IACnE,IAAI,OAAO,IAAI,OAAO,CAA2C;IACjE,IAAI,SAAS,IAAI,OAAO,CAA6C;IACrE,IAAI,OAAO,IAAI,OAAO,CAA2C;IAEjE,MAAM,IAAI,cAAc;IAExB;;;;;;;;;OASG;IACH,IAAI,OAAO,IAAI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CASzC;IAED,WAAW,IAAI,gBAAgB;IAazB,OAAO,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IASlD,4EAA4E;IACtE,MAAM,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IASjD;;;;;;;OAOG;IACG,IAAI,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;CAyB5D;AAmBD,qBAAa,WAAW;IACV,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,EAAE,UAAU;IAE7C,wEAAwE;IAClE,MAAM,CAAC,KAAK,EAAE,qBAAqB,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAU/E,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAS1D,IAAI,CAAC,KAAK,GAAE,oBAAyB,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAUzF,8EAA8E;IACxE,OAAO,CACX,KAAK,EAAE,qBAAqB,EAC5B,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,UAAU,CAAC;IAKtB,wFAAwF;IAClF,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;CAI7F"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { DeliverdError, DeliverdTimeoutError } from "./errors.js";
|
|
2
|
+
import { POLL_SCHEDULE } from "./approvals.js";
|
|
3
|
+
function pollDelay(poll, schedule) {
|
|
4
|
+
const base = schedule[Math.min(poll, schedule.length - 1)] ?? 60_000;
|
|
5
|
+
return base * 0.75 + Math.random() * base * 0.5;
|
|
6
|
+
}
|
|
7
|
+
/** A request for answers, and what you can do with it. */
|
|
8
|
+
export class Collection {
|
|
9
|
+
http;
|
|
10
|
+
data;
|
|
11
|
+
constructor(http, data) {
|
|
12
|
+
this.http = http;
|
|
13
|
+
this.data = data;
|
|
14
|
+
}
|
|
15
|
+
get id() { return this.data.id; }
|
|
16
|
+
get status() { return this.data.status; }
|
|
17
|
+
/** The page the people you asked will fill in. */
|
|
18
|
+
get url() { return this.data.url; }
|
|
19
|
+
get title() { return this.data.title; }
|
|
20
|
+
get responses() { return this.data.responses; }
|
|
21
|
+
get submittedCount() { return this.data.submittedCount; }
|
|
22
|
+
get askedCount() { return this.data.askedCount; }
|
|
23
|
+
/** The one to branch on. False for expired and cancelled. */
|
|
24
|
+
get complete() { return this.data.status === "complete"; }
|
|
25
|
+
get expired() { return this.data.status === "expired"; }
|
|
26
|
+
get cancelled() { return this.data.status === "cancelled"; }
|
|
27
|
+
get pending() { return this.data.status === "pending"; }
|
|
28
|
+
toJSON() { return this.data; }
|
|
29
|
+
/**
|
|
30
|
+
* The first submitted answer to each question, keyed by its prompt.
|
|
31
|
+
*
|
|
32
|
+
* The shape most callers want, because most callers asked one person for
|
|
33
|
+
* three facts. When several people answered the same question, later
|
|
34
|
+
* replies do not overwrite the first — use `responses` when you need all of
|
|
35
|
+
* them, and note that this returns the earliest submission rather than a
|
|
36
|
+
* merge, since silently blending two people's answers would be worse than
|
|
37
|
+
* making you choose.
|
|
38
|
+
*/
|
|
39
|
+
get answers() {
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const reply of this.data.responses) {
|
|
42
|
+
if (reply.status !== "submitted")
|
|
43
|
+
continue;
|
|
44
|
+
for (const answer of reply.answers) {
|
|
45
|
+
if (!(answer.question in out))
|
|
46
|
+
out[answer.question] = answer.value;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
toCollected() {
|
|
52
|
+
const status = this.data.status === "pending" ? "expired" : this.data.status;
|
|
53
|
+
return {
|
|
54
|
+
complete: this.data.status === "complete",
|
|
55
|
+
status,
|
|
56
|
+
responses: this.data.responses.filter((r) => r.status === "submitted"),
|
|
57
|
+
answers: this.answers,
|
|
58
|
+
id: this.data.id,
|
|
59
|
+
url: this.data.url,
|
|
60
|
+
collection: this.data,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async refresh(signal) {
|
|
64
|
+
this.data = await this.http.request({
|
|
65
|
+
method: "GET",
|
|
66
|
+
path: `/collections/${this.data.id}`,
|
|
67
|
+
signal,
|
|
68
|
+
});
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
/** Withdraw it. The row stays as evidence of what was asked and dropped. */
|
|
72
|
+
async cancel(signal) {
|
|
73
|
+
this.data = await this.http.request({
|
|
74
|
+
method: "POST",
|
|
75
|
+
path: `/collections/${this.data.id}/cancel`,
|
|
76
|
+
signal,
|
|
77
|
+
});
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Block until enough people have answered, then return.
|
|
82
|
+
*
|
|
83
|
+
* Resolves on every ending, including expiry — which is an answer of a
|
|
84
|
+
* kind, and one you must handle rather than treat as an error, because the
|
|
85
|
+
* thing you do without the figure is usually different from the thing you
|
|
86
|
+
* do with it.
|
|
87
|
+
*/
|
|
88
|
+
async wait(options = {}) {
|
|
89
|
+
const deadline = options.timeout !== undefined ? Date.now() + options.timeout : undefined;
|
|
90
|
+
let poll = 0;
|
|
91
|
+
if (!this.pending)
|
|
92
|
+
return this;
|
|
93
|
+
for (;;) {
|
|
94
|
+
if (options.signal?.aborted) {
|
|
95
|
+
throw new DeliverdError(0, "aborted", "The wait was aborted.", undefined, {
|
|
96
|
+
cause: options.signal.reason,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (deadline !== undefined && Date.now() >= deadline) {
|
|
100
|
+
throw new DeliverdTimeoutError(`Nobody finished answering "${this.data.title}" within the timeout. It is still open at ${this.data.url}.`);
|
|
101
|
+
}
|
|
102
|
+
const delay = pollDelay(poll++, options.pollIntervals ?? POLL_SCHEDULE);
|
|
103
|
+
const remaining = deadline === undefined ? delay : Math.min(delay, deadline - Date.now());
|
|
104
|
+
await sleep(Math.max(0, remaining), options.signal);
|
|
105
|
+
await this.refresh(options.signal);
|
|
106
|
+
options.onPoll?.(this.data);
|
|
107
|
+
if (!this.pending)
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function sleep(ms, signal) {
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
if (signal?.aborted) {
|
|
115
|
+
return reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
116
|
+
}
|
|
117
|
+
const timer = setTimeout(() => {
|
|
118
|
+
signal?.removeEventListener("abort", onAbort);
|
|
119
|
+
resolve();
|
|
120
|
+
}, ms);
|
|
121
|
+
const onAbort = () => {
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
reject(new DeliverdError(0, "aborted", "The wait was aborted."));
|
|
124
|
+
};
|
|
125
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
export class Collections {
|
|
129
|
+
http;
|
|
130
|
+
constructor(http) {
|
|
131
|
+
this.http = http;
|
|
132
|
+
}
|
|
133
|
+
/** Ask. Returns as soon as the request is filed; call `wait()` next. */
|
|
134
|
+
async create(input, signal) {
|
|
135
|
+
const data = await this.http.request({
|
|
136
|
+
method: "POST",
|
|
137
|
+
path: "/collections",
|
|
138
|
+
body: input,
|
|
139
|
+
signal,
|
|
140
|
+
});
|
|
141
|
+
return new Collection(this.http, data);
|
|
142
|
+
}
|
|
143
|
+
async get(id, signal) {
|
|
144
|
+
const data = await this.http.request({
|
|
145
|
+
method: "GET",
|
|
146
|
+
path: `/collections/${id}`,
|
|
147
|
+
signal,
|
|
148
|
+
});
|
|
149
|
+
return new Collection(this.http, data);
|
|
150
|
+
}
|
|
151
|
+
async list(input = {}, signal) {
|
|
152
|
+
const { collections } = await this.http.request({
|
|
153
|
+
method: "GET",
|
|
154
|
+
path: "/collections",
|
|
155
|
+
query: { status: input.status, externalId: input.externalId, limit: input.limit },
|
|
156
|
+
signal,
|
|
157
|
+
});
|
|
158
|
+
return collections.map((c) => new Collection(this.http, c));
|
|
159
|
+
}
|
|
160
|
+
/** Ask and wait, for the common case where there is nothing to do between. */
|
|
161
|
+
async request(input, options = {}) {
|
|
162
|
+
const collection = await this.create(input, options.signal);
|
|
163
|
+
return collection.wait(options);
|
|
164
|
+
}
|
|
165
|
+
/** The same request as before, when there was one — so a restart does not ask twice. */
|
|
166
|
+
async findByExternalId(externalId, signal) {
|
|
167
|
+
const found = await this.list({ externalId, limit: 1 }, signal);
|
|
168
|
+
return found[0] ?? null;
|
|
169
|
+
}
|
|
170
|
+
}
|
package/dist/dev.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ export interface DevelopmentOptions {
|
|
|
36
36
|
log?: boolean;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
* A `fetch` that serves the approvals and
|
|
39
|
+
* A `fetch` that serves the approvals, reviews and collections APIs from memory.
|
|
40
40
|
*
|
|
41
41
|
* Deliberately narrow: it answers the calls the primitives make and 501s the
|
|
42
42
|
* rest, with a message saying so, rather than pretending to be the whole API
|
package/dist/dev.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../src/dev.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../src/dev.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAwC,MAAM,YAAY,CAAC;AAErF;;;;;;;;;;;;;;;;;;GAkBG;AAEH,MAAM,MAAM,kBAAkB,GAAG,UAAU,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,CAAC;AAElF,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,OAAO,CAAC,EAAE,kBAAkB,GAAG,CAAC,CAAC,OAAO,EAAE,YAAY,KAAK,kBAAkB,CAAC,CAAC;IAC/E,+DAA+D;IAC/D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,gDAAgD;IAChD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6DAA6D;IAC7D,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAcD;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,kBAAuB,GAAG,OAAO,UAAU,CAAC,KAAK,CAwT1F"}
|
package/dist/dev.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const DEFAULT_QUESTION = "Has this been through staging?";
|
|
2
2
|
let counter = 0;
|
|
3
3
|
/**
|
|
4
|
-
* A `fetch` that serves the approvals and
|
|
4
|
+
* A `fetch` that serves the approvals, reviews and collections APIs from memory.
|
|
5
5
|
*
|
|
6
6
|
* Deliberately narrow: it answers the calls the primitives make and 501s the
|
|
7
7
|
* rest, with a message saying so, rather than pretending to be the whole API
|
|
@@ -10,6 +10,8 @@ let counter = 0;
|
|
|
10
10
|
export function developmentFetch(options = {}) {
|
|
11
11
|
const store = new Map();
|
|
12
12
|
const reviewStore = new Map();
|
|
13
|
+
const collectionStore = new Map();
|
|
14
|
+
const flowStore = new Map();
|
|
13
15
|
return (async (input, init) => {
|
|
14
16
|
const url = new URL(String(input));
|
|
15
17
|
const path = url.pathname.replace(/^\/api\/v1/, "");
|
|
@@ -155,10 +157,135 @@ export function developmentFetch(options = {}) {
|
|
|
155
157
|
reviews: externalId ? all.filter((r) => r.externalId === externalId) : all,
|
|
156
158
|
});
|
|
157
159
|
}
|
|
160
|
+
if (method === "POST" && path === "/collections") {
|
|
161
|
+
const id = `col_dev_${++counter}`;
|
|
162
|
+
const outcome = resolveOutcome(options.outcome, body);
|
|
163
|
+
const asked = (body.respondents ?? []).map(String);
|
|
164
|
+
const questions = (body.questions ?? []).map((q, i) => ({
|
|
165
|
+
id: `q_dev_${i + 1}`,
|
|
166
|
+
prompt: String(q.prompt ?? "Question"),
|
|
167
|
+
help: q.help ?? null,
|
|
168
|
+
kind: q.kind ?? "text",
|
|
169
|
+
required: q.required !== false,
|
|
170
|
+
options: q.options ?? [],
|
|
171
|
+
}));
|
|
172
|
+
const data = {
|
|
173
|
+
id,
|
|
174
|
+
status: "pending",
|
|
175
|
+
url: `https://deliverd.dev/collect/${id}`,
|
|
176
|
+
title: String(body.title ?? "Untitled"),
|
|
177
|
+
instructions: body.instructions ?? null,
|
|
178
|
+
reportId: body.reportId ?? null,
|
|
179
|
+
externalId: body.externalId ?? null,
|
|
180
|
+
metadata: body.metadata ?? {},
|
|
181
|
+
requestedBy: "You (development mode)",
|
|
182
|
+
requiredResponses: body.requiredResponses ?? (asked.length || 1),
|
|
183
|
+
questions,
|
|
184
|
+
responses: asked.map((who) => ({
|
|
185
|
+
respondent: who,
|
|
186
|
+
status: "pending",
|
|
187
|
+
submittedAt: null,
|
|
188
|
+
answers: [],
|
|
189
|
+
})),
|
|
190
|
+
submittedCount: 0,
|
|
191
|
+
askedCount: asked.length,
|
|
192
|
+
settledAt: null,
|
|
193
|
+
dueAt: body.dueAt ?? null,
|
|
194
|
+
expiresAt: body.expiresAt ?? null,
|
|
195
|
+
createdAt: new Date().toISOString(),
|
|
196
|
+
};
|
|
197
|
+
collectionStore.set(id, {
|
|
198
|
+
data,
|
|
199
|
+
settleAt: Date.now() + (options.delay ?? 0),
|
|
200
|
+
/*
|
|
201
|
+
* A simulated rejection becomes an expiry here: there is no way to
|
|
202
|
+
* say no to a question, only to leave it unanswered, and that is the
|
|
203
|
+
* ending a caller most needs to have thought about.
|
|
204
|
+
*/
|
|
205
|
+
expire: outcome === "rejected",
|
|
206
|
+
timeout: outcome === "timeout",
|
|
207
|
+
});
|
|
208
|
+
if (options.log !== false)
|
|
209
|
+
announceCollection(data, outcome);
|
|
210
|
+
return json(data, 201);
|
|
211
|
+
}
|
|
212
|
+
const collectionMatch = /^\/collections\/([^/]+)(\/cancel)?$/.exec(path);
|
|
213
|
+
if (collectionMatch) {
|
|
214
|
+
const stored = collectionStore.get(collectionMatch[1]);
|
|
215
|
+
if (!stored) {
|
|
216
|
+
return json({ error: { code: "not_found", message: "No such request in this development session." } }, 404);
|
|
217
|
+
}
|
|
218
|
+
if (collectionMatch[2] === "/cancel") {
|
|
219
|
+
stored.data.status = "cancelled";
|
|
220
|
+
return json(stored.data);
|
|
221
|
+
}
|
|
222
|
+
settleCollection(stored);
|
|
223
|
+
return json(stored.data);
|
|
224
|
+
}
|
|
225
|
+
if (method === "GET" && path === "/collections") {
|
|
226
|
+
const externalId = url.searchParams.get("externalId");
|
|
227
|
+
const all = [...collectionStore.values()].map((c) => c.data);
|
|
228
|
+
return json({
|
|
229
|
+
collections: externalId ? all.filter((c) => c.externalId === externalId) : all,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/*
|
|
233
|
+
* Flows in development are real objects with no simulation attached:
|
|
234
|
+
* there is nobody to imagine, and the thing worth exercising offline is
|
|
235
|
+
* that `flow.approve()` reaches the same code as `deliverd.approve()`.
|
|
236
|
+
* Members are not tracked here — the server derives them from rows this
|
|
237
|
+
* store does not keep — so a development flow reports what it knows and
|
|
238
|
+
* does not invent a membership.
|
|
239
|
+
*/
|
|
240
|
+
if (method === "POST" && path === "/flows") {
|
|
241
|
+
const id = `flw_dev_${++counter}`;
|
|
242
|
+
const data = {
|
|
243
|
+
id,
|
|
244
|
+
status: "open",
|
|
245
|
+
url: `https://deliverd.dev/flows/${id}`,
|
|
246
|
+
title: String(body.title ?? "Untitled"),
|
|
247
|
+
description: body.description ?? null,
|
|
248
|
+
externalId: body.externalId ?? null,
|
|
249
|
+
metadata: body.metadata ?? {},
|
|
250
|
+
startedBy: "You (development mode)",
|
|
251
|
+
members: [],
|
|
252
|
+
memberCount: 0,
|
|
253
|
+
openCount: 0,
|
|
254
|
+
allSettled: false,
|
|
255
|
+
timeline: [],
|
|
256
|
+
settledAt: null,
|
|
257
|
+
createdAt: new Date().toISOString(),
|
|
258
|
+
};
|
|
259
|
+
flowStore.set(id, data);
|
|
260
|
+
if (options.log !== false) {
|
|
261
|
+
console.log(`\n \u25B8 Flow started: ${data.title}\n ${data.url}\n`);
|
|
262
|
+
}
|
|
263
|
+
return json(data, 201);
|
|
264
|
+
}
|
|
265
|
+
const flowMatch = /^\/flows\/([^/]+)(\/complete|\/cancel)?$/.exec(path);
|
|
266
|
+
if (flowMatch) {
|
|
267
|
+
const flow = flowStore.get(flowMatch[1]);
|
|
268
|
+
if (!flow) {
|
|
269
|
+
return json({ error: { code: "not_found", message: "No such flow in this development session." } }, 404);
|
|
270
|
+
}
|
|
271
|
+
if (flowMatch[2] === "/complete" || flowMatch[2] === "/cancel") {
|
|
272
|
+
if (flow.status !== "open") {
|
|
273
|
+
return json({ error: { code: "not_open", message: `This flow is already ${flow.status}.` } }, 409);
|
|
274
|
+
}
|
|
275
|
+
flow.status = flowMatch[2] === "/complete" ? "complete" : "cancelled";
|
|
276
|
+
flow.settledAt = new Date().toISOString();
|
|
277
|
+
}
|
|
278
|
+
return json(flow);
|
|
279
|
+
}
|
|
280
|
+
if (method === "GET" && path === "/flows") {
|
|
281
|
+
const externalId = url.searchParams.get("externalId");
|
|
282
|
+
const all = [...flowStore.values()];
|
|
283
|
+
return json({ flows: externalId ? all.filter((f) => f.externalId === externalId) : all });
|
|
284
|
+
}
|
|
158
285
|
return json({
|
|
159
286
|
error: {
|
|
160
287
|
code: "not_in_development_mode",
|
|
161
|
-
message: `Development mode simulates approvals and
|
|
288
|
+
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.`,
|
|
162
289
|
},
|
|
163
290
|
}, 501);
|
|
164
291
|
});
|
|
@@ -242,3 +369,69 @@ function announceReview(data, verdict, willHang) {
|
|
|
242
369
|
];
|
|
243
370
|
console.log(lines.join("\n"));
|
|
244
371
|
}
|
|
372
|
+
/**
|
|
373
|
+
* Everyone answers, at the same moment, with a plausible value for the kind
|
|
374
|
+
* of question asked. Plausible rather than empty, because a caller writing
|
|
375
|
+
* `answers["Headcount at 30 September"] + 1` should find out here that it is
|
|
376
|
+
* a number, not in production.
|
|
377
|
+
*/
|
|
378
|
+
function settleCollection(stored) {
|
|
379
|
+
if (stored.timeout || stored.data.status !== "pending")
|
|
380
|
+
return;
|
|
381
|
+
if (Date.now() < stored.settleAt)
|
|
382
|
+
return;
|
|
383
|
+
if (stored.expire) {
|
|
384
|
+
stored.data.status = "expired";
|
|
385
|
+
stored.data.settledAt = new Date().toISOString();
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const at = new Date().toISOString();
|
|
389
|
+
stored.data.responses = stored.data.responses.map((r) => ({
|
|
390
|
+
...r,
|
|
391
|
+
status: "submitted",
|
|
392
|
+
submittedAt: at,
|
|
393
|
+
answers: stored.data.questions.map((q) => ({
|
|
394
|
+
question: q.prompt,
|
|
395
|
+
kind: q.kind,
|
|
396
|
+
value: sampleAnswer(q),
|
|
397
|
+
})),
|
|
398
|
+
}));
|
|
399
|
+
stored.data.submittedCount = stored.data.responses.length;
|
|
400
|
+
stored.data.status = "complete";
|
|
401
|
+
stored.data.settledAt = at;
|
|
402
|
+
}
|
|
403
|
+
function sampleAnswer(q) {
|
|
404
|
+
switch (q.kind) {
|
|
405
|
+
case "number":
|
|
406
|
+
return 42;
|
|
407
|
+
case "boolean":
|
|
408
|
+
return true;
|
|
409
|
+
case "date":
|
|
410
|
+
return new Date().toISOString().slice(0, 10);
|
|
411
|
+
case "choice":
|
|
412
|
+
return q.options[0] ?? "";
|
|
413
|
+
case "multi_choice":
|
|
414
|
+
return q.options.slice(0, 1);
|
|
415
|
+
case "long_text":
|
|
416
|
+
return "Simulated: a longer answer than a line.";
|
|
417
|
+
default:
|
|
418
|
+
return "Simulated answer";
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
function announceCollection(data, outcome) {
|
|
422
|
+
const lines = [
|
|
423
|
+
` ┌─ Deliverd · development mode ─────────────────────────`,
|
|
424
|
+
` │ ${data.title}`,
|
|
425
|
+
` │ Asking: ${data.questions.map((q) => q.prompt).join(" · ") || "nothing"}`,
|
|
426
|
+
` │ Of: ${data.responses.map((r) => r.respondent).join(", ") || "nobody named"}`,
|
|
427
|
+
` │`,
|
|
428
|
+
` │ Simulating: ${outcome === "timeout"
|
|
429
|
+
? "nobody answers"
|
|
430
|
+
: outcome === "rejected"
|
|
431
|
+
? "it expires unanswered"
|
|
432
|
+
: "everyone answers"}`,
|
|
433
|
+
` └───────────────────────────────────────────────────────`,
|
|
434
|
+
"",
|
|
435
|
+
];
|
|
436
|
+
console.log(lines.join("\n"));
|
|
437
|
+
}
|