@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/dist/client.js ADDED
@@ -0,0 +1,474 @@
1
+ import { API_BASE_URL } from "./config.js";
2
+ import { send } from "./http.js";
3
+ import { makeList } from "./pagination.js";
4
+ import { VERSION } from "./version.js";
5
+ /** The zpdf API. Override with `baseUrl` or the `ZPDF_API` environment variable. */
6
+ export const DEFAULT_BASE_URL = API_BASE_URL;
7
+ function environment(name) {
8
+ const env = globalThis.process?.env;
9
+ const value = env?.[name];
10
+ return value === undefined || value === "" ? undefined : value;
11
+ }
12
+ function segment(value) {
13
+ return encodeURIComponent(value);
14
+ }
15
+ function isoDate(value) {
16
+ return value instanceof Date ? value.toISOString() : value;
17
+ }
18
+ /** A paginated `GET`: the first page when awaited, every item when iterated. */
19
+ function list(send, path, params, options, query = () => ({}), auth) {
20
+ return makeList((page) => send({
21
+ method: "GET",
22
+ path,
23
+ query: { ...query(page), limit: page.limit, starting_after: page.starting_after },
24
+ ...(auth === false ? { auth } : {}),
25
+ options,
26
+ }), params);
27
+ }
28
+ /** Create a business and its first key. No key needed. */
29
+ class Signup {
30
+ constructor(send) {
31
+ this.send = send;
32
+ }
33
+ /** `POST /signup`: emails a 6-digit code to the address. */
34
+ start(params, options) {
35
+ return this.send({
36
+ method: "POST",
37
+ path: "/signup",
38
+ body: params,
39
+ auth: false,
40
+ idempotent: false,
41
+ options,
42
+ });
43
+ }
44
+ /** `POST /signup/verify`: trades the code for the business and a secret key, shown once. */
45
+ verify(params, options) {
46
+ return this.send({
47
+ method: "POST",
48
+ path: "/signup/verify",
49
+ body: params,
50
+ auth: false,
51
+ idempotent: false,
52
+ options,
53
+ });
54
+ }
55
+ }
56
+ /** The example templates. No key needed. */
57
+ class Examples {
58
+ constructor(send) {
59
+ this.send = send;
60
+ }
61
+ /** `GET /examples`: by key. Await for one page, or iterate for every example. */
62
+ list(params = {}, options) {
63
+ return list(this.send, "/examples", params, options, undefined, false);
64
+ }
65
+ /** `GET /examples/{key}`: the example's HTML, CSS and inputs in one object. */
66
+ get(key, options) {
67
+ return this.send({ method: "GET", path: `/examples/${segment(key)}`, auth: false, options });
68
+ }
69
+ /** `GET /examples/{key}/{file}`: one of the example's files, as text. */
70
+ file(key, file, options) {
71
+ return this.send({
72
+ method: "GET",
73
+ path: `/examples/${segment(key)}/${segment(file)}`,
74
+ auth: false,
75
+ expect: "text",
76
+ options,
77
+ });
78
+ }
79
+ }
80
+ class Templates {
81
+ constructor(send) {
82
+ this.send = send;
83
+ }
84
+ /**
85
+ * `PUT /templates/{key}`: saves a new version, checks it and renders its
86
+ * example. Identical content returns the existing version. If anything is
87
+ * wrong nothing is saved, and the error's `problems` lists every fault.
88
+ * A field the API does not take is refused with `parameter_unknown`.
89
+ */
90
+ save(key, params, options) {
91
+ return this.send({ method: "PUT", path: `/templates/${segment(key)}`, body: params, options });
92
+ }
93
+ /** `GET /templates`: by key. Await for one page, or iterate for every template. */
94
+ list(params = {}, options) {
95
+ return list(this.send, "/templates", params, options, (page) => ({
96
+ archived: page.archived === true ? true : undefined,
97
+ }));
98
+ }
99
+ /** `GET /templates/{key}`: the current version (or `version`) with files, inputs, regions and the changelog. */
100
+ get(key, params = {}, options) {
101
+ return this.send({
102
+ method: "GET",
103
+ path: `/templates/${segment(key)}`,
104
+ query: { version: params.version },
105
+ options,
106
+ });
107
+ }
108
+ /** `PATCH /templates/{key}`: move `current_version`, rename, archive. */
109
+ update(key, params, options) {
110
+ return this.send({ method: "PATCH", path: `/templates/${segment(key)}`, body: params, options });
111
+ }
112
+ }
113
+ /** The versions of a template: each save and each promote makes one, and none ever changes. */
114
+ class TemplateVersions {
115
+ constructor(send) {
116
+ this.send = send;
117
+ }
118
+ /** `GET /templates/{key}/versions`: summaries, newest first. Await for one page, or iterate for every version. */
119
+ list(key, params = {}, options) {
120
+ return list(this.send, `/templates/${segment(key)}/versions`, params, options);
121
+ }
122
+ /** `GET /templates/{key}/versions/{number}`: one version in full, with its files, inputs, regions and example. */
123
+ get(key, number, options) {
124
+ return this.send({
125
+ method: "GET",
126
+ path: `/templates/${segment(key)}/versions/${segment(String(number))}`,
127
+ options,
128
+ });
129
+ }
130
+ }
131
+ class Documents {
132
+ constructor(send, fetchBytes) {
133
+ this.send = send;
134
+ this.fetchBytes = fetchBytes;
135
+ }
136
+ /**
137
+ * `POST /documents`: generates a PDF from a template and data, with
138
+ * optional overrides. Answers when the PDF is stored.
139
+ */
140
+ generate(params, options) {
141
+ return this.send({ method: "POST", path: "/documents", body: params, options });
142
+ }
143
+ /**
144
+ * `POST /documents/validate`: every check short of rendering, on the body
145
+ * `generate` takes. Makes no document and counts against no limit. A
146
+ * request that would fail throws the error `generate` would throw.
147
+ */
148
+ validate(params, options) {
149
+ return this.send({
150
+ method: "POST",
151
+ path: "/documents/validate",
152
+ body: params,
153
+ readOnly: true,
154
+ options,
155
+ });
156
+ }
157
+ /** `GET /documents`: newest first. Await for one page, or iterate for every document. */
158
+ list(params = {}, options) {
159
+ return list(this.send, "/documents", params, options, (page) => {
160
+ const query = {
161
+ template: page.template,
162
+ number: page.number,
163
+ status: page.status,
164
+ created_after: isoDate(page.created_after),
165
+ created_before: isoDate(page.created_before),
166
+ };
167
+ for (const [name, value] of Object.entries(page.metadata ?? {})) {
168
+ query[`metadata[${name}]`] = value;
169
+ }
170
+ return query;
171
+ });
172
+ }
173
+ /** `GET /documents/{id}`: the record with its data and fresh URLs. */
174
+ get(id, params = {}, options) {
175
+ return this.send({
176
+ method: "GET",
177
+ path: `/documents/${segment(id)}`,
178
+ query: { url_ttl: params.url_ttl },
179
+ options,
180
+ });
181
+ }
182
+ /** `GET /documents/{id}/file`: the PDF's bytes. */
183
+ download(id, options) {
184
+ return this.send({
185
+ method: "GET",
186
+ path: `/documents/${segment(id)}/file`,
187
+ expect: "bytes",
188
+ options,
189
+ });
190
+ }
191
+ /** The PNG of one page (1-based), read from the document's signed `page_images` URL. */
192
+ async pageImage(document, page = 1, options) {
193
+ const url = document.page_images?.[page - 1];
194
+ if (url === undefined) {
195
+ throw new RangeError(`The document has no image for page ${page}.`);
196
+ }
197
+ return this.fetchBytes(url, options);
198
+ }
199
+ /** `DELETE /documents/{id}`: erases the PDF, images, data and overrides. A stub remains. */
200
+ delete(id, options) {
201
+ return this.send({ method: "DELETE", path: `/documents/${segment(id)}`, options });
202
+ }
203
+ /**
204
+ * `POST /documents/{id}/regenerate`: renders again from the record and
205
+ * returns a new document. With no params the bytes reproduce the original
206
+ * (`reproduced: true`); `data` or `overrides` replace that part only.
207
+ */
208
+ regenerate(id, params = {}, options) {
209
+ return this.send({
210
+ method: "POST",
211
+ path: `/documents/${segment(id)}/regenerate`,
212
+ body: params,
213
+ options,
214
+ });
215
+ }
216
+ /** `POST /documents/{id}/promote`: saves the document's template (version plus overrides) as the next version. */
217
+ promote(id, params = {}, options) {
218
+ return this.send({
219
+ method: "POST",
220
+ path: `/documents/${segment(id)}/promote`,
221
+ body: params,
222
+ options,
223
+ });
224
+ }
225
+ }
226
+ class ShareLinks {
227
+ constructor(send) {
228
+ this.send = send;
229
+ }
230
+ /**
231
+ * `POST /documents/{id}/share_links`: a public page for one PDF. `url` is
232
+ * returned here only. Repeating the call with the same `idempotencyKey`
233
+ * and params within 24 hours returns the same link with its `url`.
234
+ */
235
+ create(documentId, params = {}, options) {
236
+ return this.send({
237
+ method: "POST",
238
+ path: `/documents/${segment(documentId)}/share_links`,
239
+ body: params,
240
+ options,
241
+ });
242
+ }
243
+ /** `GET /documents/{id}/share_links`: newest first. Await for one page, or iterate for every link. */
244
+ list(documentId, params = {}, options) {
245
+ return list(this.send, `/documents/${segment(documentId)}/share_links`, params, options);
246
+ }
247
+ /** `DELETE /share_links/{id}`: the link stops working at once. */
248
+ revoke(id, options) {
249
+ return this.send({ method: "DELETE", path: `/share_links/${segment(id)}`, options });
250
+ }
251
+ }
252
+ class Assets {
253
+ constructor(send) {
254
+ this.send = send;
255
+ }
256
+ /** `POST /assets`: stores an image or font under a name. The same bytes return the same asset. */
257
+ upload(params, options) {
258
+ const form = new FormData();
259
+ const blob = params.file instanceof Blob ? params.file : new Blob([params.file]);
260
+ form.append("file", blob, params.filename ?? params.name);
261
+ form.append("name", params.name);
262
+ return this.send({ method: "POST", path: "/assets", form, options });
263
+ }
264
+ /** `GET /assets`: newest first. Await for one page, or iterate for every asset. */
265
+ list(params = {}, options) {
266
+ return list(this.send, "/assets", params, options);
267
+ }
268
+ /** `GET /assets/{id or name}` */
269
+ get(idOrName, options) {
270
+ return this.send({ method: "GET", path: `/assets/${segment(idOrName)}`, options });
271
+ }
272
+ /** `DELETE /assets/{id or name}`. Refused with `asset_in_use` while a current version uses it. */
273
+ delete(idOrName, options) {
274
+ return this.send({ method: "DELETE", path: `/assets/${segment(idOrName)}`, options });
275
+ }
276
+ }
277
+ class WebhookEndpoints {
278
+ constructor(send) {
279
+ this.send = send;
280
+ }
281
+ /**
282
+ * `POST /webhook_endpoints`: registers the business's endpoint and sends
283
+ * it an `endpoint.verification` challenge. `secret` is returned here
284
+ * only. Repeating the call with the same `idempotencyKey` and params
285
+ * within 24 hours returns the same endpoint with its `secret`.
286
+ */
287
+ create(params, options) {
288
+ return this.send({ method: "POST", path: "/webhook_endpoints", body: params, options });
289
+ }
290
+ /** `GET /webhook_endpoints`: newest first. Await for one page, or iterate for every endpoint. */
291
+ list(params = {}, options) {
292
+ return list(this.send, "/webhook_endpoints", params, options);
293
+ }
294
+ /** `POST /webhook_endpoints/{id}/verify`: sends a new challenge. */
295
+ verify(id, options) {
296
+ return this.send({
297
+ method: "POST",
298
+ path: `/webhook_endpoints/${segment(id)}/verify`,
299
+ options,
300
+ });
301
+ }
302
+ /**
303
+ * `PATCH /webhook_endpoints/{id}`: changes the URL, the events sent, or
304
+ * both. The signing secret stays. A new URL makes the endpoint
305
+ * `unverified` and is sent a challenge at once; `status` in the answer
306
+ * says whether it echoed it. Changing the events alone sends nothing.
307
+ */
308
+ update(id, params, options) {
309
+ return this.send({
310
+ method: "PATCH",
311
+ path: `/webhook_endpoints/${segment(id)}`,
312
+ body: params,
313
+ options,
314
+ });
315
+ }
316
+ /** `DELETE /webhook_endpoints/{id}` */
317
+ delete(id, options) {
318
+ return this.send({ method: "DELETE", path: `/webhook_endpoints/${segment(id)}`, options });
319
+ }
320
+ }
321
+ class Events {
322
+ constructor(send) {
323
+ this.send = send;
324
+ }
325
+ /** `GET /events`: the log, newest first. Await for one page, or iterate for every event. */
326
+ list(params = {}, options) {
327
+ return list(this.send, "/events", params, options, (page) => ({
328
+ type: page.type,
329
+ object: page.object,
330
+ }));
331
+ }
332
+ }
333
+ class Keys {
334
+ constructor(send) {
335
+ this.send = send;
336
+ }
337
+ /** `GET /keys`: newest first, never with secrets. Await for one page, or iterate for every key. */
338
+ list(params = {}, options) {
339
+ return list(this.send, "/keys", params, options);
340
+ }
341
+ /**
342
+ * `POST /keys`: a new secret key. `secret` is returned here only.
343
+ * Repeating the call with the same `idempotencyKey` and params within 24
344
+ * hours returns the same key with its `secret`.
345
+ */
346
+ create(params = {}, options) {
347
+ return this.send({ method: "POST", path: "/keys", body: params, options });
348
+ }
349
+ /** `PATCH /keys/{id}`: renames an active key. Its secret does not change and is not returned. */
350
+ update(id, params, options) {
351
+ return this.send({ method: "PATCH", path: `/keys/${segment(id)}`, body: params, options });
352
+ }
353
+ /** `DELETE /keys/{id}`: the key stops working at once. */
354
+ revoke(id, options) {
355
+ return this.send({ method: "DELETE", path: `/keys/${segment(id)}`, options });
356
+ }
357
+ }
358
+ class BusinessResource {
359
+ constructor(send) {
360
+ this.send = send;
361
+ }
362
+ /** `GET /business`: the business, its plan and limits. */
363
+ get(options) {
364
+ return this.send({ method: "GET", path: "/business", options });
365
+ }
366
+ }
367
+ class UsageResource {
368
+ constructor(send) {
369
+ this.send = send;
370
+ }
371
+ /** `GET /usage`: this month's documents, pages, storage and share links. */
372
+ get(options) {
373
+ return this.send({ method: "GET", path: "/usage", options });
374
+ }
375
+ }
376
+ class Billing {
377
+ constructor(send) {
378
+ this.send = send;
379
+ }
380
+ /**
381
+ * `GET /billing`: the plan status. The plan and why (`state`), this
382
+ * month's documents against the allowance and when the count resets,
383
+ * the paid period's end, whether a cancellation is pending, and whether
384
+ * zpdf is charging at all (`billing_enabled`).
385
+ */
386
+ get(options) {
387
+ return this.send({ method: "GET", path: "/billing", options });
388
+ }
389
+ /**
390
+ * `POST /billing/checkout`: the link an owner of the business opens to
391
+ * move it to the paid plan. The link carries no authority: it opens the
392
+ * console, where the owner signs in and pays. Answers
393
+ * `billing_unavailable` while zpdf is not charging, `plan_already_paid`
394
+ * for a business on the paid plan and `payment_past_due` for one whose
395
+ * payment is failing.
396
+ *
397
+ * Cancelling and resuming are done by an owner signed in to the console
398
+ * (`manage_url` in the plan status); the API refuses a secret key on
399
+ * those with `owner_session_required`.
400
+ */
401
+ checkout(options) {
402
+ return this.send({ method: "POST", path: "/billing/checkout", options });
403
+ }
404
+ }
405
+ class Members {
406
+ constructor(send) {
407
+ this.send = send;
408
+ }
409
+ /**
410
+ * `GET /members`: active members oldest first, then pending invitations.
411
+ * Await for one page, or iterate for every member.
412
+ *
413
+ * Inviting, changing a role and removing a member are done by an owner
414
+ * signed in to the console. The API refuses a secret key on those three
415
+ * operations with `owner_session_required`.
416
+ */
417
+ list(params = {}, options) {
418
+ return list(this.send, "/members", params, options);
419
+ }
420
+ }
421
+ /**
422
+ * The zpdf client.
423
+ *
424
+ * const zpdf = new ZPDF({ apiKey: process.env.ZPDF_API_KEY })
425
+ * const document = await zpdf.documents.generate({ template: "invoice", data })
426
+ * const pdf = await zpdf.documents.download(document.id)
427
+ */
428
+ export class ZPDF {
429
+ constructor(options = {}) {
430
+ const fetchImplementation = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
431
+ this.baseUrl = (options.baseUrl ?? environment("ZPDF_API") ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
432
+ this.config = {
433
+ apiKey: options.apiKey ?? environment("ZPDF_API_KEY") ?? environment("ZPDF_SECRET_KEY"),
434
+ baseUrl: this.baseUrl,
435
+ fetch: fetchImplementation,
436
+ maxRetries: options.maxRetries ?? 2,
437
+ timeoutMs: options.timeoutMs ?? 60000,
438
+ // Browsers and some edge runtimes refuse a user-agent header; send it only where there is a process.
439
+ userAgent: environment("PATH") !== undefined ? `zpdf-sdk-node/${VERSION}` : undefined,
440
+ };
441
+ const request = (input) => send(this.config, input);
442
+ this.signup = new Signup(request);
443
+ this.examples = new Examples(request);
444
+ this.templates = new Templates(request);
445
+ this.templateVersions = new TemplateVersions(request);
446
+ this.documents = new Documents(request, (url, requestOptions) => this.fetchFile(url, requestOptions));
447
+ this.shareLinks = new ShareLinks(request);
448
+ this.assets = new Assets(request);
449
+ this.webhookEndpoints = new WebhookEndpoints(request);
450
+ this.events = new Events(request);
451
+ this.keys = new Keys(request);
452
+ this.business = new BusinessResource(request);
453
+ this.usage = new UsageResource(request);
454
+ this.billing = new Billing(request);
455
+ this.members = new Members(request);
456
+ }
457
+ /** `GET /`: the API's version, docs address and plans. No key needed. */
458
+ info(options) {
459
+ return send(this.config, { method: "GET", path: "", auth: false, options });
460
+ }
461
+ /** `GET /engine`: the browser build and font set that renders documents. No key needed. */
462
+ engine(options) {
463
+ return send(this.config, { method: "GET", path: "/engine", auth: false, options });
464
+ }
465
+ /**
466
+ * Reads a signed URL (`download_url`, a `page_images` entry, a share
467
+ * link's `/file`) to bytes. The key is not sent: the URL is the permission.
468
+ * A URL past its time throws `ZPDFError` `url_expired`; a share link that
469
+ * is expired, revoked or unknown throws `share_link_unavailable`.
470
+ */
471
+ async fetchFile(url, options) {
472
+ return send({ ...this.config, baseUrl: "" }, { method: "GET", path: url, auth: false, expect: "bytes", options });
473
+ }
474
+ }
@@ -0,0 +1,4 @@
1
+ /** The zpdf API. Override with `baseUrl` or the `ZPDF_API` environment variable. */
2
+ export declare const API_BASE_URL = "https://api.zpdf.ai/v1";
3
+ /** The documentation. */
4
+ export declare const DOCS_URL = "https://zpdf.ai/docs";
package/dist/config.js ADDED
@@ -0,0 +1,5 @@
1
+ // Generated by scripts/zpdf-build-docs.mjs from zpdf.config.json. Do not edit.
2
+ /** The zpdf API. Override with `baseUrl` or the `ZPDF_API` environment variable. */
3
+ export const API_BASE_URL = "https://api.zpdf.ai/v1";
4
+ /** The documentation. */
5
+ export const DOCS_URL = "https://zpdf.ai/docs";
@@ -0,0 +1,57 @@
1
+ import type { Problem } from "./types.js";
2
+ export type ZPDFErrorType = "invalid_request_error" | "authentication_error" | "permission_error" | "limit_error" | "api_error" | (string & {});
3
+ /**
4
+ * An error answered by the API. Branch on `code`; quote `requestId` to
5
+ * support. Messages name paths and never repeat values.
6
+ *
7
+ * Codes worth a branch of their own:
8
+ * - `data_invalid`, `override_region_missing`: the request; see `problems` and `details`.
9
+ * - `parameter_invalid`, `parameter_unknown`: `param` names the field (a field
10
+ * `templates.save` does not take is `parameter_unknown`).
11
+ * - `cursor_invalid`: `starting_after` is not an item of that list.
12
+ * - `owner_session_required`: inviting, changing and removing members is for
13
+ * an owner signed in to the console, not for a secret key.
14
+ * - `member_is_founder`, `member_is_self`: a member who cannot be changed or removed.
15
+ * - `url_expired`: a signed `download_url` or page image is past its time.
16
+ * - `share_link_unavailable`: a share link is expired, revoked, reported or unknown.
17
+ * - `rate_limited`: wait `retryAfterSeconds`.
18
+ */
19
+ export declare class ZPDFError extends Error {
20
+ /** The HTTP status. */
21
+ readonly status: number;
22
+ readonly type: ZPDFErrorType;
23
+ /** The stable code to branch on, for example `data_invalid` or `override_region_missing`. */
24
+ readonly code: string;
25
+ /** The request field at fault, for example `overrides[2].region`. */
26
+ readonly param: string | undefined;
27
+ /** Code-specific facts, for example `{ regions, did_you_mean }` or `{ problems }`. */
28
+ readonly details: Record<string, unknown> | undefined;
29
+ /** The `request_id` of the failed request. */
30
+ readonly requestId: string | undefined;
31
+ readonly docUrl: string | undefined;
32
+ /** Seconds to wait, from `Retry-After` on a 429. */
33
+ readonly retryAfterSeconds: number | undefined;
34
+ constructor(input: {
35
+ status: number;
36
+ type: ZPDFErrorType;
37
+ code: string;
38
+ message: string;
39
+ param?: string;
40
+ details?: Record<string, unknown>;
41
+ requestId?: string;
42
+ docUrl?: string;
43
+ retryAfterSeconds?: number;
44
+ });
45
+ /** Every fault listed in `details.problems` (template, override and data errors carry one entry per fault). */
46
+ get problems(): Problem[];
47
+ }
48
+ /** The request never got an answer: DNS, connection, timeout or a non-JSON reply from something in between. */
49
+ export declare class ZPDFConnectionError extends Error {
50
+ constructor(message: string, options?: {
51
+ cause?: unknown;
52
+ });
53
+ }
54
+ /** A webhook whose signature, timestamp or headers did not check out. Do not act on it. */
55
+ export declare class ZPDFWebhookSignatureError extends Error {
56
+ constructor(message: string);
57
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * An error answered by the API. Branch on `code`; quote `requestId` to
3
+ * support. Messages name paths and never repeat values.
4
+ *
5
+ * Codes worth a branch of their own:
6
+ * - `data_invalid`, `override_region_missing`: the request; see `problems` and `details`.
7
+ * - `parameter_invalid`, `parameter_unknown`: `param` names the field (a field
8
+ * `templates.save` does not take is `parameter_unknown`).
9
+ * - `cursor_invalid`: `starting_after` is not an item of that list.
10
+ * - `owner_session_required`: inviting, changing and removing members is for
11
+ * an owner signed in to the console, not for a secret key.
12
+ * - `member_is_founder`, `member_is_self`: a member who cannot be changed or removed.
13
+ * - `url_expired`: a signed `download_url` or page image is past its time.
14
+ * - `share_link_unavailable`: a share link is expired, revoked, reported or unknown.
15
+ * - `rate_limited`: wait `retryAfterSeconds`.
16
+ */
17
+ export class ZPDFError extends Error {
18
+ constructor(input) {
19
+ super(input.message);
20
+ this.name = "ZPDFError";
21
+ this.status = input.status;
22
+ this.type = input.type;
23
+ this.code = input.code;
24
+ this.param = input.param;
25
+ this.details = input.details;
26
+ this.requestId = input.requestId;
27
+ this.docUrl = input.docUrl;
28
+ this.retryAfterSeconds = input.retryAfterSeconds;
29
+ }
30
+ /** Every fault listed in `details.problems` (template, override and data errors carry one entry per fault). */
31
+ get problems() {
32
+ const problems = this.details?.problems;
33
+ return Array.isArray(problems) ? problems : [];
34
+ }
35
+ }
36
+ /** The request never got an answer: DNS, connection, timeout or a non-JSON reply from something in between. */
37
+ export class ZPDFConnectionError extends Error {
38
+ constructor(message, options) {
39
+ super(message);
40
+ this.name = "ZPDFConnectionError";
41
+ if (options?.cause !== undefined)
42
+ this.cause = options.cause;
43
+ }
44
+ }
45
+ /** A webhook whose signature, timestamp or headers did not check out. Do not act on it. */
46
+ export class ZPDFWebhookSignatureError extends Error {
47
+ constructor(message) {
48
+ super(message);
49
+ this.name = "ZPDFWebhookSignatureError";
50
+ }
51
+ }
package/dist/http.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
2
+ export interface RequestOptions {
3
+ /**
4
+ * `Idempotency-Key` for a POST. The same key with the same body within 24
5
+ * hours returns the first result, including a new key's `secret`, a
6
+ * webhook endpoint's `secret` and a share link's `url`. Every POST gets a
7
+ * generated key unless you supply one, so a retry cannot make a duplicate
8
+ * and a lost response does not lose its secret.
9
+ */
10
+ idempotencyKey?: string;
11
+ signal?: AbortSignal;
12
+ /** Per attempt. Default: the client's `timeoutMs`. */
13
+ timeoutMs?: number;
14
+ }
15
+ export interface HttpConfig {
16
+ apiKey: string | undefined;
17
+ baseUrl: string;
18
+ fetch: FetchLike;
19
+ maxRetries: number;
20
+ timeoutMs: number;
21
+ userAgent: string | undefined;
22
+ }
23
+ export interface HttpRequest {
24
+ method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
25
+ path: string;
26
+ query?: Record<string, string | number | boolean | undefined>;
27
+ body?: unknown;
28
+ form?: FormData;
29
+ /** False for the operations that need no key. */
30
+ auth?: boolean;
31
+ /** False for a POST the API does not replay (sign-up): no `Idempotency-Key`, sent once. */
32
+ idempotent?: boolean;
33
+ /** True for a POST that changes nothing (validate): no `Idempotency-Key`, retried like a GET. */
34
+ readOnly?: boolean;
35
+ expect?: "json" | "bytes" | "text";
36
+ options?: RequestOptions;
37
+ }
38
+ export declare function buildUrl(baseUrl: string, path: string, query: HttpRequest["query"]): string;
39
+ export declare function send<T>(config: HttpConfig, request: HttpRequest): Promise<T>;