@zpdf/sdk 0.0.0-stage → 0.3.1
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 +69 -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
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The zpdf API's objects and request bodies, exactly as they travel: names are
|
|
3
|
+
* the API's own snake_case, so a value read from the SDK, from curl and from
|
|
4
|
+
* the CLI's `--json` output is the same value.
|
|
5
|
+
*
|
|
6
|
+
* Within v1 the API adds fields and codes and never removes or changes one,
|
|
7
|
+
* so every object may carry fields that are not listed here.
|
|
8
|
+
*/
|
|
9
|
+
import type { Override } from "./overrides.js";
|
|
10
|
+
/** Who made something: a secret key, a signed-in member, or zpdf's operator. */
|
|
11
|
+
export interface Actor {
|
|
12
|
+
type: "key" | "user" | "operator" | (string & {});
|
|
13
|
+
id: string;
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
/** One fault or warning found in a template, an override list or the data. */
|
|
17
|
+
export interface Problem {
|
|
18
|
+
severity: "error" | "warning";
|
|
19
|
+
code: string;
|
|
20
|
+
message: string;
|
|
21
|
+
param?: string;
|
|
22
|
+
line?: number;
|
|
23
|
+
details?: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
/** A list response. `next_cursor` goes in `starting_after` to read the next page. */
|
|
26
|
+
export interface Page<T> {
|
|
27
|
+
data: T[];
|
|
28
|
+
has_more: boolean;
|
|
29
|
+
/** The `id` of the last item when `has_more` is true; otherwise `null`. */
|
|
30
|
+
next_cursor: string | null;
|
|
31
|
+
}
|
|
32
|
+
/** What every list accepts. */
|
|
33
|
+
export interface ListParams {
|
|
34
|
+
/** 1 to 100. Default 20. */
|
|
35
|
+
limit?: number;
|
|
36
|
+
/** The `next_cursor` of the previous page. */
|
|
37
|
+
starting_after?: string;
|
|
38
|
+
}
|
|
39
|
+
export type FieldType = "string" | "number" | "boolean" | "date" | "datetime" | "image" | "object" | "array";
|
|
40
|
+
/** The declaration of one input field. */
|
|
41
|
+
export interface FieldDecl {
|
|
42
|
+
type: FieldType;
|
|
43
|
+
required?: boolean;
|
|
44
|
+
description?: string;
|
|
45
|
+
/** The fields of an `object`. */
|
|
46
|
+
fields?: Record<string, FieldDecl>;
|
|
47
|
+
/** The shape of each row of an `array`. */
|
|
48
|
+
items?: FieldDecl;
|
|
49
|
+
min_items?: number;
|
|
50
|
+
}
|
|
51
|
+
export type PageSize = "A3" | "A4" | "A5" | "Letter" | "Legal";
|
|
52
|
+
export interface PageDecl {
|
|
53
|
+
size?: PageSize;
|
|
54
|
+
orientation?: "portrait" | "landscape";
|
|
55
|
+
/** A CSS margin in mm, cm, in, pt or px, for example `"18mm 16mm"`. */
|
|
56
|
+
margin?: string;
|
|
57
|
+
/** Template text placed at the top of every page. */
|
|
58
|
+
header?: string;
|
|
59
|
+
/** Template text placed at the bottom of every page. */
|
|
60
|
+
footer?: string;
|
|
61
|
+
}
|
|
62
|
+
/** A template's `inputs.json`: the page, the named assets, the declared fields and an example. */
|
|
63
|
+
export interface TemplateInputs {
|
|
64
|
+
page?: PageDecl;
|
|
65
|
+
/** Asset name used in the template, mapped to a stored asset's id or name. */
|
|
66
|
+
assets?: Record<string, string>;
|
|
67
|
+
fields: Record<string, FieldDecl>;
|
|
68
|
+
/** Example data. It is rendered when the version is saved. */
|
|
69
|
+
example?: Record<string, unknown>;
|
|
70
|
+
locale?: string;
|
|
71
|
+
timezone?: string;
|
|
72
|
+
}
|
|
73
|
+
/** What every form of a template version carries. */
|
|
74
|
+
export interface TemplateVersionFacts {
|
|
75
|
+
number: number;
|
|
76
|
+
sha256: string;
|
|
77
|
+
engine: string;
|
|
78
|
+
note: string | null;
|
|
79
|
+
/** The document this version was promoted from, if any. */
|
|
80
|
+
promoted_from: string | null;
|
|
81
|
+
created_by: Actor;
|
|
82
|
+
created: string;
|
|
83
|
+
}
|
|
84
|
+
/** What a full version adds to its summary: the files, the inputs and what saving found. */
|
|
85
|
+
export interface TemplateVersionContent {
|
|
86
|
+
/** Every `data-region` name in the template, the targets overrides can address. */
|
|
87
|
+
regions: string[];
|
|
88
|
+
/** Warnings found when the version was saved (`field_unused`, `region_removed`). */
|
|
89
|
+
problems: Problem[];
|
|
90
|
+
assets: Record<string, {
|
|
91
|
+
id: string;
|
|
92
|
+
sha256: string;
|
|
93
|
+
content_type?: string;
|
|
94
|
+
[key: string]: unknown;
|
|
95
|
+
}>;
|
|
96
|
+
/** The rendered example: its page count and one signed PNG URL per page. */
|
|
97
|
+
example: {
|
|
98
|
+
pages: number;
|
|
99
|
+
page_images: string[];
|
|
100
|
+
} | null;
|
|
101
|
+
html: string;
|
|
102
|
+
css: string;
|
|
103
|
+
inputs: TemplateInputs;
|
|
104
|
+
}
|
|
105
|
+
/** A `template_version` as `templateVersions.list` returns it: the summary, without files. */
|
|
106
|
+
export interface TemplateVersionSummary extends TemplateVersionFacts {
|
|
107
|
+
object: "template_version";
|
|
108
|
+
/** `tplv_…` */
|
|
109
|
+
id: string;
|
|
110
|
+
/** The template's key. */
|
|
111
|
+
template: string;
|
|
112
|
+
/** Whether this is the template's current version. */
|
|
113
|
+
current: boolean;
|
|
114
|
+
}
|
|
115
|
+
/** A `template_version` in full, as `templateVersions.get` returns it. */
|
|
116
|
+
export interface TemplateVersion extends TemplateVersionSummary, TemplateVersionContent {
|
|
117
|
+
}
|
|
118
|
+
/** A changelog entry in `versions` on `GET /templates/{key}`. */
|
|
119
|
+
export interface TemplateVersionEntry extends TemplateVersionFacts {
|
|
120
|
+
current: boolean;
|
|
121
|
+
}
|
|
122
|
+
/** The version embedded in a template by save, get, update and promote. */
|
|
123
|
+
export interface EmbeddedTemplateVersion extends TemplateVersionFacts, TemplateVersionContent {
|
|
124
|
+
}
|
|
125
|
+
export interface Template {
|
|
126
|
+
object: "template";
|
|
127
|
+
id: string;
|
|
128
|
+
key: string;
|
|
129
|
+
name: string;
|
|
130
|
+
current_version: number;
|
|
131
|
+
archived: boolean;
|
|
132
|
+
created: string;
|
|
133
|
+
/** Present on save, get and update; absent in lists. */
|
|
134
|
+
version?: EmbeddedTemplateVersion;
|
|
135
|
+
/** The changelog, newest first. Present on get. */
|
|
136
|
+
versions?: TemplateVersionEntry[];
|
|
137
|
+
}
|
|
138
|
+
/** A template as returned by save, get, update and promote: always with `version`. */
|
|
139
|
+
export type TemplateWithVersion = Template & {
|
|
140
|
+
version: EmbeddedTemplateVersion;
|
|
141
|
+
};
|
|
142
|
+
export interface TemplateListParams extends ListParams {
|
|
143
|
+
/** Include archived templates. */
|
|
144
|
+
archived?: boolean;
|
|
145
|
+
}
|
|
146
|
+
export interface TemplateSaveParams {
|
|
147
|
+
html: string;
|
|
148
|
+
css?: string;
|
|
149
|
+
/** The inputs declaration, as an object or as the text of `inputs.json`. */
|
|
150
|
+
inputs: TemplateInputs | string;
|
|
151
|
+
name?: string;
|
|
152
|
+
note?: string;
|
|
153
|
+
/** Later versions become current only with `true`. The first version always does. */
|
|
154
|
+
make_current?: boolean;
|
|
155
|
+
}
|
|
156
|
+
export interface TemplateUpdateParams {
|
|
157
|
+
/** Any existing version, forward or back. */
|
|
158
|
+
current_version?: number;
|
|
159
|
+
name?: string;
|
|
160
|
+
archived?: boolean;
|
|
161
|
+
}
|
|
162
|
+
export interface DocumentAssetUse {
|
|
163
|
+
name: string;
|
|
164
|
+
id: string;
|
|
165
|
+
sha256: string;
|
|
166
|
+
source: string;
|
|
167
|
+
}
|
|
168
|
+
export interface Document {
|
|
169
|
+
object: "document";
|
|
170
|
+
id: string;
|
|
171
|
+
status: "ready" | "deleted";
|
|
172
|
+
template: string;
|
|
173
|
+
version: number;
|
|
174
|
+
/** The override operations as sent. */
|
|
175
|
+
overrides: Override[];
|
|
176
|
+
number: string | null;
|
|
177
|
+
pages: number;
|
|
178
|
+
bytes: number;
|
|
179
|
+
sha256: string;
|
|
180
|
+
/** The fingerprint of the template after overrides. */
|
|
181
|
+
template_sha256: string;
|
|
182
|
+
engine: string;
|
|
183
|
+
assets: DocumentAssetUse[];
|
|
184
|
+
metadata: Record<string, string>;
|
|
185
|
+
warnings: Problem[];
|
|
186
|
+
duration_ms: number;
|
|
187
|
+
regenerated_from: string | null;
|
|
188
|
+
/** On a regenerated document: whether its bytes equal the original's. */
|
|
189
|
+
reproduced?: boolean;
|
|
190
|
+
created_by: Actor;
|
|
191
|
+
created: string;
|
|
192
|
+
request_id: string | null;
|
|
193
|
+
/** The PDF, for a request carrying a key. Ready documents only. */
|
|
194
|
+
file_url?: string;
|
|
195
|
+
/** The PDF for anyone holding the URL, for 15 minutes unless `url_ttl` was set. */
|
|
196
|
+
download_url?: string;
|
|
197
|
+
/** One PNG per page, up to 50. Signed like `download_url`. */
|
|
198
|
+
page_images?: string[];
|
|
199
|
+
/** The data as sent. Returned by `documents.get` only. */
|
|
200
|
+
data?: Record<string, unknown>;
|
|
201
|
+
/** Deleted documents only. */
|
|
202
|
+
deleted?: string | null;
|
|
203
|
+
deleted_by?: Actor | null;
|
|
204
|
+
}
|
|
205
|
+
export interface DocumentGenerateParams {
|
|
206
|
+
/** The template's key. */
|
|
207
|
+
template: string;
|
|
208
|
+
/** The data, matching the template's declared fields. */
|
|
209
|
+
data?: Record<string, unknown>;
|
|
210
|
+
/** A version number. Default: the current version. */
|
|
211
|
+
version?: number;
|
|
212
|
+
/** Operations applied to the template, in order, before the data is filled in. */
|
|
213
|
+
overrides?: Override[];
|
|
214
|
+
/** Your document number, up to 120 characters. It names the file and the PDF's title. */
|
|
215
|
+
number?: string;
|
|
216
|
+
/** Up to 20 text labels, for finding the document later. */
|
|
217
|
+
metadata?: Record<string, string>;
|
|
218
|
+
}
|
|
219
|
+
/** The answer of `POST /documents/validate`: every check short of rendering passed. */
|
|
220
|
+
export interface DocumentValidation {
|
|
221
|
+
object: "document_validation";
|
|
222
|
+
valid: true;
|
|
223
|
+
/** The template's key. */
|
|
224
|
+
template: string;
|
|
225
|
+
/** The version the request resolved to. */
|
|
226
|
+
version: number;
|
|
227
|
+
/** The fingerprint of the template after overrides. */
|
|
228
|
+
template_sha256: string;
|
|
229
|
+
/** The regions of the template after overrides. */
|
|
230
|
+
regions: string[];
|
|
231
|
+
warnings: Problem[];
|
|
232
|
+
}
|
|
233
|
+
export interface DocumentListParams extends ListParams {
|
|
234
|
+
template?: string;
|
|
235
|
+
number?: string;
|
|
236
|
+
status?: "ready" | "deleted";
|
|
237
|
+
/** Every label must match. Sent as `metadata[name]=value`. */
|
|
238
|
+
metadata?: Record<string, string>;
|
|
239
|
+
/** ISO 8601 text or a Date. */
|
|
240
|
+
created_after?: string | Date;
|
|
241
|
+
created_before?: string | Date;
|
|
242
|
+
}
|
|
243
|
+
export interface DocumentGetParams {
|
|
244
|
+
/** How long `download_url` and `page_images` work, in seconds: 60 to 86400. Default 900. */
|
|
245
|
+
url_ttl?: number;
|
|
246
|
+
}
|
|
247
|
+
/** Sending `data` or `overrides` replaces that part only. An empty body reproduces the original. */
|
|
248
|
+
export interface DocumentRegenerateParams {
|
|
249
|
+
data?: Record<string, unknown>;
|
|
250
|
+
overrides?: Override[];
|
|
251
|
+
number?: string;
|
|
252
|
+
metadata?: Record<string, string>;
|
|
253
|
+
}
|
|
254
|
+
export interface DocumentPromoteParams {
|
|
255
|
+
make_current?: boolean;
|
|
256
|
+
note?: string;
|
|
257
|
+
/** Use the document's data as the new version's example. */
|
|
258
|
+
example_from_document?: boolean;
|
|
259
|
+
}
|
|
260
|
+
export interface ShareLink {
|
|
261
|
+
object: "share_link";
|
|
262
|
+
id: string;
|
|
263
|
+
document: string;
|
|
264
|
+
status: "active" | "expired" | "revoked" | "suspended";
|
|
265
|
+
/**
|
|
266
|
+
* The public page. Returned by `shareLinks.create` only: once, and again
|
|
267
|
+
* when the same `Idempotency-Key` and body are repeated within 24 hours.
|
|
268
|
+
*/
|
|
269
|
+
url?: string;
|
|
270
|
+
hint: string;
|
|
271
|
+
expires: string;
|
|
272
|
+
views: number;
|
|
273
|
+
created: string;
|
|
274
|
+
created_by: Actor;
|
|
275
|
+
revoked: string | null;
|
|
276
|
+
}
|
|
277
|
+
export interface ShareLinkCreateParams {
|
|
278
|
+
expires_in_days?: number;
|
|
279
|
+
}
|
|
280
|
+
export interface Asset {
|
|
281
|
+
object: "asset";
|
|
282
|
+
id: string;
|
|
283
|
+
name: string;
|
|
284
|
+
sha256: string;
|
|
285
|
+
content_type: string;
|
|
286
|
+
bytes: number;
|
|
287
|
+
created: string;
|
|
288
|
+
created_by: Actor;
|
|
289
|
+
}
|
|
290
|
+
export interface AssetUploadParams {
|
|
291
|
+
/** The image or font: bytes or a Blob. */
|
|
292
|
+
file: Blob | ArrayBuffer | Uint8Array;
|
|
293
|
+
/** Lowercase letters, digits, hyphens and underscores. Templates refer to the file by it. */
|
|
294
|
+
name: string;
|
|
295
|
+
/** The file name sent with the upload. Default: `name`. */
|
|
296
|
+
filename?: string;
|
|
297
|
+
}
|
|
298
|
+
export interface Deleted<T extends string> {
|
|
299
|
+
object: T;
|
|
300
|
+
id: string;
|
|
301
|
+
deleted: true;
|
|
302
|
+
}
|
|
303
|
+
export type EventType = "document.ready" | "document.failed" | "document.deleted" | "template.version_created" | "template.current_changed" | "share_link.created" | "share_link.revoked" | "share_link.reported";
|
|
304
|
+
export interface WebhookEndpoint {
|
|
305
|
+
object: "webhook_endpoint";
|
|
306
|
+
id: string;
|
|
307
|
+
url: string;
|
|
308
|
+
enabled_events: (EventType | "*")[];
|
|
309
|
+
status: "unverified" | "verified" | "disabled";
|
|
310
|
+
/**
|
|
311
|
+
* The signing secret (`whsec_…`). Returned by `webhookEndpoints.create`
|
|
312
|
+
* only: once, and again when the same `Idempotency-Key` and body are
|
|
313
|
+
* repeated within 24 hours.
|
|
314
|
+
*/
|
|
315
|
+
secret?: string;
|
|
316
|
+
created: string;
|
|
317
|
+
created_by: Actor;
|
|
318
|
+
}
|
|
319
|
+
export interface WebhookEndpointCreateParams {
|
|
320
|
+
/** `https`, port 443, a public address. */
|
|
321
|
+
url: string;
|
|
322
|
+
/** Default: `["*"]`. */
|
|
323
|
+
enabled_events?: (EventType | "*")[];
|
|
324
|
+
}
|
|
325
|
+
/** At least one of the two. The signing secret never changes. */
|
|
326
|
+
export interface WebhookEndpointUpdateParams {
|
|
327
|
+
/**
|
|
328
|
+
* `https`, port 443, a public address. A URL that differs from the
|
|
329
|
+
* current one makes the endpoint `unverified` and is sent a new
|
|
330
|
+
* challenge at once.
|
|
331
|
+
*/
|
|
332
|
+
url?: string;
|
|
333
|
+
/** The event types to send, or `["*"]` for all of them. Replaces the list. */
|
|
334
|
+
enabled_events?: (EventType | "*")[];
|
|
335
|
+
}
|
|
336
|
+
export interface EventDelivery {
|
|
337
|
+
endpoint: string;
|
|
338
|
+
status: string;
|
|
339
|
+
attempts: number;
|
|
340
|
+
response_status: number | null;
|
|
341
|
+
error: string | null;
|
|
342
|
+
delivered: string | null;
|
|
343
|
+
next_attempt: string | null;
|
|
344
|
+
}
|
|
345
|
+
export interface Event<T = Record<string, unknown>> {
|
|
346
|
+
object: "event";
|
|
347
|
+
id: string;
|
|
348
|
+
/** One of `EventType`, or `endpoint.verification` for the challenge sent to a new endpoint. */
|
|
349
|
+
type: EventType | "endpoint.verification" | (string & {});
|
|
350
|
+
created: string;
|
|
351
|
+
request_id?: string | null;
|
|
352
|
+
data: T;
|
|
353
|
+
/** Present in `events.list` when the event was sent to an endpoint. */
|
|
354
|
+
deliveries?: EventDelivery[];
|
|
355
|
+
}
|
|
356
|
+
export interface EventListParams extends ListParams {
|
|
357
|
+
type?: EventType;
|
|
358
|
+
/** A document, template or share link id. */
|
|
359
|
+
object?: string;
|
|
360
|
+
}
|
|
361
|
+
export interface Key {
|
|
362
|
+
object: "key";
|
|
363
|
+
id: string;
|
|
364
|
+
name: string;
|
|
365
|
+
hint: string;
|
|
366
|
+
created: string;
|
|
367
|
+
created_by: Actor;
|
|
368
|
+
last_used: string | null;
|
|
369
|
+
revoked: string | null;
|
|
370
|
+
/**
|
|
371
|
+
* The secret key (`zp_sk_…`). Returned by `keys.create` only: once, and
|
|
372
|
+
* again when the same `Idempotency-Key` and body are repeated within 24
|
|
373
|
+
* hours.
|
|
374
|
+
*/
|
|
375
|
+
secret?: string;
|
|
376
|
+
}
|
|
377
|
+
export interface KeyCreateParams {
|
|
378
|
+
name?: string;
|
|
379
|
+
}
|
|
380
|
+
export interface KeyUpdateParams {
|
|
381
|
+
/** The new name, up to 80 characters. */
|
|
382
|
+
name: string;
|
|
383
|
+
}
|
|
384
|
+
export interface Business {
|
|
385
|
+
object: "business";
|
|
386
|
+
id: string;
|
|
387
|
+
name: string;
|
|
388
|
+
plan: "free" | "paid" | (string & {});
|
|
389
|
+
price_usd_per_month: number;
|
|
390
|
+
limits: {
|
|
391
|
+
documents_per_month: number;
|
|
392
|
+
pages_per_document: number;
|
|
393
|
+
retention_days: number | null;
|
|
394
|
+
storage_bytes: number;
|
|
395
|
+
renders_per_minute: number;
|
|
396
|
+
share_link_max_days: number;
|
|
397
|
+
};
|
|
398
|
+
api_url: string;
|
|
399
|
+
suspended: boolean;
|
|
400
|
+
created: string;
|
|
401
|
+
}
|
|
402
|
+
export interface Usage {
|
|
403
|
+
object: "usage";
|
|
404
|
+
plan: string;
|
|
405
|
+
period_start: string;
|
|
406
|
+
period_end: string;
|
|
407
|
+
documents: {
|
|
408
|
+
used: number;
|
|
409
|
+
limit: number;
|
|
410
|
+
};
|
|
411
|
+
pages: {
|
|
412
|
+
used: number;
|
|
413
|
+
};
|
|
414
|
+
storage: {
|
|
415
|
+
used_bytes: number;
|
|
416
|
+
template_bytes: number;
|
|
417
|
+
data_bytes: number;
|
|
418
|
+
limit_bytes: number;
|
|
419
|
+
documents: number;
|
|
420
|
+
assets: number;
|
|
421
|
+
};
|
|
422
|
+
share_links: {
|
|
423
|
+
active: number;
|
|
424
|
+
limit: number;
|
|
425
|
+
};
|
|
426
|
+
renders_per_minute: number;
|
|
427
|
+
}
|
|
428
|
+
/** `free` or `paid`: which limits apply to the business. */
|
|
429
|
+
export type PlanCode = "free" | "paid";
|
|
430
|
+
/**
|
|
431
|
+
* Why the business has its plan.
|
|
432
|
+
*
|
|
433
|
+
* - `complimentary`: the paid allowance at no charge (zpdf is not charging, or the operator granted it).
|
|
434
|
+
* - `free`: the Free limits.
|
|
435
|
+
* - `active`: subscribed and renewing.
|
|
436
|
+
* - `canceling`: subscribed until `current_period_end`, then Free.
|
|
437
|
+
* - `past_due`: the last payment failed. `plan` stays `paid` until `grace_ends`, then is `free` until a payment succeeds.
|
|
438
|
+
*/
|
|
439
|
+
export type BillingState = "complimentary" | "free" | "active" | "canceling" | "past_due";
|
|
440
|
+
/** `GET /billing`: the plan, why, what is used, and what happens next. */
|
|
441
|
+
export interface BillingStatus {
|
|
442
|
+
object: "billing";
|
|
443
|
+
/** Whether zpdf charges. While `false`, every business has the paid allowance at no charge. */
|
|
444
|
+
billing_enabled: boolean;
|
|
445
|
+
plan: PlanCode | (string & {});
|
|
446
|
+
state: BillingState | (string & {});
|
|
447
|
+
/** What this business is charged a month: 10 while subscribed, otherwise 0. */
|
|
448
|
+
price_usd_per_month: number;
|
|
449
|
+
/** Documents made this month (UTC) against the plan's allowance; `resets` is when the count starts again. */
|
|
450
|
+
documents: {
|
|
451
|
+
used: number;
|
|
452
|
+
limit: number;
|
|
453
|
+
resets: string;
|
|
454
|
+
};
|
|
455
|
+
/** `active`: the next renewal. `canceling`: when the paid plan ends. Otherwise null. */
|
|
456
|
+
current_period_end: string | null;
|
|
457
|
+
cancel_at_period_end: boolean;
|
|
458
|
+
/** `past_due` only: when the paid allowance stops unless a payment succeeds. */
|
|
459
|
+
grace_ends: string | null;
|
|
460
|
+
/** After a drop to Free: nothing is deleted by retention before this time. Otherwise null. */
|
|
461
|
+
documents_kept_until: string | null;
|
|
462
|
+
/** Whether `billing.checkout()` returns a link now. */
|
|
463
|
+
upgrade_available: boolean;
|
|
464
|
+
/** Whether the payment provider's page for the card and invoices exists for this business. */
|
|
465
|
+
portal_available: boolean;
|
|
466
|
+
/** The console page where an owner upgrades, cancels or resumes. */
|
|
467
|
+
manage_url: string;
|
|
468
|
+
}
|
|
469
|
+
/** `POST /billing/checkout`: the link an owner opens to move the business to the paid plan. */
|
|
470
|
+
export interface CheckoutLink {
|
|
471
|
+
object: "checkout_link";
|
|
472
|
+
/** Hand this to a person. It opens the console; an owner of the business signs in and pays. */
|
|
473
|
+
url: string;
|
|
474
|
+
business: string;
|
|
475
|
+
plan: "paid";
|
|
476
|
+
price_usd_per_month: number;
|
|
477
|
+
documents_per_month: number;
|
|
478
|
+
opened_by: string;
|
|
479
|
+
}
|
|
480
|
+
/** `owner`: members, keys, everything. `developer`: templates, documents, keys, webhooks. `viewer`: read. */
|
|
481
|
+
export type MemberRole = "owner" | "developer" | "viewer";
|
|
482
|
+
/**
|
|
483
|
+
* A person in the business, or a pending invitation. Inviting, changing a
|
|
484
|
+
* role and removing are done by an owner signed in to the console; a secret
|
|
485
|
+
* key that tries is refused with `owner_session_required`.
|
|
486
|
+
*/
|
|
487
|
+
export interface Member {
|
|
488
|
+
object: "member";
|
|
489
|
+
id: string;
|
|
490
|
+
email: string | null;
|
|
491
|
+
role: MemberRole;
|
|
492
|
+
status: "active" | "invited";
|
|
493
|
+
/** The person whose sign-up created the business. They stay an owner: no one can change or remove them. */
|
|
494
|
+
founder: boolean;
|
|
495
|
+
/** The signed-in member reading the list in the console. Always `false` for a secret key. */
|
|
496
|
+
you: boolean;
|
|
497
|
+
created: string;
|
|
498
|
+
}
|
|
499
|
+
export interface SignupStarted {
|
|
500
|
+
object: "signup";
|
|
501
|
+
email: string;
|
|
502
|
+
status: "code_sent";
|
|
503
|
+
next: string;
|
|
504
|
+
}
|
|
505
|
+
export interface SignupVerifyParams {
|
|
506
|
+
email: string;
|
|
507
|
+
/** The 6-digit code from the email. */
|
|
508
|
+
code: string;
|
|
509
|
+
/** Plain words, up to 60 characters, with no web address. */
|
|
510
|
+
business_name?: string;
|
|
511
|
+
key_name?: string;
|
|
512
|
+
}
|
|
513
|
+
export interface SignupComplete {
|
|
514
|
+
object: "signup";
|
|
515
|
+
status: "complete";
|
|
516
|
+
business: Business;
|
|
517
|
+
/** Shown once. */
|
|
518
|
+
secret_key: string;
|
|
519
|
+
key: Key;
|
|
520
|
+
api_url: string;
|
|
521
|
+
console_url: string;
|
|
522
|
+
next: string;
|
|
523
|
+
}
|
|
524
|
+
export interface ApiInfo {
|
|
525
|
+
object: "api";
|
|
526
|
+
version: string;
|
|
527
|
+
docs: string;
|
|
528
|
+
/** Whether zpdf charges. While `enabled` is false, every business has the paid allowance at no charge. */
|
|
529
|
+
billing: {
|
|
530
|
+
enabled: boolean;
|
|
531
|
+
};
|
|
532
|
+
plans: Record<string, {
|
|
533
|
+
price_usd_per_month: number;
|
|
534
|
+
documents_per_month: number;
|
|
535
|
+
pages_per_document: number;
|
|
536
|
+
retention_days: number | null;
|
|
537
|
+
share_link_max_days: number;
|
|
538
|
+
}>;
|
|
539
|
+
}
|
|
540
|
+
/** What the render service measured about how it is isolated. */
|
|
541
|
+
export interface EngineIsolation {
|
|
542
|
+
/** The browser's own process sandbox is confirmed on its running processes. */
|
|
543
|
+
sandbox: boolean;
|
|
544
|
+
/** The whole browser runs with no network. */
|
|
545
|
+
network_namespace: boolean;
|
|
546
|
+
/** The browser can write only its own scratch directory. */
|
|
547
|
+
read_only_filesystem: boolean;
|
|
548
|
+
/** The browser cannot see the render service's processes. */
|
|
549
|
+
process_namespace: boolean;
|
|
550
|
+
/** Every render gets a browser process of its own. */
|
|
551
|
+
process_per_render: boolean;
|
|
552
|
+
/** Memory, in bytes, a render may add before it is stopped. */
|
|
553
|
+
memory_limit_bytes: number;
|
|
554
|
+
}
|
|
555
|
+
export interface Engine {
|
|
556
|
+
object: "engine";
|
|
557
|
+
/** The browser build and font set that renders documents. */
|
|
558
|
+
id: string;
|
|
559
|
+
/** How the renderer is isolated; null where the engine cannot say. */
|
|
560
|
+
isolation: EngineIsolation | null;
|
|
561
|
+
}
|
|
562
|
+
export interface ExampleSummary {
|
|
563
|
+
object: "example";
|
|
564
|
+
key: string;
|
|
565
|
+
url: string;
|
|
566
|
+
}
|
|
567
|
+
export interface Example {
|
|
568
|
+
object: "example";
|
|
569
|
+
key: string;
|
|
570
|
+
html: string;
|
|
571
|
+
css: string;
|
|
572
|
+
inputs: TemplateInputs;
|
|
573
|
+
}
|
|
574
|
+
export type ExampleFile = "template.html" | "template.css" | "inputs.json" | "data-400-rows.json";
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The zpdf API's objects and request bodies, exactly as they travel: names are
|
|
3
|
+
* the API's own snake_case, so a value read from the SDK, from curl and from
|
|
4
|
+
* the CLI's `--json` output is the same value.
|
|
5
|
+
*
|
|
6
|
+
* Within v1 the API adds fields and codes and never removes or changes one,
|
|
7
|
+
* so every object may carry fields that are not listed here.
|
|
8
|
+
*/
|
|
9
|
+
export {};
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Event } from "./types.js";
|
|
2
|
+
/** A `Headers` object, or a plain record as Node and most frameworks give. */
|
|
3
|
+
export type HeadersInput = {
|
|
4
|
+
get(name: string): string | null;
|
|
5
|
+
} | Record<string, string | string[] | undefined>;
|
|
6
|
+
export interface VerifyWebhookParams {
|
|
7
|
+
/** The request body exactly as received: text or bytes, not a parsed object. */
|
|
8
|
+
body: string | Uint8Array | ArrayBuffer;
|
|
9
|
+
headers: HeadersInput;
|
|
10
|
+
/** The endpoint's signing secret, `whsec_…`. */
|
|
11
|
+
secret: string;
|
|
12
|
+
/** How old a `webhook-timestamp` may be, in seconds. Default 300. */
|
|
13
|
+
toleranceSeconds?: number;
|
|
14
|
+
/** The current time in milliseconds, for tests. */
|
|
15
|
+
now?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The value of the `webhook-signature` header: `v1,` and the base64
|
|
19
|
+
* HMAC-SHA256 of `{id}.{timestamp}.{body}`, keyed with the base64-decoded
|
|
20
|
+
* part of the secret after `whsec_`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function signWebhook(secret: string, id: string, timestamp: number, body: string): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Checks a webhook's signature and age and returns its event. Throws
|
|
25
|
+
* `ZPDFWebhookSignatureError` when anything is off. Treat a repeated
|
|
26
|
+
* `event.id` as already handled.
|
|
27
|
+
*
|
|
28
|
+
* A new endpoint first receives an `endpoint.verification` event: answer 2xx
|
|
29
|
+
* with `event.data.challenge` in the response body.
|
|
30
|
+
*/
|
|
31
|
+
export declare function verifyWebhook<T = Record<string, unknown>>(params: VerifyWebhookParams): Promise<Event<T>>;
|
package/dist/webhooks.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ZPDFWebhookSignatureError } from "./errors.js";
|
|
2
|
+
import { base64ToBytes, bytesToBase64, getWebCrypto } from "./runtime.js";
|
|
3
|
+
function header(headers, name) {
|
|
4
|
+
if (typeof headers.get === "function") {
|
|
5
|
+
return headers.get(name) ?? undefined;
|
|
6
|
+
}
|
|
7
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
8
|
+
if (key.toLowerCase() === name)
|
|
9
|
+
return Array.isArray(value) ? value[0] : value;
|
|
10
|
+
}
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
function bodyText(body) {
|
|
14
|
+
if (typeof body === "string")
|
|
15
|
+
return body;
|
|
16
|
+
return new TextDecoder().decode(body instanceof ArrayBuffer ? new Uint8Array(body) : body);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The value of the `webhook-signature` header: `v1,` and the base64
|
|
20
|
+
* HMAC-SHA256 of `{id}.{timestamp}.{body}`, keyed with the base64-decoded
|
|
21
|
+
* part of the secret after `whsec_`.
|
|
22
|
+
*/
|
|
23
|
+
export async function signWebhook(secret, id, timestamp, body) {
|
|
24
|
+
const crypto = await getWebCrypto();
|
|
25
|
+
const key = await crypto.subtle.importKey("raw", base64ToBytes(secret.replace(/^whsec_/, "")), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
26
|
+
const signature = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(`${id}.${timestamp}.${body}`));
|
|
27
|
+
return `v1,${bytesToBase64(new Uint8Array(signature))}`;
|
|
28
|
+
}
|
|
29
|
+
function constantTimeEqual(a, b) {
|
|
30
|
+
if (a.length !== b.length)
|
|
31
|
+
return false;
|
|
32
|
+
let difference = 0;
|
|
33
|
+
for (let index = 0; index < a.length; index++)
|
|
34
|
+
difference |= a.charCodeAt(index) ^ b.charCodeAt(index);
|
|
35
|
+
return difference === 0;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks a webhook's signature and age and returns its event. Throws
|
|
39
|
+
* `ZPDFWebhookSignatureError` when anything is off. Treat a repeated
|
|
40
|
+
* `event.id` as already handled.
|
|
41
|
+
*
|
|
42
|
+
* A new endpoint first receives an `endpoint.verification` event: answer 2xx
|
|
43
|
+
* with `event.data.challenge` in the response body.
|
|
44
|
+
*/
|
|
45
|
+
export async function verifyWebhook(params) {
|
|
46
|
+
const id = header(params.headers, "webhook-id");
|
|
47
|
+
const timestampText = header(params.headers, "webhook-timestamp");
|
|
48
|
+
const signatures = header(params.headers, "webhook-signature");
|
|
49
|
+
if (id === undefined || timestampText === undefined || signatures === undefined) {
|
|
50
|
+
throw new ZPDFWebhookSignatureError("The webhook-id, webhook-timestamp and webhook-signature headers are required.");
|
|
51
|
+
}
|
|
52
|
+
const timestamp = Number(timestampText);
|
|
53
|
+
if (!Number.isInteger(timestamp)) {
|
|
54
|
+
throw new ZPDFWebhookSignatureError("webhook-timestamp is not a number of seconds.");
|
|
55
|
+
}
|
|
56
|
+
const tolerance = params.toleranceSeconds ?? 300;
|
|
57
|
+
const now = Math.floor((params.now ?? Date.now()) / 1000);
|
|
58
|
+
if (Math.abs(now - timestamp) > tolerance) {
|
|
59
|
+
throw new ZPDFWebhookSignatureError(`webhook-timestamp is more than ${tolerance} seconds from now.`);
|
|
60
|
+
}
|
|
61
|
+
const text = bodyText(params.body);
|
|
62
|
+
const expected = await signWebhook(params.secret, id, timestamp, text);
|
|
63
|
+
const matches = signatures
|
|
64
|
+
.split(/\s+/)
|
|
65
|
+
.filter((candidate) => candidate !== "")
|
|
66
|
+
.some((candidate) => constantTimeEqual(candidate, expected));
|
|
67
|
+
if (!matches) {
|
|
68
|
+
throw new ZPDFWebhookSignatureError("The webhook signature does not match the body and secret.");
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
return JSON.parse(text);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
throw new ZPDFWebhookSignatureError("The webhook body is not JSON.");
|
|
75
|
+
}
|
|
76
|
+
}
|