@blocklive/cli 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -0
- package/README.md +202 -2
- package/dist/browser.js +194 -0
- package/dist/catalog.js +1016 -0
- package/dist/change.js +181 -0
- package/dist/cli.js +1176 -0
- package/dist/config.js +105 -0
- package/dist/csv.js +76 -0
- package/dist/errors.js +75 -0
- package/dist/http.js +189 -0
- package/dist/idempotency.js +39 -0
- package/dist/image-file.js +95 -0
- package/dist/lock.js +186 -0
- package/dist/login.js +203 -0
- package/dist/main.js +49 -0
- package/dist/read.js +155 -0
- package/dist/reads/developer-request.js +31 -0
- package/dist/reads/developers.js +53 -0
- package/dist/reads/domains.js +84 -0
- package/dist/reads/events.js +956 -0
- package/dist/reads/index.js +47 -0
- package/dist/reads/invoices.js +177 -0
- package/dist/reads/payment-account.js +177 -0
- package/dist/reads/places.js +55 -0
- package/dist/reads/promo-import.js +183 -0
- package/dist/reads/promos.js +62 -0
- package/dist/reads/requests.js +119 -0
- package/dist/reads/tickets.js +386 -0
- package/dist/safe.js +88 -0
- package/dist/secrets.js +267 -0
- package/dist/stdin.js +72 -0
- package/dist/style.js +44 -0
- package/dist/unattended.js +311 -0
- package/package.json +30 -5
package/dist/change.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.either = exports.time = exports.change = exports.ALREADY = exports.answered = exports.send = exports.sendAgain = exports.confirm = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
const idempotency_1 = require("./idempotency");
|
|
6
|
+
const safe_1 = require("./safe");
|
|
7
|
+
// What every `change` command does the same way (tickets.ts, events.ts): try
|
|
8
|
+
// the change first on Blocklive (a dry run) and show the plan; then ask at a
|
|
9
|
+
// terminal, or --yes; then send it with one idempotency key a run (or
|
|
10
|
+
// --idempotency-key's). Its answer can be lost after Blocklive made it (no
|
|
11
|
+
// answer, a failure on its side, something that isn't its answer): then it
|
|
12
|
+
// is sent again with the same key, which Blocklive answers as before, never
|
|
13
|
+
// making it twice; and if no answer comes, the command says how to send it
|
|
14
|
+
// again safely. A run with --idempotency-key reaches Blocklive's answer for
|
|
15
|
+
// that key even when trying the change first is refused now (its budget
|
|
16
|
+
// spent since, its start passed) or nothing would change.
|
|
17
|
+
/** Asked at a terminal; with --yes it goes ahead. With nothing to ask on (a
|
|
18
|
+
* script, a pipe) it stops, the plan shown, unless --yes. `doing` is what
|
|
19
|
+
* --yes would do, when the question in lower case doesn't say it (a name). */
|
|
20
|
+
async function confirm(read, question, doing) {
|
|
21
|
+
if (read.values.yes)
|
|
22
|
+
return true;
|
|
23
|
+
if (!read.io.ask)
|
|
24
|
+
throw (0, errors_1.usage)(doing
|
|
25
|
+
? `Nothing was changed. Add --yes to ${doing} without asking.`
|
|
26
|
+
: `Nothing was changed. Add --yes to ${question
|
|
27
|
+
.toLowerCase()
|
|
28
|
+
.replace(/\?$/, '')} without asking.`);
|
|
29
|
+
const answer = (await read.io.ask(`${question} [y/N] `))
|
|
30
|
+
.trim()
|
|
31
|
+
.toLowerCase();
|
|
32
|
+
return answer === 'y' || answer === 'yes';
|
|
33
|
+
}
|
|
34
|
+
exports.confirm = confirm;
|
|
35
|
+
/** How a lost answer is told, and how to send the change again. */
|
|
36
|
+
const sendAgain = (key) => `Run the same command again with --idempotency-key ${key}: Blocklive makes it once, or answers that it was made.`;
|
|
37
|
+
exports.sendAgain = sendAgain;
|
|
38
|
+
/** Sends the change with its key. Without an answer (none, Blocklive's own
|
|
39
|
+
* failure, or one that can't be read) it may have been made all the same:
|
|
40
|
+
* twice more with the same key, then it stops and says how to send it again
|
|
41
|
+
* (the key in the message, and as idempotencyKey in --json). */
|
|
42
|
+
async function send(read, path, body, key, by) {
|
|
43
|
+
for (let attempt = 1;; attempt += 1) {
|
|
44
|
+
try {
|
|
45
|
+
return by
|
|
46
|
+
? await by(key)
|
|
47
|
+
: await read.client.post(path, body, key);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (!(error instanceof errors_1.NoAnswer))
|
|
51
|
+
throw error;
|
|
52
|
+
if (attempt < 3) {
|
|
53
|
+
read.io.err(`No answer from Blocklive. Trying again with the same idempotency key (${key})…`);
|
|
54
|
+
await read.io.wait(2000);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
throw new errors_1.CliError(`No answer from Blocklive, so it isn't known whether the change was made. ${(0, exports.sendAgain)(key)}`, errors_1.EXIT.error, error.code === 'unreachable' ? 'unreachable' : 'unavailable', error.requestId, { idempotencyKey: key });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.send = send;
|
|
62
|
+
// Refusals that may change by the time a change is sent again (a budget, a
|
|
63
|
+
// rule about now, a role, the event), so a key may already have an answer
|
|
64
|
+
// for them: Blocklive answers a key before it checks the change again.
|
|
65
|
+
const MAY_HAVE_BEEN_MADE = new Set([
|
|
66
|
+
'refused',
|
|
67
|
+
'forbidden',
|
|
68
|
+
'rate_limited',
|
|
69
|
+
'not_found',
|
|
70
|
+
]);
|
|
71
|
+
const mayHaveBeenMade = (error) => error instanceof errors_1.CliError &&
|
|
72
|
+
!(error instanceof errors_1.NoAnswer) &&
|
|
73
|
+
!!error.code &&
|
|
74
|
+
MAY_HAVE_BEEN_MADE.has(error.code);
|
|
75
|
+
/** A change's answer as --json prints it: the key it was sent with, and what
|
|
76
|
+
* refused trying it first when that didn't stop it. */
|
|
77
|
+
const answered = (answer, key, tried) => JSON.stringify({
|
|
78
|
+
...answer,
|
|
79
|
+
idempotencyKey: key,
|
|
80
|
+
...(tried
|
|
81
|
+
? { triedFirst: { refused: tried.code, message: tried.message } }
|
|
82
|
+
: {}),
|
|
83
|
+
});
|
|
84
|
+
exports.answered = answered;
|
|
85
|
+
/** Words both change commands say alike. */
|
|
86
|
+
exports.ALREADY = 'Already like that; sent again with its key.';
|
|
87
|
+
/** Tries the change, shows it, asks (or --yes), sends it once with its key. */
|
|
88
|
+
async function change(read, flow) {
|
|
89
|
+
const out = read.io.out;
|
|
90
|
+
const given = read.values['idempotency-key'] !== undefined;
|
|
91
|
+
const key = (0, idempotency_1.idempotencyKey)(read.values['idempotency-key']);
|
|
92
|
+
const planOnly = !!read.values['dry-run'] || (read.json && !read.values.yes);
|
|
93
|
+
// Every check, nothing written: the plan, read as sent and cleaned to
|
|
94
|
+
// be shown (with --json it stays as sent).
|
|
95
|
+
let sent;
|
|
96
|
+
try {
|
|
97
|
+
sent = flow.send
|
|
98
|
+
? await flow.send(undefined, { raw: true })
|
|
99
|
+
: await read.client.post(flow.path, { ...flow.body, dryRun: true }, undefined, { raw: true });
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
// A run with a key may be a change that key made already: only the
|
|
103
|
+
// change itself, sent with it, gets Blocklive's answer for the key.
|
|
104
|
+
if (!given || planOnly || !mayHaveBeenMade(error))
|
|
105
|
+
throw error;
|
|
106
|
+
if (!read.json) {
|
|
107
|
+
out(`Trying it first was refused: ${error.message}`);
|
|
108
|
+
out(`It is sent with its idempotency key (${key}) all the same: if that key made it before, Blocklive answers what it did; if not, it refuses it again.`);
|
|
109
|
+
}
|
|
110
|
+
if (!(await confirm(read, 'Send it with its key?'))) {
|
|
111
|
+
out('Nothing was changed.');
|
|
112
|
+
return errors_1.EXIT.ok;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
flow.done(await send(read, flow.path, flow.body, key, flow.send), key, error);
|
|
116
|
+
}
|
|
117
|
+
catch (again) {
|
|
118
|
+
if (!mayHaveBeenMade(again))
|
|
119
|
+
throw again;
|
|
120
|
+
throw new errors_1.CliError(`This key didn't make it before (or not in the last 24 hours), and Blocklive refuses it now: ${again.message}`, again.exitCode, again.code, again.requestId, { idempotencyKey: key });
|
|
121
|
+
}
|
|
122
|
+
return errors_1.EXIT.ok;
|
|
123
|
+
}
|
|
124
|
+
const plan = read.json ? sent : (0, safe_1.cleanData)(sent);
|
|
125
|
+
const unchanged = flow.unchanged?.(sent) ?? false;
|
|
126
|
+
if (unchanged && (!given || planOnly)) {
|
|
127
|
+
if (read.json)
|
|
128
|
+
out(JSON.stringify({ plan, applied: false, idempotencyKey: key }));
|
|
129
|
+
else
|
|
130
|
+
out(flow.nothing?.(plan) ?? 'Nothing to change.');
|
|
131
|
+
if (!read.json && read.values['dry-run'])
|
|
132
|
+
out('Dry run: nothing was changed.');
|
|
133
|
+
return errors_1.EXIT.ok;
|
|
134
|
+
}
|
|
135
|
+
if (planOnly) {
|
|
136
|
+
if (read.json)
|
|
137
|
+
out(JSON.stringify({ plan, applied: false, idempotencyKey: key }));
|
|
138
|
+
else {
|
|
139
|
+
flow.show(plan);
|
|
140
|
+
out('Dry run: nothing was changed.');
|
|
141
|
+
}
|
|
142
|
+
return errors_1.EXIT.ok;
|
|
143
|
+
}
|
|
144
|
+
if (unchanged) {
|
|
145
|
+
// Nothing would change: with its key it is sent again, to get what
|
|
146
|
+
// the key did (a change it made already is answered as before).
|
|
147
|
+
if (!read.json)
|
|
148
|
+
out(exports.ALREADY);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
if (!read.json)
|
|
152
|
+
flow.show(plan);
|
|
153
|
+
if (!(await confirm(read, flow.question))) {
|
|
154
|
+
out('Nothing was changed.');
|
|
155
|
+
return errors_1.EXIT.ok;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
flow.done(await send(read, flow.path, flow.body, key, flow.send), key);
|
|
159
|
+
return errors_1.EXIT.ok;
|
|
160
|
+
}
|
|
161
|
+
exports.change = change;
|
|
162
|
+
const OFFSET = /(?:Z|[+-]\d{2}:\d{2})$/;
|
|
163
|
+
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
164
|
+
/** A time as Blocklive takes it: ISO 8601, with its offset. */
|
|
165
|
+
function time(value, name) {
|
|
166
|
+
if (value === undefined)
|
|
167
|
+
return undefined;
|
|
168
|
+
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?$/.test(value))
|
|
169
|
+
throw (0, errors_1.usage)(`${name} needs its offset, so it means one time everywhere: ${value}+01:00, or ${value}Z for UTC.`);
|
|
170
|
+
if (!ISO.test(value) || !OFFSET.test(value))
|
|
171
|
+
throw (0, errors_1.usage)(`${name} is a date and time with its offset, like 2026-12-01T19:00:00+01:00.`);
|
|
172
|
+
return value;
|
|
173
|
+
}
|
|
174
|
+
exports.time = time;
|
|
175
|
+
/** One of two switches that say opposite things, or neither. */
|
|
176
|
+
function either(read, yes, no) {
|
|
177
|
+
if (read.values[yes] && read.values[no])
|
|
178
|
+
throw (0, errors_1.usage)(`Use --${String(yes)} or --${String(no)}, not both.`);
|
|
179
|
+
return read.values[yes] ? true : read.values[no] ? false : undefined;
|
|
180
|
+
}
|
|
181
|
+
exports.either = either;
|