@firstdraft.com/cli 0.9.0 → 0.11.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 +10 -8
- package/docs/README.md +1 -1
- package/docs/commands.md +659 -132
- package/docs/errors.md +227 -73
- package/package.json +7 -1
- package/src/agent-sentences.js +210 -0
- package/src/api-authentication.js +25 -6
- package/src/api-response.js +110 -6
- package/src/cli.js +3143 -912
- package/src/commands/account.js +97 -0
- package/src/commands/compilation.js +57 -5
- package/src/commands/login.js +246 -42
- package/src/commands/maintain.js +662 -0
- package/src/commands/note.js +204 -0
- package/src/commands/plan-compile.js +44 -77
- package/src/commands/plan-init.js +14 -1
- package/src/commands/plan-push.js +26 -12
- package/src/commands/plan-status.js +1 -1
- package/src/commands/report.js +469 -0
- package/src/commands/sketches.js +388 -0
- package/src/commands/start.js +52 -0
- package/src/compilation-artifact.js +39 -4
- package/src/credentials.js +145 -7
- package/src/oauth.js +33 -25
- package/src/plan-compile-progress.js +8 -81
- package/src/plan-state.js +325 -5
- package/src/root-output.js +13 -1
- package/src/service-client.js +513 -0
- package/src/sketch-capture.js +449 -0
- package/src/commands/plan-publish.js +0 -1146
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FirstDraftNetworkError,
|
|
3
|
+
FirstDraftProtocolError,
|
|
4
|
+
isProblemBody,
|
|
5
|
+
problemProjection,
|
|
6
|
+
readResponseBody,
|
|
7
|
+
responseMediaType,
|
|
8
|
+
sendRequest,
|
|
9
|
+
serviceEndpoint,
|
|
10
|
+
} from "../api-response.js";
|
|
11
|
+
|
|
12
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
13
|
+
|
|
14
|
+
export const PROGRESS_STEPS = Object.freeze([
|
|
15
|
+
"setup",
|
|
16
|
+
"specs_first_run",
|
|
17
|
+
"behavior_written",
|
|
18
|
+
"specs_passing",
|
|
19
|
+
"preview_started",
|
|
20
|
+
]);
|
|
21
|
+
export const FAILURE_KINDS = Object.freeze([
|
|
22
|
+
"spec_failure",
|
|
23
|
+
"setup_failure",
|
|
24
|
+
"boot_error",
|
|
25
|
+
"lint_failure",
|
|
26
|
+
// An error in a deployed copy of the application, from a `firstdraft
|
|
27
|
+
// maintain` item: it names the item and carries no message.
|
|
28
|
+
"deployed_error",
|
|
29
|
+
]);
|
|
30
|
+
const MAX_FAILURE_FRAMES = 20;
|
|
31
|
+
|
|
32
|
+
// A failure report carries only fields of a known shape. Free text keeps
|
|
33
|
+
// plain words, and anything that does not fit its shape is left out, so the
|
|
34
|
+
// rules say what may leave the computer rather than guessing what a secret
|
|
35
|
+
// looks like. The Service refuses the same shapes.
|
|
36
|
+
const MAX_MESSAGE = 300;
|
|
37
|
+
const MAX_FIX_SUMMARY = 200;
|
|
38
|
+
const MAX_ERROR_CLASS = 200;
|
|
39
|
+
const MAX_RERUN = 300;
|
|
40
|
+
const MAX_FRAME = 300;
|
|
41
|
+
const MAX_VERSION = 32;
|
|
42
|
+
// Components of safe characters, each separated by one slash.
|
|
43
|
+
const SAFE_PATH = String.raw`[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*`;
|
|
44
|
+
// bundle exec rspec ./spec/models/post_spec.rb:42, bin/rspec spec/x_spec.rb
|
|
45
|
+
const RERUN = new RegExp(
|
|
46
|
+
String.raw`^(?:bundle exec |bin/)?rspec(?: (?:\./)?spec/${SAFE_PATH}(?::\d{1,7})*)+$`,
|
|
47
|
+
);
|
|
48
|
+
const ERROR_CLASS = /^(?:::)?[A-Z][A-Za-z0-9_]*(?:::[A-Z][A-Za-z0-9_]*)*$/;
|
|
49
|
+
const FRAME = new RegExp(
|
|
50
|
+
String.raw`^(?:\./)?((?:app|lib|config|db|spec)/${SAFE_PATH}):(\d{1,7})(?::in .*)?$`,
|
|
51
|
+
);
|
|
52
|
+
// Checked in every argument and frame: spec/.. spec/models has one too.
|
|
53
|
+
export const PARENT_SEGMENT = /(?:^|[/\s:])\.\.(?=[/\s:]|$)/;
|
|
54
|
+
// 4.0.6, 8.1.4.1, 8.2.0.beta1, 3.4.0-preview2
|
|
55
|
+
const VERSION_NUMBER =
|
|
56
|
+
/^\d{1,4}(?:\.\d{1,4}){0,3}(?:[.-](?:alpha|beta|rc|pre|preview)\d{0,3})?$/;
|
|
57
|
+
const ASSIGNMENT_WORD = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
58
|
+
// eslint-disable-next-line no-control-regex
|
|
59
|
+
const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g;
|
|
60
|
+
// Quotes are not paired: from the word holding the first one, the rest of the
|
|
61
|
+
// line is one placeholder, so no quoted value survives.
|
|
62
|
+
const QUOTE = /['"`]/;
|
|
63
|
+
// A word that can carry a value: an assignment, a path or URL, an address,
|
|
64
|
+
// or anything long enough to be a token.
|
|
65
|
+
const VALUE_WORD = /[=/\\@]/;
|
|
66
|
+
const MAX_PLAIN_WORD = 24;
|
|
67
|
+
const PLACEHOLDER = "[…]";
|
|
68
|
+
|
|
69
|
+
export class ReportRejectedError extends Error {
|
|
70
|
+
/**
|
|
71
|
+
* @param {number} status
|
|
72
|
+
* @param {Record<string, unknown>} response
|
|
73
|
+
*/
|
|
74
|
+
constructor(status, response) {
|
|
75
|
+
super("First Draft rejected the report.");
|
|
76
|
+
this.status = status;
|
|
77
|
+
this.response = response;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class ReportUnavailableError extends Error {
|
|
82
|
+
/**
|
|
83
|
+
* @param {number | undefined} status
|
|
84
|
+
* @param {Record<string, unknown> | null} [response]
|
|
85
|
+
*/
|
|
86
|
+
constructor(status, response = null) {
|
|
87
|
+
super("The report's outcome could not be confirmed.");
|
|
88
|
+
this.status = status;
|
|
89
|
+
this.response = response;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @typedef {object} ProgressCounts
|
|
95
|
+
* @property {number} [examples]
|
|
96
|
+
* @property {number} [failures]
|
|
97
|
+
* @property {number} [behavior_to_write_failures]
|
|
98
|
+
* @property {number} [items_remaining]
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {string} step one of PROGRESS_STEPS
|
|
103
|
+
* @param {ProgressCounts} counts only the counts that were given
|
|
104
|
+
*/
|
|
105
|
+
export function progressReportBody(step, counts) {
|
|
106
|
+
return {
|
|
107
|
+
progress_report: {
|
|
108
|
+
step,
|
|
109
|
+
...(Object.keys(counts).length === 0 ? {} : { counts }),
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @typedef {object} FailureReport
|
|
116
|
+
* @property {string} kind one of FAILURE_KINDS
|
|
117
|
+
* @property {string} [maintenanceItemId] a deployed_error's item, checked by the caller
|
|
118
|
+
* @property {string} [rerun]
|
|
119
|
+
* @property {string} [errorClass]
|
|
120
|
+
* @property {string} [message]
|
|
121
|
+
* @property {readonly string[]} [frames]
|
|
122
|
+
* @property {string} [fixSummary]
|
|
123
|
+
* @property {string} [rubyVersion]
|
|
124
|
+
* @property {string} [railsVersion]
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Shapes the failure report that is printed and sent. Each field keeps only
|
|
129
|
+
* what fits its shape; `omitted` says, for the preview, what was left out.
|
|
130
|
+
*
|
|
131
|
+
* @param {FailureReport} report
|
|
132
|
+
* @param {string} cwd an absolute frame inside it becomes relative
|
|
133
|
+
*/
|
|
134
|
+
export function failureReportBody(
|
|
135
|
+
{
|
|
136
|
+
kind,
|
|
137
|
+
maintenanceItemId,
|
|
138
|
+
rerun,
|
|
139
|
+
errorClass,
|
|
140
|
+
message,
|
|
141
|
+
frames = [],
|
|
142
|
+
fixSummary,
|
|
143
|
+
rubyVersion,
|
|
144
|
+
railsVersion,
|
|
145
|
+
},
|
|
146
|
+
cwd,
|
|
147
|
+
) {
|
|
148
|
+
/** @type {string[]} */
|
|
149
|
+
const omitted = [];
|
|
150
|
+
const shapedRerun = rerun === undefined ? undefined : rspecRun(rerun);
|
|
151
|
+
if (rerun !== undefined && shapedRerun === undefined) {
|
|
152
|
+
omitted.push("rerun, which is not an RSpec run of the application's specs");
|
|
153
|
+
}
|
|
154
|
+
const shapedErrorClass =
|
|
155
|
+
errorClass !== undefined &&
|
|
156
|
+
errorClass.length <= MAX_ERROR_CLASS &&
|
|
157
|
+
ERROR_CLASS.test(errorClass)
|
|
158
|
+
? errorClass
|
|
159
|
+
: undefined;
|
|
160
|
+
if (errorClass !== undefined && shapedErrorClass === undefined) {
|
|
161
|
+
omitted.push("error_class, which is not a Ruby constant");
|
|
162
|
+
}
|
|
163
|
+
const shapedFrames = frames
|
|
164
|
+
.map((frame) => appFrame(frame, cwd))
|
|
165
|
+
.filter((frame) => frame !== undefined);
|
|
166
|
+
if (shapedFrames.length < frames.length) {
|
|
167
|
+
const dropped = frames.length - shapedFrames.length;
|
|
168
|
+
omitted.push(
|
|
169
|
+
`${dropped} ${dropped === 1 ? "frame" : "frames"} not in the application's files with a line number`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
if (shapedFrames.length > MAX_FAILURE_FRAMES) {
|
|
173
|
+
omitted.push(`frames after the first ${MAX_FAILURE_FRAMES}`);
|
|
174
|
+
}
|
|
175
|
+
/** @type {Record<string, string>} */
|
|
176
|
+
const versions = {};
|
|
177
|
+
for (const [name, value] of /** @type {const} */ ([
|
|
178
|
+
["ruby", rubyVersion],
|
|
179
|
+
["rails", railsVersion],
|
|
180
|
+
])) {
|
|
181
|
+
if (value === undefined) continue;
|
|
182
|
+
if (value.length <= MAX_VERSION && VERSION_NUMBER.test(value)) {
|
|
183
|
+
versions[name] = value;
|
|
184
|
+
} else {
|
|
185
|
+
omitted.push(`the ${name} version, which is not a version number`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const shapedMessage =
|
|
189
|
+
message === undefined ? undefined : plainWords(message, MAX_MESSAGE);
|
|
190
|
+
if (message !== undefined && !shapedMessage) {
|
|
191
|
+
omitted.push("message, which has no plain words on its first line");
|
|
192
|
+
}
|
|
193
|
+
const shapedFixSummary =
|
|
194
|
+
fixSummary === undefined
|
|
195
|
+
? undefined
|
|
196
|
+
: plainWords(fixSummary, MAX_FIX_SUMMARY);
|
|
197
|
+
if (fixSummary !== undefined && !shapedFixSummary) {
|
|
198
|
+
omitted.push("fix_summary, which has no plain words on its first line");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
body: {
|
|
203
|
+
failure_report: {
|
|
204
|
+
kind,
|
|
205
|
+
...(maintenanceItemId === undefined
|
|
206
|
+
? {}
|
|
207
|
+
: { maintenance_item_id: maintenanceItemId }),
|
|
208
|
+
...(shapedRerun === undefined ? {} : { rerun: shapedRerun }),
|
|
209
|
+
...(shapedErrorClass === undefined
|
|
210
|
+
? {}
|
|
211
|
+
: { error_class: shapedErrorClass }),
|
|
212
|
+
...(shapedMessage ? { message: shapedMessage } : {}),
|
|
213
|
+
...(shapedFrames.length === 0
|
|
214
|
+
? {}
|
|
215
|
+
: { frames: shapedFrames.slice(0, MAX_FAILURE_FRAMES) }),
|
|
216
|
+
...(shapedFixSummary ? { fix_summary: shapedFixSummary } : {}),
|
|
217
|
+
...(Object.keys(versions).length === 0 ? {} : { versions }),
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
omitted,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The command without its leading environment assignments, when what is left
|
|
226
|
+
* is an RSpec run of the application's specs.
|
|
227
|
+
*
|
|
228
|
+
* @param {string} command
|
|
229
|
+
* @returns {string | undefined}
|
|
230
|
+
*/
|
|
231
|
+
export function rspecRun(command) {
|
|
232
|
+
const words = shellWords(command);
|
|
233
|
+
if (words === null) return undefined;
|
|
234
|
+
|
|
235
|
+
let start = 0;
|
|
236
|
+
if (
|
|
237
|
+
words[0]?.value === "env" &&
|
|
238
|
+
words[1] !== undefined &&
|
|
239
|
+
ASSIGNMENT_WORD.test(words[1].value)
|
|
240
|
+
) {
|
|
241
|
+
start = 1;
|
|
242
|
+
}
|
|
243
|
+
while (ASSIGNMENT_WORD.test(words[start]?.value ?? "")) {
|
|
244
|
+
start += 1;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// A spec directory may be written with a trailing slash: spec/models/.
|
|
248
|
+
const run = words
|
|
249
|
+
.slice(start)
|
|
250
|
+
.map((word) => word.raw.replace(/^((?:\.\/)?spec\/.*[^/])\/+$/, "$1"))
|
|
251
|
+
.join(" ");
|
|
252
|
+
return run.length <= MAX_RERUN && RERUN.test(run) && !PARENT_SEGMENT.test(run)
|
|
253
|
+
? run
|
|
254
|
+
: undefined;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Splits a command into shell words, keeping each word's text as written
|
|
259
|
+
* and its value with quotes and escapes removed. Returns null for an
|
|
260
|
+
* unterminated quote.
|
|
261
|
+
*
|
|
262
|
+
* @param {string} command
|
|
263
|
+
* @returns {{raw: string, value: string}[] | null}
|
|
264
|
+
*/
|
|
265
|
+
function shellWords(command) {
|
|
266
|
+
/** @type {{raw: string, value: string}[]} */
|
|
267
|
+
const words = [];
|
|
268
|
+
let raw = "";
|
|
269
|
+
let value = "";
|
|
270
|
+
let inWord = false;
|
|
271
|
+
let index = 0;
|
|
272
|
+
while (index < command.length) {
|
|
273
|
+
const character = /** @type {string} */ (command[index]);
|
|
274
|
+
if (/\s/.test(character)) {
|
|
275
|
+
if (inWord) words.push({ raw, value });
|
|
276
|
+
raw = "";
|
|
277
|
+
value = "";
|
|
278
|
+
inWord = false;
|
|
279
|
+
index += 1;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
inWord = true;
|
|
284
|
+
if (character === "'" || character === '"') {
|
|
285
|
+
const close = command.indexOf(character, index + 1);
|
|
286
|
+
if (close === -1) return null;
|
|
287
|
+
raw += command.slice(index, close + 1);
|
|
288
|
+
value += command.slice(index + 1, close);
|
|
289
|
+
index = close + 1;
|
|
290
|
+
} else if (character === "\\" && index + 1 < command.length) {
|
|
291
|
+
raw += command.slice(index, index + 2);
|
|
292
|
+
value += command[index + 1];
|
|
293
|
+
index += 2;
|
|
294
|
+
} else {
|
|
295
|
+
raw += character;
|
|
296
|
+
value += character;
|
|
297
|
+
index += 1;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (inWord) words.push({ raw, value });
|
|
301
|
+
return words;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* A frame in the application's own files, as `path:line`.
|
|
306
|
+
*
|
|
307
|
+
* @param {string} frame
|
|
308
|
+
* @param {string} cwd
|
|
309
|
+
* @returns {string | undefined}
|
|
310
|
+
*/
|
|
311
|
+
function appFrame(frame, cwd) {
|
|
312
|
+
const root = `${cwd.replace(/[\\/]+$/, "")}/`;
|
|
313
|
+
const relative = frame.startsWith(root) ? frame.slice(root.length) : frame;
|
|
314
|
+
const match = FRAME.exec(
|
|
315
|
+
relative.replace(/^[^:]*/, (file) => file.replace(/\\/g, "/")),
|
|
316
|
+
);
|
|
317
|
+
if (match === null || PARENT_SEGMENT.test(match[1] ?? "")) return undefined;
|
|
318
|
+
|
|
319
|
+
const shaped = `${match[1]}:${match[2]}`;
|
|
320
|
+
return shaped.length <= MAX_FRAME ? shaped : undefined;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The plain words of free text's first line. From the word that holds the
|
|
325
|
+
* first quote character to the end of the line is one placeholder; before
|
|
326
|
+
* it, a word that can carry a value is one too; then words that fit within
|
|
327
|
+
* `limit` are kept.
|
|
328
|
+
*
|
|
329
|
+
* @param {string} text
|
|
330
|
+
* @param {number} limit
|
|
331
|
+
*/
|
|
332
|
+
export function plainWords(text, limit) {
|
|
333
|
+
const [firstLine = ""] = text.split(/\r\n|[\r\n\u0085\u2028\u2029]/, 1);
|
|
334
|
+
const line = firstLine.replace(/\t/g, " ").replace(CONTROL, "");
|
|
335
|
+
const quote = line.search(QUOTE);
|
|
336
|
+
const words = (quote === -1 ? line : line.slice(0, quote))
|
|
337
|
+
.split(/\s+/)
|
|
338
|
+
.filter((word) => word !== "");
|
|
339
|
+
// The word that holds the quote goes with the rest of the line.
|
|
340
|
+
if (quote !== -1 && !/\s/.test(line[quote - 1] ?? " ")) words.pop();
|
|
341
|
+
const shaped = words.map((word) =>
|
|
342
|
+
!VALUE_WORD.test(word) && Array.from(word).length <= MAX_PLAIN_WORD
|
|
343
|
+
? word
|
|
344
|
+
: PLACEHOLDER,
|
|
345
|
+
);
|
|
346
|
+
if (quote !== -1) shaped.push(PLACEHOLDER);
|
|
347
|
+
|
|
348
|
+
/** @type {string[]} */
|
|
349
|
+
const kept = [];
|
|
350
|
+
let length = 0;
|
|
351
|
+
for (const word of shaped) {
|
|
352
|
+
const next = length + (kept.length === 0 ? 0 : 1) + Array.from(word).length;
|
|
353
|
+
if (next > limit) break;
|
|
354
|
+
kept.push(word);
|
|
355
|
+
length = next;
|
|
356
|
+
}
|
|
357
|
+
return kept.join(" ");
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @typedef {object} SendReportOptions
|
|
362
|
+
* @property {string} origin
|
|
363
|
+
* @property {{method: string, path: string}} route
|
|
364
|
+
* @property {Record<string, string>} parameters the route's path parameters
|
|
365
|
+
* @property {string | null} compilationId the Compilation the stored report
|
|
366
|
+
* or note names, which a note may leave null
|
|
367
|
+
* @property {string} body the exact JSON text that was printed
|
|
368
|
+
* @property {string} wrapper the key, such as progress_report or note, under
|
|
369
|
+
* which First Draft acknowledges what it stored with its id and Compilation
|
|
370
|
+
* @property {typeof globalThis.fetch} [fetchFunction]
|
|
371
|
+
* @property {(timeoutMs: number) => AbortSignal} [createRequestSignal]
|
|
372
|
+
*/
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Sends one report or note once. Neither is retried: First Draft may already
|
|
376
|
+
* hold it when the outcome is unconfirmed.
|
|
377
|
+
*
|
|
378
|
+
* @param {SendReportOptions} options
|
|
379
|
+
* @returns {Promise<{outcome: "sent" | "reports_off"}>}
|
|
380
|
+
*/
|
|
381
|
+
export async function sendReport({
|
|
382
|
+
origin,
|
|
383
|
+
route,
|
|
384
|
+
parameters,
|
|
385
|
+
compilationId,
|
|
386
|
+
body,
|
|
387
|
+
wrapper,
|
|
388
|
+
fetchFunction = globalThis.fetch,
|
|
389
|
+
createRequestSignal = (timeoutMs) => AbortSignal.timeout(timeoutMs),
|
|
390
|
+
}) {
|
|
391
|
+
const endpoint = serviceEndpoint(route, origin, parameters);
|
|
392
|
+
let response;
|
|
393
|
+
try {
|
|
394
|
+
response = await sendRequest(fetchFunction, endpoint, {
|
|
395
|
+
headers: {
|
|
396
|
+
Accept: "application/json, application/problem+json",
|
|
397
|
+
"Content-Type": "application/json",
|
|
398
|
+
},
|
|
399
|
+
body,
|
|
400
|
+
redirect: "error",
|
|
401
|
+
signal: createRequestSignal(REQUEST_TIMEOUT_MS),
|
|
402
|
+
});
|
|
403
|
+
} catch (error) {
|
|
404
|
+
if (error instanceof FirstDraftNetworkError) {
|
|
405
|
+
throw new ReportUnavailableError(error.status);
|
|
406
|
+
}
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
let responseBody;
|
|
411
|
+
try {
|
|
412
|
+
responseBody = await readResponseBody(response);
|
|
413
|
+
} catch (error) {
|
|
414
|
+
if (
|
|
415
|
+
error instanceof FirstDraftNetworkError ||
|
|
416
|
+
error instanceof FirstDraftProtocolError
|
|
417
|
+
) {
|
|
418
|
+
throw new ReportUnavailableError(response.status);
|
|
419
|
+
}
|
|
420
|
+
throw error;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// First Draft acknowledges a stored report with 201 and the report itself.
|
|
424
|
+
// Any other success, such as a proxy's page, confirms nothing.
|
|
425
|
+
if (response.ok) {
|
|
426
|
+
if (
|
|
427
|
+
response.status === 201 &&
|
|
428
|
+
responseMediaType(response) === "application/json" &&
|
|
429
|
+
isRecord(responseBody) &&
|
|
430
|
+
isRecord(responseBody[wrapper]) &&
|
|
431
|
+
isStoredReport(responseBody[wrapper], compilationId)
|
|
432
|
+
) {
|
|
433
|
+
return { outcome: "sent" };
|
|
434
|
+
}
|
|
435
|
+
throw new ReportUnavailableError(response.status);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const problem = isProblemBody(response, responseBody)
|
|
439
|
+
? problemProjection(responseBody)
|
|
440
|
+
: null;
|
|
441
|
+
if (response.status === 403 && problem?.code === "reports_off") {
|
|
442
|
+
return { outcome: "reports_off" };
|
|
443
|
+
}
|
|
444
|
+
if (problem !== null && response.status !== 408 && response.status < 500) {
|
|
445
|
+
throw new ReportRejectedError(response.status, problem);
|
|
446
|
+
}
|
|
447
|
+
throw new ReportUnavailableError(response.status, problem);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
451
|
+
function isRecord(value) {
|
|
452
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* @param {Record<string, unknown>} report
|
|
457
|
+
* @param {string | null} compilationId
|
|
458
|
+
*/
|
|
459
|
+
function isStoredReport(report, compilationId) {
|
|
460
|
+
return (
|
|
461
|
+
typeof report.id === "string" &&
|
|
462
|
+
STORED_ID.test(report.id) &&
|
|
463
|
+
report.compilation_id === compilationId
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// First Draft stores each report under a UUID.
|
|
468
|
+
const STORED_ID =
|
|
469
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|