@zpdf/sdk 0.0.0-stage → 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/CHANGELOG.md +65 -0
- package/LICENSE +201 -0
- package/README.md +276 -2
- package/dist/cli/commands.d.ts +53 -0
- package/dist/cli/commands.js +1280 -0
- package/dist/cli/config.d.ts +8 -0
- package/dist/cli/config.js +21 -0
- package/dist/cli/errors.d.ts +30 -0
- package/dist/cli/errors.js +67 -0
- package/dist/cli/overrides.d.ts +4 -0
- package/dist/cli/overrides.js +107 -0
- package/dist/cli/parse.d.ts +18 -0
- package/dist/cli/parse.js +72 -0
- package/dist/cli/program.d.ts +15 -0
- package/dist/cli/program.js +323 -0
- package/dist/client.d.ts +263 -0
- package/dist/client.js +474 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.js +5 -0
- package/dist/errors.d.ts +57 -0
- package/dist/errors.js +51 -0
- package/dist/http.d.ts +39 -0
- package/dist/http.js +156 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +6 -0
- package/dist/overrides.d.ts +122 -0
- package/dist/overrides.js +72 -0
- package/dist/pagination.d.ts +20 -0
- package/dist/pagination.js +32 -0
- package/dist/runtime.d.ts +5 -0
- package/dist/runtime.js +31 -0
- package/dist/types.d.ts +574 -0
- package/dist/types.js +9 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/dist/webhooks.d.ts +31 -0
- package/dist/webhooks.js +76 -0
- package/package.json +68 -5
|
@@ -0,0 +1,1280 @@
|
|
|
1
|
+
import { basename, join } from "node:path";
|
|
2
|
+
import { verifyWebhook, ZPDF, } from "../index.js";
|
|
3
|
+
import { readConfig, writeConfig } from "./config.js";
|
|
4
|
+
import { CliError, EXIT, usageError } from "./errors.js";
|
|
5
|
+
import { OVERRIDE_HELP, parseOverride } from "./overrides.js";
|
|
6
|
+
import { keyValues } from "./parse.js";
|
|
7
|
+
// ---- Formatting -------------------------------------------------------------
|
|
8
|
+
function cell(value) {
|
|
9
|
+
if (value === null || value === undefined)
|
|
10
|
+
return "-";
|
|
11
|
+
if (typeof value === "object")
|
|
12
|
+
return JSON.stringify(value);
|
|
13
|
+
return String(value);
|
|
14
|
+
}
|
|
15
|
+
export function table(context, columns, rows) {
|
|
16
|
+
if (rows.length === 0) {
|
|
17
|
+
context.note("Nothing to show.");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const cells = rows.map((row) => columns.map((column) => cell(row[column])));
|
|
21
|
+
const widths = columns.map((column, index) => Math.max(column.length, ...cells.map((row) => row[index].length)));
|
|
22
|
+
const render = (row) => row
|
|
23
|
+
.map((text, index) => text.padEnd(widths[index]))
|
|
24
|
+
.join(" ")
|
|
25
|
+
.trimEnd();
|
|
26
|
+
context.line(render(columns.map((column) => column.toUpperCase())));
|
|
27
|
+
for (const row of cells)
|
|
28
|
+
context.line(render(row));
|
|
29
|
+
}
|
|
30
|
+
/** `name: value` lines for the listed fields that are present. */
|
|
31
|
+
export function fields(context, value, names) {
|
|
32
|
+
const present = names.filter((name) => value[name] !== undefined);
|
|
33
|
+
const width = Math.max(0, ...present.map((name) => name.length));
|
|
34
|
+
for (const name of present)
|
|
35
|
+
context.line(`${`${name}:`.padEnd(width + 2)}${cell(value[name])}`);
|
|
36
|
+
}
|
|
37
|
+
function listOf(columns) {
|
|
38
|
+
return (value, context) => {
|
|
39
|
+
const page = value;
|
|
40
|
+
table(context, columns, page.data);
|
|
41
|
+
if (page.has_more && page.next_cursor !== null) {
|
|
42
|
+
context.note(`More results: add --starting-after ${page.next_cursor}, or --all.`);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function warnings(context, problems) {
|
|
47
|
+
for (const problem of problems ?? []) {
|
|
48
|
+
context.note(`warning: ${problem.code}${problem.param !== undefined ? ` (${problem.param})` : ""}: ${problem.message}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const DOCUMENT_FIELDS = [
|
|
52
|
+
"id",
|
|
53
|
+
"status",
|
|
54
|
+
"template",
|
|
55
|
+
"version",
|
|
56
|
+
"number",
|
|
57
|
+
"pages",
|
|
58
|
+
"bytes",
|
|
59
|
+
"sha256",
|
|
60
|
+
"template_sha256",
|
|
61
|
+
"engine",
|
|
62
|
+
"duration_ms",
|
|
63
|
+
"regenerated_from",
|
|
64
|
+
"reproduced",
|
|
65
|
+
"created",
|
|
66
|
+
"request_id",
|
|
67
|
+
"file_url",
|
|
68
|
+
"download_url",
|
|
69
|
+
];
|
|
70
|
+
function showDocument(value, context) {
|
|
71
|
+
const document = value;
|
|
72
|
+
fields(context, document, DOCUMENT_FIELDS);
|
|
73
|
+
if (document.overrides.length > 0)
|
|
74
|
+
context.line(`overrides: ${document.overrides.map((entry) => entry.op).join(", ")}`);
|
|
75
|
+
warnings(context, document.warnings);
|
|
76
|
+
}
|
|
77
|
+
function showTemplate(value, context) {
|
|
78
|
+
const template = value;
|
|
79
|
+
fields(context, template, [
|
|
80
|
+
"key",
|
|
81
|
+
"id",
|
|
82
|
+
"name",
|
|
83
|
+
"current_version",
|
|
84
|
+
"archived",
|
|
85
|
+
]);
|
|
86
|
+
if (template.version !== undefined)
|
|
87
|
+
versionLines(context, template.version);
|
|
88
|
+
}
|
|
89
|
+
function versionLines(context, version) {
|
|
90
|
+
context.line(`version: ${version.number}${version.note !== null ? ` (${version.note})` : ""}`);
|
|
91
|
+
context.line(`sha256: ${version.sha256}`);
|
|
92
|
+
context.line(`regions: ${version.regions.length > 0 ? version.regions.join(", ") : "-"}`);
|
|
93
|
+
context.line(`fields: ${Object.keys(version.inputs?.fields ?? {}).join(", ") || "-"}`);
|
|
94
|
+
if (version.example !== null && version.example !== undefined) {
|
|
95
|
+
context.line(`example: ${version.example.pages} page${version.example.pages === 1 ? "" : "s"} ${version.example.page_images[0] ?? ""}`);
|
|
96
|
+
}
|
|
97
|
+
warnings(context, version.problems);
|
|
98
|
+
}
|
|
99
|
+
function showVersion(value, context) {
|
|
100
|
+
const version = value;
|
|
101
|
+
fields(context, version, ["template", "id", "current", "created"]);
|
|
102
|
+
versionLines(context, version);
|
|
103
|
+
}
|
|
104
|
+
// ---- Inputs -----------------------------------------------------------------
|
|
105
|
+
function text(context, path) {
|
|
106
|
+
return new TextDecoder().decode(context.io.readFile(path));
|
|
107
|
+
}
|
|
108
|
+
/** JSON from a file, from stdin (`-`) or inline (starting with `{` or `[`). */
|
|
109
|
+
async function jsonInput(context, source, flag) {
|
|
110
|
+
const trimmed = source.trim();
|
|
111
|
+
const raw = trimmed === "-"
|
|
112
|
+
? await context.io.readStdin()
|
|
113
|
+
: trimmed.startsWith("{") || trimmed.startsWith("[")
|
|
114
|
+
? trimmed
|
|
115
|
+
: text(context, source);
|
|
116
|
+
try {
|
|
117
|
+
return JSON.parse(raw);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
throw usageError(`--${flag}: ${trimmed === "-" ? "stdin" : trimmed.startsWith("{") || trimmed.startsWith("[") ? "the value" : source} is not valid JSON.`, `--${flag}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async function overridesFrom(context) {
|
|
124
|
+
const list = [];
|
|
125
|
+
if (typeof context.flags.overrides === "string") {
|
|
126
|
+
const loaded = await jsonInput(context, context.flags.overrides, "overrides");
|
|
127
|
+
if (!Array.isArray(loaded)) {
|
|
128
|
+
throw usageError("--overrides is a JSON list of operations.", "--overrides");
|
|
129
|
+
}
|
|
130
|
+
list.push(...loaded);
|
|
131
|
+
}
|
|
132
|
+
for (const entry of context.flags.override ?? []) {
|
|
133
|
+
list.push(parseOverride(entry, (path) => text(context, path)));
|
|
134
|
+
}
|
|
135
|
+
return list.length > 0 ? list : undefined;
|
|
136
|
+
}
|
|
137
|
+
/** What --data, --overrides, --override, --number and --metadata say. */
|
|
138
|
+
async function renderParams(context) {
|
|
139
|
+
const data = typeof context.flags.data === "string"
|
|
140
|
+
? await jsonInput(context, context.flags.data, "data")
|
|
141
|
+
: undefined;
|
|
142
|
+
const overrides = await overridesFrom(context);
|
|
143
|
+
const metadata = keyValues(context.flags.metadata, "metadata");
|
|
144
|
+
return {
|
|
145
|
+
...(data !== undefined ? { data: data } : {}),
|
|
146
|
+
...(typeof context.flags.number === "string" ? { number: context.flags.number } : {}),
|
|
147
|
+
...(metadata !== undefined ? { metadata } : {}),
|
|
148
|
+
...(overrides !== undefined ? { overrides } : {}),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
async function generateParams(context) {
|
|
152
|
+
return {
|
|
153
|
+
template: context.args.template,
|
|
154
|
+
...(typeof context.flags.version === "number" ? { version: context.flags.version } : {}),
|
|
155
|
+
...(await renderParams(context)),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function requestOptions(context) {
|
|
159
|
+
return typeof context.flags["idempotency-key"] === "string"
|
|
160
|
+
? { idempotencyKey: context.flags["idempotency-key"] }
|
|
161
|
+
: undefined;
|
|
162
|
+
}
|
|
163
|
+
async function saveAndOpen(context, document) {
|
|
164
|
+
if (typeof context.flags.output === "string") {
|
|
165
|
+
const bytes = await context.zpdf.documents.download(document.id);
|
|
166
|
+
context.io.writeFile(context.flags.output, bytes);
|
|
167
|
+
context.note(`Saved ${context.flags.output} (${bytes.length} bytes).`);
|
|
168
|
+
}
|
|
169
|
+
if (context.flags.open === true) {
|
|
170
|
+
await context.io.open(typeof context.flags.output === "string" ? context.flags.output : document.download_url);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const DATA_FLAGS = [
|
|
174
|
+
{
|
|
175
|
+
name: "data",
|
|
176
|
+
short: "d",
|
|
177
|
+
type: "string",
|
|
178
|
+
value: "<file>",
|
|
179
|
+
help: "The data as a JSON file, - for stdin, or inline JSON",
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "override",
|
|
183
|
+
type: "list",
|
|
184
|
+
value: "<op>",
|
|
185
|
+
help: "One override operation; repeat for more, applied in order",
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "overrides",
|
|
189
|
+
type: "string",
|
|
190
|
+
value: "<file>",
|
|
191
|
+
help: "A JSON file holding a list of override operations (applied before --override)",
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
name: "number",
|
|
195
|
+
type: "string",
|
|
196
|
+
value: "<text>",
|
|
197
|
+
help: "Your document number; names the file and the PDF's title",
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: "metadata",
|
|
201
|
+
type: "list",
|
|
202
|
+
value: "<name=value>",
|
|
203
|
+
help: "A text label for finding the document later; repeat for more",
|
|
204
|
+
},
|
|
205
|
+
];
|
|
206
|
+
const OUTPUT_FLAGS = [
|
|
207
|
+
{ name: "output", short: "o", type: "string", value: "<file>", help: "Download the PDF to this file" },
|
|
208
|
+
{ name: "open", type: "boolean", help: "Open the PDF when it is ready" },
|
|
209
|
+
];
|
|
210
|
+
const IDEMPOTENCY_FLAG = {
|
|
211
|
+
name: "idempotency-key",
|
|
212
|
+
type: "string",
|
|
213
|
+
value: "<key>",
|
|
214
|
+
help: "Idempotency-Key; the same key and body within 24 hours returns the first result",
|
|
215
|
+
};
|
|
216
|
+
const PAGE_FLAGS = [
|
|
217
|
+
{ name: "limit", type: "number", value: "<n>", help: "Results per page, 1 to 100 (default 20)" },
|
|
218
|
+
{
|
|
219
|
+
name: "starting-after",
|
|
220
|
+
type: "string",
|
|
221
|
+
value: "<cursor>",
|
|
222
|
+
help: "The next_cursor of the previous page",
|
|
223
|
+
},
|
|
224
|
+
{ name: "all", type: "boolean", help: "Read every page" },
|
|
225
|
+
];
|
|
226
|
+
/** What --limit and --starting-after say. */
|
|
227
|
+
function pageParams(context) {
|
|
228
|
+
return {
|
|
229
|
+
...(typeof context.flags.limit === "number" ? { limit: context.flags.limit } : {}),
|
|
230
|
+
...(typeof context.flags["starting-after"] === "string"
|
|
231
|
+
? { starting_after: context.flags["starting-after"] }
|
|
232
|
+
: {}),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/** One page, or with --all every page as one. */
|
|
236
|
+
async function paged(context, list) {
|
|
237
|
+
return context.flags.all === true ? { data: await list.all(), has_more: false, next_cursor: null } : list;
|
|
238
|
+
}
|
|
239
|
+
const SHOWN_ONCE = (what) => `The ${what} is shown once. With --idempotency-key, the same command within 24 hours prints it again.`;
|
|
240
|
+
// ---- The commands -----------------------------------------------------------
|
|
241
|
+
export const commands = [
|
|
242
|
+
{
|
|
243
|
+
name: "signup start",
|
|
244
|
+
summary: "Email a 6-digit sign-up code to an address",
|
|
245
|
+
operation: "POST /signup",
|
|
246
|
+
args: [{ name: "email", help: "The address that will own the business" }],
|
|
247
|
+
example: "zpdf signup start agent@example.com",
|
|
248
|
+
run: (c) => c.zpdf.signup.start({ email: c.args.email }),
|
|
249
|
+
human: (value, c) => c.line(`A 6-digit code was emailed to ${value.email}. Next: zpdf signup verify ${value.email} <code>`),
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "signup verify",
|
|
253
|
+
summary: "Trade the emailed code for a business and its first secret key, and sign in",
|
|
254
|
+
operation: "POST /signup/verify",
|
|
255
|
+
args: [
|
|
256
|
+
{ name: "email", help: "The address the code was sent to" },
|
|
257
|
+
{ name: "code", help: "The 6-digit code" },
|
|
258
|
+
],
|
|
259
|
+
flags: [
|
|
260
|
+
{
|
|
261
|
+
name: "business-name",
|
|
262
|
+
type: "string",
|
|
263
|
+
value: "<name>",
|
|
264
|
+
help: "Plain words, up to 60 characters",
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
name: "key-name",
|
|
268
|
+
type: "string",
|
|
269
|
+
value: "<name>",
|
|
270
|
+
help: "A name for the key (default: first key)",
|
|
271
|
+
},
|
|
272
|
+
{ name: "no-save", type: "boolean", help: "Print the key without storing it in the config file" },
|
|
273
|
+
],
|
|
274
|
+
example: 'zpdf signup verify agent@example.com 123456 --business-name "Example Trading"',
|
|
275
|
+
notes: "The secret key is shown once. It is stored in the config file unless --no-save is given.",
|
|
276
|
+
async run(c) {
|
|
277
|
+
const result = await c.zpdf.signup.verify({
|
|
278
|
+
email: c.args.email,
|
|
279
|
+
code: c.args.code,
|
|
280
|
+
...(typeof c.flags["business-name"] === "string"
|
|
281
|
+
? { business_name: c.flags["business-name"] }
|
|
282
|
+
: {}),
|
|
283
|
+
...(typeof c.flags["key-name"] === "string" ? { key_name: c.flags["key-name"] } : {}),
|
|
284
|
+
});
|
|
285
|
+
if (c.flags["no-save"] !== true) {
|
|
286
|
+
const path = writeConfig(c.io.env, {
|
|
287
|
+
...readConfig(c.io.env),
|
|
288
|
+
apiKey: result.secret_key,
|
|
289
|
+
apiUrl: c.zpdf.baseUrl,
|
|
290
|
+
});
|
|
291
|
+
c.note(`Signed in. The key is stored in ${path}.`);
|
|
292
|
+
}
|
|
293
|
+
c.note("The secret key is shown once. Keep it out of files you commit.");
|
|
294
|
+
return result;
|
|
295
|
+
},
|
|
296
|
+
human(value, c) {
|
|
297
|
+
c.line(`business: ${value.business.name} (${value.business.id}, ${value.business.plan})`);
|
|
298
|
+
c.line(`secret_key: ${value.secret_key}`);
|
|
299
|
+
c.line(`console: ${value.console_url}`);
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
name: "login",
|
|
304
|
+
summary: "Store a secret key in the config file after checking it",
|
|
305
|
+
operation: "GET /business",
|
|
306
|
+
flags: [
|
|
307
|
+
{
|
|
308
|
+
name: "key",
|
|
309
|
+
type: "string",
|
|
310
|
+
value: "<key>",
|
|
311
|
+
help: "The secret key (zp_sk_…), or - to read it from stdin (default: ZPDF_API_KEY)",
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
example: "zpdf login --key zp_sk_…",
|
|
315
|
+
notes: "No key yet? Run: zpdf signup start <email>",
|
|
316
|
+
async run(c) {
|
|
317
|
+
const given = c.flags.key === "-" ? (await c.io.readStdin()).trim() : c.flags.key;
|
|
318
|
+
const key = given ?? environmentKey(c.io.env);
|
|
319
|
+
if (key === undefined || key === "") {
|
|
320
|
+
throw usageError("login needs a key: zpdf login --key zp_sk_… (or --key - to read stdin). No key yet? zpdf signup start <email>.", "--key");
|
|
321
|
+
}
|
|
322
|
+
const business = await new ZPDF({
|
|
323
|
+
apiKey: key,
|
|
324
|
+
baseUrl: c.zpdf.baseUrl,
|
|
325
|
+
fetch: clientFetch(c),
|
|
326
|
+
...(c.flags["dry-run"] === true ? { maxRetries: 0 } : {}),
|
|
327
|
+
}).business.get();
|
|
328
|
+
const path = writeConfig(c.io.env, {
|
|
329
|
+
...readConfig(c.io.env),
|
|
330
|
+
apiKey: key,
|
|
331
|
+
apiUrl: c.zpdf.baseUrl,
|
|
332
|
+
});
|
|
333
|
+
return { object: "login", business, config_path: path };
|
|
334
|
+
},
|
|
335
|
+
human: (value, c) => c.line(`Signed in to ${value.business.name} (${value.business.plan}). The key is stored in ${value.config_path}.`),
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
name: "logout",
|
|
339
|
+
summary: "Remove the stored key from the config file",
|
|
340
|
+
operation: "local",
|
|
341
|
+
example: "zpdf logout",
|
|
342
|
+
async run(c) {
|
|
343
|
+
const config = readConfig(c.io.env);
|
|
344
|
+
delete config.apiKey;
|
|
345
|
+
return { object: "logout", config_path: writeConfig(c.io.env, config) };
|
|
346
|
+
},
|
|
347
|
+
human: (value, c) => c.line(`Signed out. ${value.config_path} no longer holds a key.`),
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
name: "info",
|
|
351
|
+
summary: "The API's version, docs address and plans (no key needed)",
|
|
352
|
+
operation: "GET /",
|
|
353
|
+
example: "zpdf info",
|
|
354
|
+
run: (c) => c.zpdf.info(),
|
|
355
|
+
human(value, c) {
|
|
356
|
+
c.line(`version: ${value.version}`);
|
|
357
|
+
c.line(`docs: ${value.docs}`);
|
|
358
|
+
table(c, [
|
|
359
|
+
"plan",
|
|
360
|
+
"price_usd_per_month",
|
|
361
|
+
"documents_per_month",
|
|
362
|
+
"pages_per_document",
|
|
363
|
+
"retention_days",
|
|
364
|
+
"share_link_max_days",
|
|
365
|
+
], Object.entries(value.plans).map(([plan, facts]) => ({ plan, ...facts })));
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
{
|
|
369
|
+
name: "engine",
|
|
370
|
+
summary: "The browser build and font set that renders documents (no key needed)",
|
|
371
|
+
operation: "GET /engine",
|
|
372
|
+
example: "zpdf engine",
|
|
373
|
+
run: (c) => c.zpdf.engine(),
|
|
374
|
+
human: (value, c) => c.line(value.id),
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
name: "examples list",
|
|
378
|
+
summary: "List the example templates (no key needed)",
|
|
379
|
+
operation: "GET /examples",
|
|
380
|
+
flags: PAGE_FLAGS,
|
|
381
|
+
example: "zpdf examples list",
|
|
382
|
+
run: (c) => paged(c, c.zpdf.examples.list(pageParams(c))),
|
|
383
|
+
human: listOf(["key", "url"]),
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
name: "examples get",
|
|
387
|
+
summary: "Read one example: its HTML, CSS and inputs",
|
|
388
|
+
operation: "GET /examples/{key}",
|
|
389
|
+
args: [{ name: "key", help: "The example's key, for example invoice" }],
|
|
390
|
+
example: "zpdf examples get invoice --json",
|
|
391
|
+
run: (c) => c.zpdf.examples.get(c.args.key),
|
|
392
|
+
human(value, c) {
|
|
393
|
+
c.line(`key: ${value.key}`);
|
|
394
|
+
c.line(`html: ${value.html.length} characters`);
|
|
395
|
+
c.line(`css: ${value.css.length} characters`);
|
|
396
|
+
c.line(`fields: ${Object.keys(value.inputs.fields ?? {}).join(", ")}`);
|
|
397
|
+
c.note(`Write its files with: zpdf examples pull ${value.key}`);
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
{
|
|
401
|
+
name: "examples file",
|
|
402
|
+
summary: "Print one file of an example",
|
|
403
|
+
operation: "GET /examples/{key}/{file}",
|
|
404
|
+
args: [
|
|
405
|
+
{ name: "key", help: "The example's key" },
|
|
406
|
+
{ name: "file", help: "template.html, template.css, inputs.json or data-400-rows.json" },
|
|
407
|
+
],
|
|
408
|
+
example: "zpdf examples file packing-list data-400-rows.json > data.json",
|
|
409
|
+
async run(c) {
|
|
410
|
+
return {
|
|
411
|
+
object: "example_file",
|
|
412
|
+
key: c.args.key,
|
|
413
|
+
file: c.args.file,
|
|
414
|
+
content: await c.zpdf.examples.file(c.args.key, c.args.file),
|
|
415
|
+
};
|
|
416
|
+
},
|
|
417
|
+
human: (value, c) => c.io.stdout(value.content.endsWith("\n") ? value.content : `${value.content}\n`),
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
name: "examples pull",
|
|
421
|
+
summary: "Write an example's template.html, template.css and inputs.json to a directory",
|
|
422
|
+
operation: "GET /examples/{key}",
|
|
423
|
+
args: [{ name: "key", help: "The example's key" }],
|
|
424
|
+
flags: [
|
|
425
|
+
{
|
|
426
|
+
name: "dir",
|
|
427
|
+
type: "string",
|
|
428
|
+
value: "<dir>",
|
|
429
|
+
help: "Where to write the files (default: the current directory)",
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
example: "zpdf examples pull invoice --dir invoice",
|
|
433
|
+
async run(c) {
|
|
434
|
+
const example = await c.zpdf.examples.get(c.args.key);
|
|
435
|
+
return { object: "example_files", key: example.key, files: writeTemplateFiles(c, example) };
|
|
436
|
+
},
|
|
437
|
+
human: (value, c) => value.files.forEach((file) => c.line(file)),
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
name: "templates push",
|
|
441
|
+
aliases: ["templates save"],
|
|
442
|
+
summary: "Save a new version of a template from local HTML, CSS and JSON files",
|
|
443
|
+
operation: "PUT /templates/{key}",
|
|
444
|
+
args: [{ name: "key", help: "The template's key: lowercase letters, digits and hyphens" }],
|
|
445
|
+
flags: [
|
|
446
|
+
{
|
|
447
|
+
name: "dir",
|
|
448
|
+
type: "string",
|
|
449
|
+
value: "<dir>",
|
|
450
|
+
help: "The directory holding template.html, template.css and inputs.json (default: .)",
|
|
451
|
+
},
|
|
452
|
+
{
|
|
453
|
+
name: "html",
|
|
454
|
+
type: "string",
|
|
455
|
+
value: "<file>",
|
|
456
|
+
help: "The HTML file (default: <dir>/template.html)",
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
name: "css",
|
|
460
|
+
type: "string",
|
|
461
|
+
value: "<file>",
|
|
462
|
+
help: "The CSS file (default: <dir>/template.css when it exists)",
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
name: "inputs",
|
|
466
|
+
type: "string",
|
|
467
|
+
value: "<file>",
|
|
468
|
+
help: "The inputs file (default: <dir>/inputs.json)",
|
|
469
|
+
},
|
|
470
|
+
{ name: "name", type: "string", value: "<name>", help: "A display name" },
|
|
471
|
+
{ name: "note", type: "string", value: "<text>", help: "A line for the changelog" },
|
|
472
|
+
{
|
|
473
|
+
name: "make-current",
|
|
474
|
+
type: "boolean",
|
|
475
|
+
help: "Make this version current (the first version always is)",
|
|
476
|
+
},
|
|
477
|
+
],
|
|
478
|
+
example: 'zpdf templates push invoice --dir invoice --make-current --note "First version"',
|
|
479
|
+
notes: "The version is checked and its example rendered. If anything is wrong nothing is saved and every fault is listed.",
|
|
480
|
+
async run(c) {
|
|
481
|
+
const dir = typeof c.flags.dir === "string" ? c.flags.dir : ".";
|
|
482
|
+
const htmlPath = typeof c.flags.html === "string" ? c.flags.html : join(dir, "template.html");
|
|
483
|
+
const cssPath = typeof c.flags.css === "string" ? c.flags.css : join(dir, "template.css");
|
|
484
|
+
const inputsPath = typeof c.flags.inputs === "string" ? c.flags.inputs : join(dir, "inputs.json");
|
|
485
|
+
const hasCss = typeof c.flags.css === "string" || c.io.fileExists(cssPath);
|
|
486
|
+
return c.zpdf.templates.save(c.args.key, {
|
|
487
|
+
html: text(c, htmlPath),
|
|
488
|
+
...(hasCss ? { css: text(c, cssPath) } : {}),
|
|
489
|
+
inputs: text(c, inputsPath),
|
|
490
|
+
...(typeof c.flags.name === "string" ? { name: c.flags.name } : {}),
|
|
491
|
+
...(typeof c.flags.note === "string" ? { note: c.flags.note } : {}),
|
|
492
|
+
...(c.flags["make-current"] === true ? { make_current: true } : {}),
|
|
493
|
+
});
|
|
494
|
+
},
|
|
495
|
+
human: showTemplate,
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
name: "templates list",
|
|
499
|
+
summary: "List templates",
|
|
500
|
+
operation: "GET /templates",
|
|
501
|
+
flags: [{ name: "archived", type: "boolean", help: "Include archived templates" }, ...PAGE_FLAGS],
|
|
502
|
+
example: "zpdf templates list",
|
|
503
|
+
run: (c) => paged(c, c.zpdf.templates.list({ archived: c.flags.archived === true, ...pageParams(c) })),
|
|
504
|
+
human: listOf(["key", "name", "current_version", "archived", "created"]),
|
|
505
|
+
},
|
|
506
|
+
{
|
|
507
|
+
name: "templates get",
|
|
508
|
+
summary: "Read a template: its regions, fields, example and current version",
|
|
509
|
+
operation: "GET /templates/{key}",
|
|
510
|
+
args: [{ name: "key", help: "The template's key" }],
|
|
511
|
+
flags: [
|
|
512
|
+
{
|
|
513
|
+
name: "version",
|
|
514
|
+
type: "number",
|
|
515
|
+
value: "<n>",
|
|
516
|
+
help: "Read this version instead of the current one",
|
|
517
|
+
},
|
|
518
|
+
],
|
|
519
|
+
example: "zpdf templates get invoice",
|
|
520
|
+
run: (c) => c.zpdf.templates.get(c.args.key, typeof c.flags.version === "number" ? { version: c.flags.version } : {}),
|
|
521
|
+
human: showTemplate,
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
name: "templates pull",
|
|
525
|
+
summary: "Write a template version's template.html, template.css and inputs.json to a directory",
|
|
526
|
+
operation: "GET /templates/{key}",
|
|
527
|
+
args: [{ name: "key", help: "The template's key" }],
|
|
528
|
+
flags: [
|
|
529
|
+
{
|
|
530
|
+
name: "version",
|
|
531
|
+
type: "number",
|
|
532
|
+
value: "<n>",
|
|
533
|
+
help: "The version to write (default: the current one)",
|
|
534
|
+
},
|
|
535
|
+
{
|
|
536
|
+
name: "dir",
|
|
537
|
+
type: "string",
|
|
538
|
+
value: "<dir>",
|
|
539
|
+
help: "Where to write the files (default: the current directory)",
|
|
540
|
+
},
|
|
541
|
+
],
|
|
542
|
+
example: "zpdf templates pull invoice --dir invoice",
|
|
543
|
+
async run(c) {
|
|
544
|
+
const template = await c.zpdf.templates.get(c.args.key, typeof c.flags.version === "number" ? { version: c.flags.version } : {});
|
|
545
|
+
return {
|
|
546
|
+
object: "template_files",
|
|
547
|
+
key: template.key,
|
|
548
|
+
version: template.version.number,
|
|
549
|
+
files: writeTemplateFiles(c, template.version),
|
|
550
|
+
};
|
|
551
|
+
},
|
|
552
|
+
human: (value, c) => value.files.forEach((file) => c.line(file)),
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
name: "templates update",
|
|
556
|
+
summary: "Move the current version, rename or archive a template",
|
|
557
|
+
operation: "PATCH /templates/{key}",
|
|
558
|
+
args: [{ name: "key", help: "The template's key" }],
|
|
559
|
+
flags: [
|
|
560
|
+
{
|
|
561
|
+
name: "current-version",
|
|
562
|
+
type: "number",
|
|
563
|
+
value: "<n>",
|
|
564
|
+
help: "Make this version current (forward or back)",
|
|
565
|
+
},
|
|
566
|
+
{ name: "name", type: "string", value: "<name>", help: "A new display name" },
|
|
567
|
+
{ name: "archive", type: "boolean", help: "Archive: no new documents; existing ones stay" },
|
|
568
|
+
{ name: "unarchive", type: "boolean", help: "Bring an archived template back" },
|
|
569
|
+
],
|
|
570
|
+
example: "zpdf templates update invoice --current-version 1",
|
|
571
|
+
run(c) {
|
|
572
|
+
const params = {
|
|
573
|
+
...(typeof c.flags["current-version"] === "number"
|
|
574
|
+
? { current_version: c.flags["current-version"] }
|
|
575
|
+
: {}),
|
|
576
|
+
...(typeof c.flags.name === "string" ? { name: c.flags.name } : {}),
|
|
577
|
+
...(c.flags.archive === true
|
|
578
|
+
? { archived: true }
|
|
579
|
+
: c.flags.unarchive === true
|
|
580
|
+
? { archived: false }
|
|
581
|
+
: {}),
|
|
582
|
+
};
|
|
583
|
+
if (Object.keys(params).length === 0) {
|
|
584
|
+
throw usageError("Nothing to change. Pass --current-version, --name, --archive or --unarchive.");
|
|
585
|
+
}
|
|
586
|
+
return c.zpdf.templates.update(c.args.key, params);
|
|
587
|
+
},
|
|
588
|
+
human: showTemplate,
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
name: "template-versions list",
|
|
592
|
+
summary: "List a template's versions, newest first",
|
|
593
|
+
operation: "GET /templates/{key}/versions",
|
|
594
|
+
args: [{ name: "key", help: "The template's key" }],
|
|
595
|
+
flags: PAGE_FLAGS,
|
|
596
|
+
example: "zpdf template-versions list invoice",
|
|
597
|
+
run: (c) => paged(c, c.zpdf.templateVersions.list(c.args.key, pageParams(c))),
|
|
598
|
+
human: listOf(["number", "current", "note", "promoted_from", "created", "id"]),
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
name: "template-versions get",
|
|
602
|
+
summary: "Read one version in full: its files, inputs, regions and example",
|
|
603
|
+
operation: "GET /templates/{key}/versions/{number}",
|
|
604
|
+
args: [
|
|
605
|
+
{ name: "key", help: "The template's key" },
|
|
606
|
+
{ name: "number", help: "The version number" },
|
|
607
|
+
],
|
|
608
|
+
example: "zpdf template-versions get invoice 2",
|
|
609
|
+
run(c) {
|
|
610
|
+
const number = Number(c.args.number);
|
|
611
|
+
if (!Number.isInteger(number) || number < 1) {
|
|
612
|
+
throw usageError("<number> is a version number: 1, 2, 3…", "number");
|
|
613
|
+
}
|
|
614
|
+
return c.zpdf.templateVersions.get(c.args.key, number);
|
|
615
|
+
},
|
|
616
|
+
human: showVersion,
|
|
617
|
+
},
|
|
618
|
+
{
|
|
619
|
+
name: "documents generate",
|
|
620
|
+
aliases: ["render"],
|
|
621
|
+
summary: "Generate a PDF from a template and data, with optional overrides",
|
|
622
|
+
operation: "POST /documents",
|
|
623
|
+
args: [{ name: "template", help: "The template's key" }],
|
|
624
|
+
flags: [
|
|
625
|
+
...DATA_FLAGS,
|
|
626
|
+
{
|
|
627
|
+
name: "version",
|
|
628
|
+
type: "number",
|
|
629
|
+
value: "<n>",
|
|
630
|
+
help: "The template version (default: the current one)",
|
|
631
|
+
},
|
|
632
|
+
IDEMPOTENCY_FLAG,
|
|
633
|
+
...OUTPUT_FLAGS,
|
|
634
|
+
],
|
|
635
|
+
example: "zpdf documents generate invoice --data data.json --override remove:col-unit-price -o invoice.pdf",
|
|
636
|
+
notes: OVERRIDE_HELP,
|
|
637
|
+
async run(c) {
|
|
638
|
+
const document = await c.zpdf.documents.generate(await generateParams(c), requestOptions(c));
|
|
639
|
+
await saveAndOpen(c, document);
|
|
640
|
+
return document;
|
|
641
|
+
},
|
|
642
|
+
human: showDocument,
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
name: "documents validate",
|
|
646
|
+
summary: "Run every check short of rendering; makes no document",
|
|
647
|
+
operation: "POST /documents/validate",
|
|
648
|
+
args: [{ name: "template", help: "The template's key" }],
|
|
649
|
+
flags: [
|
|
650
|
+
...DATA_FLAGS,
|
|
651
|
+
{
|
|
652
|
+
name: "version",
|
|
653
|
+
type: "number",
|
|
654
|
+
value: "<n>",
|
|
655
|
+
help: "The template version (default: the current one)",
|
|
656
|
+
},
|
|
657
|
+
],
|
|
658
|
+
example: "zpdf documents validate invoice --data data.json --override remove:col-unit-price",
|
|
659
|
+
notes: OVERRIDE_HELP,
|
|
660
|
+
run: async (c) => c.zpdf.documents.validate(await generateParams(c)),
|
|
661
|
+
human(value, c) {
|
|
662
|
+
c.line(`valid: ${value.template} version ${value.version}`);
|
|
663
|
+
c.line(`template_sha256: ${value.template_sha256}`);
|
|
664
|
+
c.line(`regions: ${value.regions.join(", ") || "-"}`);
|
|
665
|
+
warnings(c, value.warnings);
|
|
666
|
+
},
|
|
667
|
+
},
|
|
668
|
+
{
|
|
669
|
+
name: "documents list",
|
|
670
|
+
summary: "List documents, newest first",
|
|
671
|
+
operation: "GET /documents",
|
|
672
|
+
flags: [
|
|
673
|
+
{ name: "template", type: "string", value: "<key>", help: "Only this template's documents" },
|
|
674
|
+
{ name: "number", type: "string", value: "<text>", help: "Only this document number" },
|
|
675
|
+
{ name: "status", type: "string", value: "<status>", help: "ready or deleted" },
|
|
676
|
+
{
|
|
677
|
+
name: "metadata",
|
|
678
|
+
type: "list",
|
|
679
|
+
value: "<name=value>",
|
|
680
|
+
help: "Only documents with this label; repeat for more",
|
|
681
|
+
},
|
|
682
|
+
{ name: "created-after", type: "string", value: "<time>", help: "ISO 8601 date and time" },
|
|
683
|
+
{ name: "created-before", type: "string", value: "<time>", help: "ISO 8601 date and time" },
|
|
684
|
+
...PAGE_FLAGS,
|
|
685
|
+
],
|
|
686
|
+
example: "zpdf documents list --template invoice --metadata order=ORD-1042",
|
|
687
|
+
async run(c) {
|
|
688
|
+
const metadata = keyValues(c.flags.metadata, "metadata");
|
|
689
|
+
return paged(c, c.zpdf.documents.list({
|
|
690
|
+
...(typeof c.flags.template === "string" ? { template: c.flags.template } : {}),
|
|
691
|
+
...(typeof c.flags.number === "string" ? { number: c.flags.number } : {}),
|
|
692
|
+
...(typeof c.flags.status === "string" ? { status: c.flags.status } : {}),
|
|
693
|
+
...(metadata !== undefined ? { metadata } : {}),
|
|
694
|
+
...(typeof c.flags["created-after"] === "string"
|
|
695
|
+
? { created_after: c.flags["created-after"] }
|
|
696
|
+
: {}),
|
|
697
|
+
...(typeof c.flags["created-before"] === "string"
|
|
698
|
+
? { created_before: c.flags["created-before"] }
|
|
699
|
+
: {}),
|
|
700
|
+
...pageParams(c),
|
|
701
|
+
}));
|
|
702
|
+
},
|
|
703
|
+
human: listOf(["id", "status", "template", "version", "number", "pages", "created"]),
|
|
704
|
+
},
|
|
705
|
+
{
|
|
706
|
+
name: "documents get",
|
|
707
|
+
summary: "Read one document's record, with fresh URLs",
|
|
708
|
+
operation: "GET /documents/{id}",
|
|
709
|
+
args: [{ name: "id", help: "The document's id (doc_…)" }],
|
|
710
|
+
flags: [
|
|
711
|
+
{
|
|
712
|
+
name: "url-ttl",
|
|
713
|
+
type: "number",
|
|
714
|
+
value: "<seconds>",
|
|
715
|
+
help: "How long download_url works: 60 to 86400 (default 900)",
|
|
716
|
+
},
|
|
717
|
+
],
|
|
718
|
+
example: "zpdf documents get doc_… --url-ttl 3600",
|
|
719
|
+
run: (c) => c.zpdf.documents.get(c.args.id, typeof c.flags["url-ttl"] === "number" ? { url_ttl: c.flags["url-ttl"] } : {}),
|
|
720
|
+
human: showDocument,
|
|
721
|
+
},
|
|
722
|
+
{
|
|
723
|
+
name: "documents download",
|
|
724
|
+
summary: "Download a document's PDF",
|
|
725
|
+
operation: "GET /documents/{id}/file",
|
|
726
|
+
args: [{ name: "id", help: "The document's id (doc_…)" }],
|
|
727
|
+
flags: [
|
|
728
|
+
{
|
|
729
|
+
name: "output",
|
|
730
|
+
short: "o",
|
|
731
|
+
type: "string",
|
|
732
|
+
value: "<file>",
|
|
733
|
+
help: "Where to write the PDF (default: <id>.pdf; - for stdout)",
|
|
734
|
+
},
|
|
735
|
+
{ name: "open", type: "boolean", help: "Open the PDF after downloading" },
|
|
736
|
+
],
|
|
737
|
+
example: "zpdf documents download doc_… -o invoice.pdf",
|
|
738
|
+
async run(c) {
|
|
739
|
+
const bytes = await c.zpdf.documents.download(c.args.id);
|
|
740
|
+
const path = typeof c.flags.output === "string" ? c.flags.output : `${c.args.id}.pdf`;
|
|
741
|
+
if (path === "-") {
|
|
742
|
+
c.io.stdout(bytes);
|
|
743
|
+
return undefined;
|
|
744
|
+
}
|
|
745
|
+
c.io.writeFile(path, bytes);
|
|
746
|
+
if (c.flags.open === true)
|
|
747
|
+
await c.io.open(path);
|
|
748
|
+
return { object: "download", id: c.args.id, path, bytes: bytes.length };
|
|
749
|
+
},
|
|
750
|
+
human: (value, c) => {
|
|
751
|
+
if (value !== undefined)
|
|
752
|
+
c.line(`Saved ${value.path} (${value.bytes} bytes).`);
|
|
753
|
+
},
|
|
754
|
+
},
|
|
755
|
+
{
|
|
756
|
+
name: "documents open",
|
|
757
|
+
summary: "Open a document's PDF in the browser",
|
|
758
|
+
operation: "GET /documents/{id}",
|
|
759
|
+
args: [{ name: "id", help: "The document's id (doc_…)" }],
|
|
760
|
+
example: "zpdf documents open doc_…",
|
|
761
|
+
async run(c) {
|
|
762
|
+
const document = await c.zpdf.documents.get(c.args.id);
|
|
763
|
+
if (document.download_url === undefined) {
|
|
764
|
+
throw new CliError("This document was deleted: it has no PDF.", EXIT.FAILURE, "cli_error", "document_deleted");
|
|
765
|
+
}
|
|
766
|
+
await c.io.open(document.download_url);
|
|
767
|
+
return { object: "opened", id: document.id, url: document.download_url };
|
|
768
|
+
},
|
|
769
|
+
human: (value, c) => c.line(`Opened ${value.url}`),
|
|
770
|
+
},
|
|
771
|
+
{
|
|
772
|
+
name: "documents delete",
|
|
773
|
+
summary: "Erase a document's PDF, images, data and overrides; a stub remains",
|
|
774
|
+
operation: "DELETE /documents/{id}",
|
|
775
|
+
args: [{ name: "id", help: "The document's id (doc_…)" }],
|
|
776
|
+
example: "zpdf documents delete doc_…",
|
|
777
|
+
run: (c) => c.zpdf.documents.delete(c.args.id),
|
|
778
|
+
human: (value, c) => c.line(`Deleted ${value.id}${value.deleted !== undefined && value.deleted !== null ? ` at ${value.deleted}` : ""}.`),
|
|
779
|
+
},
|
|
780
|
+
{
|
|
781
|
+
name: "documents regenerate",
|
|
782
|
+
summary: "Render a document again from its record, optionally with corrected data or overrides",
|
|
783
|
+
operation: "POST /documents/{id}/regenerate",
|
|
784
|
+
args: [{ name: "id", help: "The document's id (doc_…)" }],
|
|
785
|
+
flags: [...DATA_FLAGS, IDEMPOTENCY_FLAG, ...OUTPUT_FLAGS],
|
|
786
|
+
example: "zpdf documents regenerate doc_… --data corrected.json",
|
|
787
|
+
notes: `With no flags the new PDF reproduces the original byte for byte (reproduced: true).\n${OVERRIDE_HELP}`,
|
|
788
|
+
async run(c) {
|
|
789
|
+
const document = await c.zpdf.documents.regenerate(c.args.id, await renderParams(c), requestOptions(c));
|
|
790
|
+
await saveAndOpen(c, document);
|
|
791
|
+
return document;
|
|
792
|
+
},
|
|
793
|
+
human: showDocument,
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
name: "documents promote",
|
|
797
|
+
summary: "Save a document's template (its version plus its overrides) as the template's next version",
|
|
798
|
+
operation: "POST /documents/{id}/promote",
|
|
799
|
+
args: [{ name: "id", help: "The document's id (doc_…)" }],
|
|
800
|
+
flags: [
|
|
801
|
+
{ name: "make-current", type: "boolean", help: "Make the new version current" },
|
|
802
|
+
{ name: "note", type: "string", value: "<text>", help: "A line for the changelog" },
|
|
803
|
+
{
|
|
804
|
+
name: "example-from-document",
|
|
805
|
+
type: "boolean",
|
|
806
|
+
help: "Use the document's data as the new version's example",
|
|
807
|
+
},
|
|
808
|
+
],
|
|
809
|
+
example: 'zpdf documents promote doc_… --make-current --note "No unit price column"',
|
|
810
|
+
run: (c) => c.zpdf.documents.promote(c.args.id, {
|
|
811
|
+
...(c.flags["make-current"] === true ? { make_current: true } : {}),
|
|
812
|
+
...(typeof c.flags.note === "string" ? { note: c.flags.note } : {}),
|
|
813
|
+
...(c.flags["example-from-document"] === true ? { example_from_document: true } : {}),
|
|
814
|
+
}),
|
|
815
|
+
human: showTemplate,
|
|
816
|
+
},
|
|
817
|
+
{
|
|
818
|
+
name: "fetch",
|
|
819
|
+
summary: "Download a signed URL: a download_url, a page image or a share link's file (no key sent)",
|
|
820
|
+
operation: "GET <signed URL>",
|
|
821
|
+
args: [
|
|
822
|
+
{
|
|
823
|
+
name: "url",
|
|
824
|
+
help: "A download_url, a page_images entry, or a share link's URL followed by /file",
|
|
825
|
+
},
|
|
826
|
+
],
|
|
827
|
+
flags: [
|
|
828
|
+
{
|
|
829
|
+
name: "output",
|
|
830
|
+
short: "o",
|
|
831
|
+
type: "string",
|
|
832
|
+
value: "<file>",
|
|
833
|
+
help: "Where to write the bytes (default: stdout)",
|
|
834
|
+
},
|
|
835
|
+
],
|
|
836
|
+
example: 'zpdf fetch "https://…/f/doc_…/document.pdf?expires=…&signature=…" -o invoice.pdf',
|
|
837
|
+
notes: "A URL past its time fails with url_expired; a share link that is expired, revoked or unknown fails with share_link_unavailable.",
|
|
838
|
+
async run(c) {
|
|
839
|
+
const bytes = await c.zpdf.fetchFile(c.args.url);
|
|
840
|
+
if (typeof c.flags.output !== "string" || c.flags.output === "-") {
|
|
841
|
+
c.io.stdout(bytes);
|
|
842
|
+
return undefined;
|
|
843
|
+
}
|
|
844
|
+
c.io.writeFile(c.flags.output, bytes);
|
|
845
|
+
return { object: "download", url: c.args.url, path: c.flags.output, bytes: bytes.length };
|
|
846
|
+
},
|
|
847
|
+
human: (value, c) => {
|
|
848
|
+
if (value !== undefined)
|
|
849
|
+
c.line(`Saved ${value.path} (${value.bytes} bytes).`);
|
|
850
|
+
},
|
|
851
|
+
},
|
|
852
|
+
{
|
|
853
|
+
name: "share-links create",
|
|
854
|
+
summary: "Make a public link to one document's PDF",
|
|
855
|
+
operation: "POST /documents/{id}/share_links",
|
|
856
|
+
args: [{ name: "document", help: "The document's id (doc_…)" }],
|
|
857
|
+
flags: [
|
|
858
|
+
{ name: "expires-in-days", type: "number", value: "<n>", help: "The link's lifetime in days" },
|
|
859
|
+
IDEMPOTENCY_FLAG,
|
|
860
|
+
],
|
|
861
|
+
example: "zpdf share-links create doc_… --expires-in-days 7",
|
|
862
|
+
notes: SHOWN_ONCE("URL"),
|
|
863
|
+
run: (c) => c.zpdf.shareLinks.create(c.args.document, typeof c.flags["expires-in-days"] === "number"
|
|
864
|
+
? { expires_in_days: c.flags["expires-in-days"] }
|
|
865
|
+
: {}, requestOptions(c)),
|
|
866
|
+
human: (value, c) => fields(c, value, ["url", "id", "document", "status", "expires"]),
|
|
867
|
+
},
|
|
868
|
+
{
|
|
869
|
+
name: "share-links list",
|
|
870
|
+
summary: "List a document's share links",
|
|
871
|
+
operation: "GET /documents/{id}/share_links",
|
|
872
|
+
args: [{ name: "document", help: "The document's id (doc_…)" }],
|
|
873
|
+
flags: PAGE_FLAGS,
|
|
874
|
+
example: "zpdf share-links list doc_…",
|
|
875
|
+
run: (c) => paged(c, c.zpdf.shareLinks.list(c.args.document, pageParams(c))),
|
|
876
|
+
human: listOf(["id", "status", "hint", "views", "expires", "created"]),
|
|
877
|
+
},
|
|
878
|
+
{
|
|
879
|
+
name: "share-links revoke",
|
|
880
|
+
summary: "Revoke a share link at once",
|
|
881
|
+
operation: "DELETE /share_links/{id}",
|
|
882
|
+
args: [{ name: "id", help: "The share link's id (shl_…)" }],
|
|
883
|
+
example: "zpdf share-links revoke shl_…",
|
|
884
|
+
run: (c) => c.zpdf.shareLinks.revoke(c.args.id),
|
|
885
|
+
human: (value, c) => c.line(`${value.id} is ${value.status}.`),
|
|
886
|
+
},
|
|
887
|
+
{
|
|
888
|
+
name: "assets upload",
|
|
889
|
+
summary: "Store an image or font under a name templates can use",
|
|
890
|
+
operation: "POST /assets",
|
|
891
|
+
args: [{ name: "file", help: "The image or font file" }],
|
|
892
|
+
flags: [
|
|
893
|
+
{
|
|
894
|
+
name: "name",
|
|
895
|
+
type: "string",
|
|
896
|
+
value: "<name>",
|
|
897
|
+
help: "Lowercase letters, digits, hyphens and underscores (default: from the file name)",
|
|
898
|
+
},
|
|
899
|
+
],
|
|
900
|
+
example: "zpdf assets upload logo.png --name logo",
|
|
901
|
+
run(c) {
|
|
902
|
+
const filename = basename(c.args.file);
|
|
903
|
+
const name = typeof c.flags.name === "string"
|
|
904
|
+
? c.flags.name
|
|
905
|
+
: filename
|
|
906
|
+
.replace(/\.[^.]+$/, "")
|
|
907
|
+
.toLowerCase()
|
|
908
|
+
.replace(/[^a-z0-9_-]+/g, "_");
|
|
909
|
+
return c.zpdf.assets.upload({ file: c.io.readFile(c.args.file), name, filename });
|
|
910
|
+
},
|
|
911
|
+
human: (value, c) => fields(c, value, ["id", "name", "content_type", "bytes", "sha256", "created"]),
|
|
912
|
+
},
|
|
913
|
+
{
|
|
914
|
+
name: "assets list",
|
|
915
|
+
summary: "List stored images and fonts",
|
|
916
|
+
operation: "GET /assets",
|
|
917
|
+
flags: PAGE_FLAGS,
|
|
918
|
+
example: "zpdf assets list",
|
|
919
|
+
run: (c) => paged(c, c.zpdf.assets.list(pageParams(c))),
|
|
920
|
+
human: listOf(["id", "name", "content_type", "bytes", "created"]),
|
|
921
|
+
},
|
|
922
|
+
{
|
|
923
|
+
name: "assets get",
|
|
924
|
+
summary: "Read one asset by id or name",
|
|
925
|
+
operation: "GET /assets/{id or name}",
|
|
926
|
+
args: [{ name: "asset", help: "The asset's id (ast_…) or name" }],
|
|
927
|
+
example: "zpdf assets get logo",
|
|
928
|
+
run: (c) => c.zpdf.assets.get(c.args.asset),
|
|
929
|
+
human: (value, c) => fields(c, value, ["id", "name", "content_type", "bytes", "sha256", "created"]),
|
|
930
|
+
},
|
|
931
|
+
{
|
|
932
|
+
name: "assets delete",
|
|
933
|
+
summary: "Delete an asset no current template version uses",
|
|
934
|
+
operation: "DELETE /assets/{id or name}",
|
|
935
|
+
args: [{ name: "asset", help: "The asset's id (ast_…) or name" }],
|
|
936
|
+
example: "zpdf assets delete logo",
|
|
937
|
+
run: (c) => c.zpdf.assets.delete(c.args.asset),
|
|
938
|
+
human: (value, c) => c.line(`Deleted ${value.id}.`),
|
|
939
|
+
},
|
|
940
|
+
{
|
|
941
|
+
name: "webhooks create",
|
|
942
|
+
summary: "Register the business's webhook endpoint and send it a verification challenge",
|
|
943
|
+
operation: "POST /webhook_endpoints",
|
|
944
|
+
flags: [
|
|
945
|
+
{
|
|
946
|
+
name: "url",
|
|
947
|
+
type: "string",
|
|
948
|
+
value: "<url>",
|
|
949
|
+
help: "An https URL on port 443 at a public address",
|
|
950
|
+
},
|
|
951
|
+
{
|
|
952
|
+
name: "event",
|
|
953
|
+
type: "list",
|
|
954
|
+
value: "<type>",
|
|
955
|
+
help: "An event type to send; repeat for more (default: every type)",
|
|
956
|
+
},
|
|
957
|
+
IDEMPOTENCY_FLAG,
|
|
958
|
+
],
|
|
959
|
+
example: "zpdf webhooks create --url https://hooks.example.com/zpdf --event document.ready --event document.failed",
|
|
960
|
+
notes: `${SHOWN_ONCE("signing secret (whsec_…)")}\nThe endpoint must answer the challenge before anything else is sent to it.`,
|
|
961
|
+
run(c) {
|
|
962
|
+
if (typeof c.flags.url !== "string")
|
|
963
|
+
throw usageError("webhooks create needs --url <url>.", "--url");
|
|
964
|
+
const events = c.flags.event;
|
|
965
|
+
c.note("The signing secret is shown once. Store it where your endpoint can read it.");
|
|
966
|
+
return c.zpdf.webhookEndpoints.create({
|
|
967
|
+
url: c.flags.url,
|
|
968
|
+
...(events !== undefined ? { enabled_events: events } : {}),
|
|
969
|
+
}, requestOptions(c));
|
|
970
|
+
},
|
|
971
|
+
human: (value, c) => fields(c, value, ["id", "url", "status", "enabled_events", "secret"]),
|
|
972
|
+
},
|
|
973
|
+
{
|
|
974
|
+
name: "webhooks list",
|
|
975
|
+
summary: "List webhook endpoints",
|
|
976
|
+
operation: "GET /webhook_endpoints",
|
|
977
|
+
flags: PAGE_FLAGS,
|
|
978
|
+
example: "zpdf webhooks list",
|
|
979
|
+
run: (c) => paged(c, c.zpdf.webhookEndpoints.list(pageParams(c))),
|
|
980
|
+
human: listOf(["id", "url", "status", "enabled_events", "created"]),
|
|
981
|
+
},
|
|
982
|
+
{
|
|
983
|
+
name: "webhooks verify",
|
|
984
|
+
summary: "Send a new verification challenge to an endpoint",
|
|
985
|
+
operation: "POST /webhook_endpoints/{id}/verify",
|
|
986
|
+
args: [{ name: "id", help: "The endpoint's id (whe_…)" }],
|
|
987
|
+
example: "zpdf webhooks verify whe_…",
|
|
988
|
+
run: (c) => c.zpdf.webhookEndpoints.verify(c.args.id),
|
|
989
|
+
human: (value, c) => c.line(`${value.id} is ${value.status}.`),
|
|
990
|
+
},
|
|
991
|
+
{
|
|
992
|
+
name: "webhooks update",
|
|
993
|
+
summary: "Change a webhook endpoint's URL or the events it receives",
|
|
994
|
+
operation: "PATCH /webhook_endpoints/{id}",
|
|
995
|
+
args: [{ name: "id", help: "The endpoint's id (whe_…)" }],
|
|
996
|
+
flags: [
|
|
997
|
+
{
|
|
998
|
+
name: "url",
|
|
999
|
+
type: "string",
|
|
1000
|
+
value: "<url>",
|
|
1001
|
+
help: "A new https URL on port 443 at a public address",
|
|
1002
|
+
},
|
|
1003
|
+
{
|
|
1004
|
+
name: "event",
|
|
1005
|
+
type: "list",
|
|
1006
|
+
value: "<type>",
|
|
1007
|
+
help: "An event type to send; repeat for more, or '*' for every type. Replaces the list",
|
|
1008
|
+
},
|
|
1009
|
+
],
|
|
1010
|
+
example: "zpdf webhooks update whe_… --event document.ready --event document.failed",
|
|
1011
|
+
notes: "The signing secret stays.\nA new URL makes the endpoint unverified and is sent a challenge at once; it must answer it before events are sent there.",
|
|
1012
|
+
run(c) {
|
|
1013
|
+
const events = c.flags.event;
|
|
1014
|
+
if (typeof c.flags.url !== "string" && events === undefined)
|
|
1015
|
+
throw usageError("webhooks update needs --url <url>, --event <type> or both.", "--url");
|
|
1016
|
+
return c.zpdf.webhookEndpoints.update(c.args.id, {
|
|
1017
|
+
...(typeof c.flags.url === "string" ? { url: c.flags.url } : {}),
|
|
1018
|
+
...(events !== undefined ? { enabled_events: events } : {}),
|
|
1019
|
+
});
|
|
1020
|
+
},
|
|
1021
|
+
human: (value, c) => fields(c, value, ["id", "url", "status", "enabled_events"]),
|
|
1022
|
+
},
|
|
1023
|
+
{
|
|
1024
|
+
name: "webhooks delete",
|
|
1025
|
+
summary: "Delete a webhook endpoint",
|
|
1026
|
+
operation: "DELETE /webhook_endpoints/{id}",
|
|
1027
|
+
args: [{ name: "id", help: "The endpoint's id (whe_…)" }],
|
|
1028
|
+
example: "zpdf webhooks delete whe_…",
|
|
1029
|
+
run: (c) => c.zpdf.webhookEndpoints.delete(c.args.id),
|
|
1030
|
+
human: (value, c) => c.line(`Deleted ${value.id}.`),
|
|
1031
|
+
},
|
|
1032
|
+
{
|
|
1033
|
+
name: "webhooks check",
|
|
1034
|
+
summary: "Check a received webhook's signature offline",
|
|
1035
|
+
operation: "local",
|
|
1036
|
+
flags: [
|
|
1037
|
+
{
|
|
1038
|
+
name: "secret",
|
|
1039
|
+
type: "string",
|
|
1040
|
+
value: "<secret>",
|
|
1041
|
+
help: "The endpoint's signing secret (default: ZPDF_WEBHOOK_SECRET)",
|
|
1042
|
+
},
|
|
1043
|
+
{ name: "id", type: "string", value: "<id>", help: "The webhook-id header" },
|
|
1044
|
+
{ name: "timestamp", type: "string", value: "<seconds>", help: "The webhook-timestamp header" },
|
|
1045
|
+
{ name: "signature", type: "string", value: "<signature>", help: "The webhook-signature header" },
|
|
1046
|
+
{
|
|
1047
|
+
name: "body",
|
|
1048
|
+
type: "string",
|
|
1049
|
+
value: "<file>",
|
|
1050
|
+
help: "The request body exactly as received: a file, or - for stdin",
|
|
1051
|
+
},
|
|
1052
|
+
{
|
|
1053
|
+
name: "tolerance",
|
|
1054
|
+
type: "number",
|
|
1055
|
+
value: "<seconds>",
|
|
1056
|
+
help: "How old the timestamp may be (default 300)",
|
|
1057
|
+
},
|
|
1058
|
+
],
|
|
1059
|
+
example: "zpdf webhooks check --id evt_… --timestamp 1795000000 --signature 'v1,…' --body body.json",
|
|
1060
|
+
async run(c) {
|
|
1061
|
+
const secret = c.flags.secret ?? c.io.env.ZPDF_WEBHOOK_SECRET;
|
|
1062
|
+
for (const name of ["id", "timestamp", "signature", "body"]) {
|
|
1063
|
+
if (typeof c.flags[name] !== "string")
|
|
1064
|
+
throw usageError(`webhooks check needs --${name}.`, `--${name}`);
|
|
1065
|
+
}
|
|
1066
|
+
if (secret === undefined)
|
|
1067
|
+
throw usageError("webhooks check needs --secret or ZPDF_WEBHOOK_SECRET.", "--secret");
|
|
1068
|
+
const body = c.flags.body === "-" ? await c.io.readStdin() : text(c, c.flags.body);
|
|
1069
|
+
try {
|
|
1070
|
+
const event = await verifyWebhook({
|
|
1071
|
+
body,
|
|
1072
|
+
secret,
|
|
1073
|
+
headers: {
|
|
1074
|
+
"webhook-id": c.flags.id,
|
|
1075
|
+
"webhook-timestamp": c.flags.timestamp,
|
|
1076
|
+
"webhook-signature": c.flags.signature,
|
|
1077
|
+
},
|
|
1078
|
+
...(typeof c.flags.tolerance === "number" ? { toleranceSeconds: c.flags.tolerance } : {}),
|
|
1079
|
+
});
|
|
1080
|
+
return { object: "webhook_check", valid: true, event };
|
|
1081
|
+
}
|
|
1082
|
+
catch (error) {
|
|
1083
|
+
throw new CliError(error instanceof Error ? error.message : String(error), EXIT.FAILURE, "webhook_error", "webhook_signature_invalid");
|
|
1084
|
+
}
|
|
1085
|
+
},
|
|
1086
|
+
human: (value, c) => c.line(`Valid: ${value.event.type} ${value.event.id}`),
|
|
1087
|
+
},
|
|
1088
|
+
{
|
|
1089
|
+
name: "events list",
|
|
1090
|
+
summary: "The log: every render, failed attempt, template change and share link, newest first",
|
|
1091
|
+
operation: "GET /events",
|
|
1092
|
+
flags: [
|
|
1093
|
+
{
|
|
1094
|
+
name: "type",
|
|
1095
|
+
type: "string",
|
|
1096
|
+
value: "<type>",
|
|
1097
|
+
help: "Only this event type, for example document.failed",
|
|
1098
|
+
},
|
|
1099
|
+
{
|
|
1100
|
+
name: "object",
|
|
1101
|
+
type: "string",
|
|
1102
|
+
value: "<id>",
|
|
1103
|
+
help: "Only events about this document, template or share link",
|
|
1104
|
+
},
|
|
1105
|
+
...PAGE_FLAGS,
|
|
1106
|
+
],
|
|
1107
|
+
example: "zpdf events list --type document.failed --limit 5",
|
|
1108
|
+
async run(c) {
|
|
1109
|
+
return paged(c, c.zpdf.events.list({
|
|
1110
|
+
...(typeof c.flags.type === "string" ? { type: c.flags.type } : {}),
|
|
1111
|
+
...(typeof c.flags.object === "string" ? { object: c.flags.object } : {}),
|
|
1112
|
+
...pageParams(c),
|
|
1113
|
+
}));
|
|
1114
|
+
},
|
|
1115
|
+
human(value, c) {
|
|
1116
|
+
listOf(["id", "type", "object", "created", "request_id"])({
|
|
1117
|
+
...value,
|
|
1118
|
+
data: value.data.map((event) => ({
|
|
1119
|
+
...event,
|
|
1120
|
+
object: event.data?.id ?? event.data?.error?.code,
|
|
1121
|
+
})),
|
|
1122
|
+
}, c);
|
|
1123
|
+
},
|
|
1124
|
+
},
|
|
1125
|
+
{
|
|
1126
|
+
name: "keys list",
|
|
1127
|
+
summary: "List secret keys (never their secrets)",
|
|
1128
|
+
operation: "GET /keys",
|
|
1129
|
+
flags: PAGE_FLAGS,
|
|
1130
|
+
example: "zpdf keys list",
|
|
1131
|
+
run: (c) => paged(c, c.zpdf.keys.list(pageParams(c))),
|
|
1132
|
+
human: listOf(["id", "name", "hint", "created", "last_used", "revoked"]),
|
|
1133
|
+
},
|
|
1134
|
+
{
|
|
1135
|
+
name: "keys create",
|
|
1136
|
+
summary: "Create a secret key",
|
|
1137
|
+
operation: "POST /keys",
|
|
1138
|
+
flags: [
|
|
1139
|
+
{
|
|
1140
|
+
name: "name",
|
|
1141
|
+
type: "string",
|
|
1142
|
+
value: "<name>",
|
|
1143
|
+
help: "A name; it is on every version and document the key makes",
|
|
1144
|
+
},
|
|
1145
|
+
IDEMPOTENCY_FLAG,
|
|
1146
|
+
],
|
|
1147
|
+
example: "zpdf keys create --name packing-agent",
|
|
1148
|
+
notes: SHOWN_ONCE("secret (zp_sk_…)"),
|
|
1149
|
+
run(c) {
|
|
1150
|
+
c.note("The secret is shown once. Keep it out of files you commit.");
|
|
1151
|
+
return c.zpdf.keys.create(typeof c.flags.name === "string" ? { name: c.flags.name } : {}, requestOptions(c));
|
|
1152
|
+
},
|
|
1153
|
+
human: (value, c) => fields(c, value, ["id", "name", "secret", "created"]),
|
|
1154
|
+
},
|
|
1155
|
+
{
|
|
1156
|
+
name: "keys rename",
|
|
1157
|
+
summary: "Rename a secret key",
|
|
1158
|
+
operation: "PATCH /keys/{id}",
|
|
1159
|
+
args: [{ name: "id", help: "The key's id (key_…)" }],
|
|
1160
|
+
flags: [{ name: "name", type: "string", value: "<name>", help: "The new name" }],
|
|
1161
|
+
example: "zpdf keys rename key_… --name packing-agent",
|
|
1162
|
+
notes: "The secret does not change.",
|
|
1163
|
+
run(c) {
|
|
1164
|
+
if (typeof c.flags.name !== "string")
|
|
1165
|
+
throw usageError("keys rename needs --name <name>.", "--name");
|
|
1166
|
+
return c.zpdf.keys.update(c.args.id, { name: c.flags.name });
|
|
1167
|
+
},
|
|
1168
|
+
human: (value, c) => fields(c, value, ["id", "name", "hint", "created"]),
|
|
1169
|
+
},
|
|
1170
|
+
{
|
|
1171
|
+
name: "keys revoke",
|
|
1172
|
+
summary: "Revoke a secret key at once",
|
|
1173
|
+
operation: "DELETE /keys/{id}",
|
|
1174
|
+
args: [{ name: "id", help: "The key's id (key_…)" }],
|
|
1175
|
+
example: "zpdf keys revoke key_…",
|
|
1176
|
+
run: (c) => c.zpdf.keys.revoke(c.args.id),
|
|
1177
|
+
human: (value, c) => c.line(`Revoked ${value.id}${value.revoked !== null ? ` at ${value.revoked}` : ""}.`),
|
|
1178
|
+
},
|
|
1179
|
+
{
|
|
1180
|
+
name: "business",
|
|
1181
|
+
aliases: ["whoami"],
|
|
1182
|
+
summary: "The business this key belongs to, its plan and limits",
|
|
1183
|
+
operation: "GET /business",
|
|
1184
|
+
example: "zpdf business",
|
|
1185
|
+
run: (c) => c.zpdf.business.get(),
|
|
1186
|
+
human(value, c) {
|
|
1187
|
+
fields(c, value, ["name", "id", "plan", "price_usd_per_month", "suspended", "api_url", "created"]);
|
|
1188
|
+
fields(c, value.limits, Object.keys(value.limits));
|
|
1189
|
+
},
|
|
1190
|
+
},
|
|
1191
|
+
{
|
|
1192
|
+
name: "usage",
|
|
1193
|
+
summary: "This month's documents, pages, storage and share links",
|
|
1194
|
+
operation: "GET /usage",
|
|
1195
|
+
example: "zpdf usage",
|
|
1196
|
+
run: (c) => c.zpdf.usage.get(),
|
|
1197
|
+
human(value, c) {
|
|
1198
|
+
c.line(`plan: ${value.plan}`);
|
|
1199
|
+
c.line(`period: ${value.period_start} to ${value.period_end}`);
|
|
1200
|
+
c.line(`documents: ${value.documents.used} of ${value.documents.limit}`);
|
|
1201
|
+
c.line(`pages: ${value.pages.used}`);
|
|
1202
|
+
c.line(`storage: ${value.storage.used_bytes} of ${value.storage.limit_bytes} bytes`);
|
|
1203
|
+
c.line(`share links: ${value.share_links.active} active of ${value.share_links.limit}`);
|
|
1204
|
+
},
|
|
1205
|
+
},
|
|
1206
|
+
{
|
|
1207
|
+
name: "billing get",
|
|
1208
|
+
summary: "The plan, the month's allowance and what is used, and whether zpdf is charging",
|
|
1209
|
+
operation: "GET /billing",
|
|
1210
|
+
example: "zpdf billing get",
|
|
1211
|
+
notes: "state is complimentary (the paid allowance at no charge), free, active, canceling or past_due.\nAn owner upgrades, cancels and resumes in the console, at manage_url.",
|
|
1212
|
+
run: (c) => c.zpdf.billing.get(),
|
|
1213
|
+
human(value, c) {
|
|
1214
|
+
c.line(`plan: ${value.plan} (${value.state})`);
|
|
1215
|
+
c.line(`charging: ${value.billing_enabled ? "on" : "off: every business has the full allowance at no charge"}`);
|
|
1216
|
+
c.line(`price: USD ${value.price_usd_per_month} a month`);
|
|
1217
|
+
c.line(`documents: ${value.documents.used} of ${value.documents.limit}, resets ${value.documents.resets}`);
|
|
1218
|
+
if (value.current_period_end !== null) {
|
|
1219
|
+
c.line(`${value.cancel_at_period_end ? "ends: " : "renews: "}${value.current_period_end}`);
|
|
1220
|
+
}
|
|
1221
|
+
if (value.grace_ends !== null)
|
|
1222
|
+
c.line(`grace ends: ${value.grace_ends}`);
|
|
1223
|
+
if (value.documents_kept_until !== null) {
|
|
1224
|
+
c.line(`kept until: ${value.documents_kept_until}`);
|
|
1225
|
+
}
|
|
1226
|
+
c.line(`manage: ${value.manage_url}`);
|
|
1227
|
+
},
|
|
1228
|
+
},
|
|
1229
|
+
{
|
|
1230
|
+
name: "billing checkout",
|
|
1231
|
+
summary: "The link an owner opens to move the business to the paid plan",
|
|
1232
|
+
operation: "POST /billing/checkout",
|
|
1233
|
+
example: "zpdf billing checkout",
|
|
1234
|
+
notes: "Hand the link to a person: it opens the console, where an owner of the business signs in and pays.\nAnswers billing_unavailable while zpdf is not charging, and plan_already_paid for a business on the paid plan.",
|
|
1235
|
+
run: (c) => c.zpdf.billing.checkout(),
|
|
1236
|
+
human(value, c) {
|
|
1237
|
+
c.line(value.url);
|
|
1238
|
+
c.note(`An owner opens this link to upgrade: USD ${value.price_usd_per_month} a month, ${value.documents_per_month} documents a month.`);
|
|
1239
|
+
},
|
|
1240
|
+
},
|
|
1241
|
+
{
|
|
1242
|
+
name: "members list",
|
|
1243
|
+
summary: "The team, including pending invitations",
|
|
1244
|
+
operation: "GET /members",
|
|
1245
|
+
flags: PAGE_FLAGS,
|
|
1246
|
+
example: "zpdf members list",
|
|
1247
|
+
notes: "Inviting, changing a role and removing a member are done by an owner signed in to the console.\nA secret key is refused on those with owner_session_required.",
|
|
1248
|
+
run: (c) => paged(c, c.zpdf.members.list(pageParams(c))),
|
|
1249
|
+
human: listOf(["id", "email", "role", "status", "founder", "created"]),
|
|
1250
|
+
},
|
|
1251
|
+
];
|
|
1252
|
+
/** The key in the environment: `ZPDF_API_KEY`, then `ZPDF_SECRET_KEY`. An empty value counts as unset. */
|
|
1253
|
+
export function environmentKey(env) {
|
|
1254
|
+
for (const name of ["ZPDF_API_KEY", "ZPDF_SECRET_KEY"]) {
|
|
1255
|
+
const value = env[name];
|
|
1256
|
+
if (value !== undefined && value !== "")
|
|
1257
|
+
return value;
|
|
1258
|
+
}
|
|
1259
|
+
return undefined;
|
|
1260
|
+
}
|
|
1261
|
+
/** The fetch the context's client uses, for the one command that builds a second client. */
|
|
1262
|
+
const fetches = new WeakMap();
|
|
1263
|
+
export function registerFetch(io, fetch) {
|
|
1264
|
+
if (fetch !== undefined)
|
|
1265
|
+
fetches.set(io, fetch);
|
|
1266
|
+
}
|
|
1267
|
+
function clientFetch(context) {
|
|
1268
|
+
return fetches.get(context.io);
|
|
1269
|
+
}
|
|
1270
|
+
function writeTemplateFiles(context, source) {
|
|
1271
|
+
const dir = typeof context.flags.dir === "string" ? context.flags.dir : ".";
|
|
1272
|
+
const files = [
|
|
1273
|
+
[join(dir, "template.html"), source.html],
|
|
1274
|
+
[join(dir, "template.css"), source.css],
|
|
1275
|
+
[join(dir, "inputs.json"), `${JSON.stringify(source.inputs, null, 4)}\n`],
|
|
1276
|
+
];
|
|
1277
|
+
for (const [path, content] of files)
|
|
1278
|
+
context.io.writeFile(path, content);
|
|
1279
|
+
return files.map(([path]) => path);
|
|
1280
|
+
}
|