@pageweaver/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +205 -0
- package/dist/client.d.ts +34 -0
- package/dist/client.js +32 -0
- package/dist/client.js.map +1 -0
- package/dist/documents.d.ts +62 -0
- package/dist/documents.js +141 -0
- package/dist/documents.js.map +1 -0
- package/dist/errors.d.ts +43 -0
- package/dist/errors.js +70 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +41 -0
- package/dist/http.js +162 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/schemas.d.ts +19 -0
- package/dist/schemas.js +30 -0
- package/dist/schemas.js.map +1 -0
- package/dist/templates.d.ts +13 -0
- package/dist/templates.js +24 -0
- package/dist/templates.js.map +1 -0
- package/dist/types.d.ts +270 -0
- package/dist/types.js +11 -0
- package/dist/types.js.map +1 -0
- package/dist/usage.d.ts +9 -0
- package/dist/usage.js +16 -0
- package/dist/usage.js.map +1 -0
- package/dist/webhooks.d.ts +72 -0
- package/dist/webhooks.js +65 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PageWeaver
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# @pageweaver/sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for the [PageWeaver](https://pageweaver.io) PDF generation API. Create PDFs from versioned templates and typed JSON payloads (or from inline HTML), then poll or receive a webhook for the result.
|
|
4
|
+
|
|
5
|
+
Works in any server-side JavaScript runtime with a global `fetch` (Node 18+, Bun, Deno, edge runtimes). Fully typed, zero runtime dependencies.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @pageweaver/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { PageWeaver } from "@pageweaver/sdk";
|
|
17
|
+
|
|
18
|
+
const pw = new PageWeaver({ apiKey: process.env.PAGEWEAVER_API_KEY! });
|
|
19
|
+
|
|
20
|
+
// Create a document and wait for it to finish rendering.
|
|
21
|
+
const doc = await pw.documents.createAndWait({
|
|
22
|
+
templateId: "tmpl_invoice",
|
|
23
|
+
payload: { invoiceNumber: "1042", total: 4200 },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Download the finished PDF.
|
|
27
|
+
const pdf = await pw.documents.download(doc.id);
|
|
28
|
+
await fs.writeFile("invoice.pdf", pdf);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`createAndWait` submits the render, polls until it is done, and resolves with the finished document. If you would rather manage polling yourself, use `create` then `get` (or `waitFor`).
|
|
32
|
+
|
|
33
|
+
## Configuration
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const pw = new PageWeaver({
|
|
37
|
+
apiKey: "pk_live_...", // required
|
|
38
|
+
baseUrl: "https://api.pageweaver.io", // default; use http://localhost:4000 for local dev
|
|
39
|
+
timeoutMs: 30000, // per-request timeout
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Your API key comes from the portal (`pk_test_...` for development, `pk_live_...` for production).
|
|
44
|
+
|
|
45
|
+
## Creating documents
|
|
46
|
+
|
|
47
|
+
### From a template
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const doc = await pw.documents.create({
|
|
51
|
+
templateId: "tmpl_invoice",
|
|
52
|
+
payload: { invoiceNumber: "1042", lineItems: [{ name: "Widget", qty: 3 }] },
|
|
53
|
+
version: 4, // optional: pin a published version so future edits never change this output
|
|
54
|
+
});
|
|
55
|
+
// { id, status: "queued", version }
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The payload is validated against the template's JSON Schema before the render is queued. A validation failure throws a `PageWeaverApiError` (status 400) whose `errors` field lists what was wrong.
|
|
59
|
+
|
|
60
|
+
### From inline HTML
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const doc = await pw.documents.create({
|
|
64
|
+
html: "<h1>Hello {{ name }}</h1>",
|
|
65
|
+
css: "h1 { color: #4f46e5 }",
|
|
66
|
+
payload: { name: "Ada" },
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Inline HTML may use Liquid tokens and reference your uploaded image assets by name. External images, stylesheets, and JavaScript are rejected.
|
|
71
|
+
|
|
72
|
+
### Per-render options
|
|
73
|
+
|
|
74
|
+
Every document property is overridable per request under the single `options` key, layered on the template's saved settings for that render only:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
await pw.documents.create({
|
|
78
|
+
templateId: "tmpl_invoice",
|
|
79
|
+
payload,
|
|
80
|
+
options: {
|
|
81
|
+
page: { size: "A4", orientation: "portrait", margin: "18mm" },
|
|
82
|
+
metadata: { title: "Invoice #1042", author: "Acme Inc." },
|
|
83
|
+
footer: { center: "Page {{@page}} of {{@pages}}" },
|
|
84
|
+
watermark: { text: "DRAFT", opacity: 0.3 },
|
|
85
|
+
localization: { locale: "de-DE", timeZone: "Europe/Berlin", currency: "EUR" },
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Polling
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const created = await pw.documents.create({ templateId: "t", payload });
|
|
94
|
+
|
|
95
|
+
// Block until terminal (done or failed), with backoff and a timeout.
|
|
96
|
+
const done = await pw.documents.waitFor(created.id, {
|
|
97
|
+
intervalMs: 1000,
|
|
98
|
+
timeoutMs: 60000,
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`waitFor` throws `PageWeaverDocumentFailedError` if the document fails. Pass `throwOnFailure: false` to receive the failed document instead.
|
|
103
|
+
|
|
104
|
+
## Downloading
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
// Unprotected document: the signed URL is resolved and fetched for you.
|
|
108
|
+
const pdf = await pw.documents.download(doc.id);
|
|
109
|
+
|
|
110
|
+
// Download-protected document: supply the download password.
|
|
111
|
+
const pdf = await pw.documents.download(doc.id, { password: "the-download-password" });
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`download` returns a `Uint8Array`. For a protected document created with `options.security.download`, the response to `create`/`get` includes the generated password in `download.password` (visible only to you, the owner).
|
|
115
|
+
|
|
116
|
+
## Listing history
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
const page = await pw.documents.list({ status: "done", limit: 50 });
|
|
120
|
+
// { items, nextCursor }
|
|
121
|
+
|
|
122
|
+
// Or iterate every page automatically:
|
|
123
|
+
for await (const item of pw.documents.listAll({ status: "failed" })) {
|
|
124
|
+
console.log(item.id, item.error);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Regenerate
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
const replay = await pw.documents.regenerate(doc.id);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Faithfully replays a prior document with the same version, payload, and options. Returns a new document id.
|
|
135
|
+
|
|
136
|
+
## Discovery (read only)
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
await pw.templates.list();
|
|
140
|
+
await pw.templates.get("tmpl_invoice");
|
|
141
|
+
await pw.templates.versions("tmpl_invoice");
|
|
142
|
+
|
|
143
|
+
await pw.schemas.list();
|
|
144
|
+
await pw.schemas.get("schema_id", { version: 2 });
|
|
145
|
+
|
|
146
|
+
await pw.usage.get(); // page consumption vs. your plan quota this period
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Webhooks
|
|
150
|
+
|
|
151
|
+
Set `callbackUrl` on a create call (or a default in the portal) to receive a signed POST when a document reaches a terminal state. Verify the signature before trusting the payload:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { verifyWebhook, isDocumentEvent } from "@pageweaver/sdk";
|
|
155
|
+
|
|
156
|
+
// Express example. Give the verifier the RAW request body bytes.
|
|
157
|
+
app.post("/webhooks/pageweaver", express.raw({ type: "application/json" }), (req, res) => {
|
|
158
|
+
try {
|
|
159
|
+
const event = verifyWebhook({
|
|
160
|
+
secret: process.env.PAGEWEAVER_WEBHOOK_SECRET!,
|
|
161
|
+
body: req.body.toString("utf8"),
|
|
162
|
+
signature: req.headers["x-pageweaver-signature"],
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
if (isDocumentEvent(event) && event.status === "done") {
|
|
166
|
+
console.log("Document ready:", event.documentId, event.url);
|
|
167
|
+
}
|
|
168
|
+
res.sendStatus(200);
|
|
169
|
+
} catch {
|
|
170
|
+
res.sendStatus(400); // invalid signature
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Always verify against the unparsed body. Re-serializing a parsed object can change the bytes and break the signature.
|
|
176
|
+
|
|
177
|
+
## Errors
|
|
178
|
+
|
|
179
|
+
Every error extends `PageWeaverError`:
|
|
180
|
+
|
|
181
|
+
| Class | Thrown when |
|
|
182
|
+
| --- | --- |
|
|
183
|
+
| `PageWeaverApiError` | The API returned a non-2xx response. Carries `status`, `code`, `errors`, `body`. |
|
|
184
|
+
| `PageWeaverConnectionError` | A network failure, or the request timed out. |
|
|
185
|
+
| `PageWeaverTimeoutError` | `waitFor` exceeded its timeout before the document finished. |
|
|
186
|
+
| `PageWeaverDocumentFailedError` | The document reached the `failed` state while waiting. Carries the `document`. |
|
|
187
|
+
| `PageWeaverWebhookSignatureError` | A webhook signature did not match the body. |
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
import { PageWeaverApiError } from "@pageweaver/sdk";
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
await pw.documents.create({ templateId: "t", payload });
|
|
194
|
+
} catch (err) {
|
|
195
|
+
if (err instanceof PageWeaverApiError && err.status === 400) {
|
|
196
|
+
console.error("Validation failed:", err.errors);
|
|
197
|
+
} else {
|
|
198
|
+
throw err;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
MIT
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type FetchLike } from "./http";
|
|
2
|
+
import { DocumentsResource } from "./documents";
|
|
3
|
+
import { TemplatesResource } from "./templates";
|
|
4
|
+
import { SchemasResource } from "./schemas";
|
|
5
|
+
import { UsageResource } from "./usage";
|
|
6
|
+
export interface PageWeaverOptions {
|
|
7
|
+
/** Your secret API key: `pk_live_...` in production, `pk_test_...` in development. */
|
|
8
|
+
apiKey: string;
|
|
9
|
+
/**
|
|
10
|
+
* API base URL. Defaults to `https://api.pageweaver.io`. Point it at `http://localhost:4000`
|
|
11
|
+
* when developing against a local stack.
|
|
12
|
+
*/
|
|
13
|
+
baseUrl?: string;
|
|
14
|
+
/** Per-request timeout in milliseconds. Default 30000. */
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
/** Provide a custom `fetch` (defaults to the global one; requires Node 18+ otherwise). */
|
|
17
|
+
fetch?: FetchLike;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The PageWeaver API client.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* const pw = new PageWeaver({ apiKey: process.env.PAGEWEAVER_API_KEY! });
|
|
24
|
+
* const doc = await pw.documents.createAndWait({ templateId: "tmpl_invoice", payload: { total: 42 } });
|
|
25
|
+
* const pdf = await pw.documents.download(doc.id);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare class PageWeaver {
|
|
29
|
+
readonly documents: DocumentsResource;
|
|
30
|
+
readonly templates: TemplatesResource;
|
|
31
|
+
readonly schemas: SchemasResource;
|
|
32
|
+
readonly usage: UsageResource;
|
|
33
|
+
constructor(options: PageWeaverOptions);
|
|
34
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PageWeaver = void 0;
|
|
4
|
+
const http_1 = require("./http");
|
|
5
|
+
const documents_1 = require("./documents");
|
|
6
|
+
const templates_1 = require("./templates");
|
|
7
|
+
const schemas_1 = require("./schemas");
|
|
8
|
+
const usage_1 = require("./usage");
|
|
9
|
+
/**
|
|
10
|
+
* The PageWeaver API client.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const pw = new PageWeaver({ apiKey: process.env.PAGEWEAVER_API_KEY! });
|
|
14
|
+
* const doc = await pw.documents.createAndWait({ templateId: "tmpl_invoice", payload: { total: 42 } });
|
|
15
|
+
* const pdf = await pw.documents.download(doc.id);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
class PageWeaver {
|
|
19
|
+
documents;
|
|
20
|
+
templates;
|
|
21
|
+
schemas;
|
|
22
|
+
usage;
|
|
23
|
+
constructor(options) {
|
|
24
|
+
const http = new http_1.HttpClient(options);
|
|
25
|
+
this.documents = new documents_1.DocumentsResource(http);
|
|
26
|
+
this.templates = new templates_1.TemplatesResource(http);
|
|
27
|
+
this.schemas = new schemas_1.SchemasResource(http);
|
|
28
|
+
this.usage = new usage_1.UsageResource(http);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.PageWeaver = PageWeaver;
|
|
32
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;AAAA,iCAAoD;AACpD,2CAAgD;AAChD,2CAAgD;AAChD,uCAA4C;AAC5C,mCAAwC;AAgBxC;;;;;;;;GAQG;AACH,MAAa,UAAU;IACZ,SAAS,CAAoB;IAC7B,SAAS,CAAoB;IAC7B,OAAO,CAAkB;IACzB,KAAK,CAAgB;IAE9B,YAAY,OAA0B;QACpC,MAAM,IAAI,GAAG,IAAI,iBAAU,CAAC,OAAO,CAAC,CAAC;QACrC,IAAI,CAAC,SAAS,GAAG,IAAI,6BAAiB,CAAC,IAAI,CAAC,CAAC;QAC7C,IAAI,CAAC,SAAS,GAAG,IAAI,6BAAiB,CAAC,IAAI,CAAC,CAAC;QAC7C,IAAI,CAAC,OAAO,GAAG,IAAI,yBAAe,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,CAAC,KAAK,GAAG,IAAI,qBAAa,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;CACF;AAbD,gCAaC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { HttpClient } from "./http";
|
|
2
|
+
import type { CreateDocumentParams, CreateDocumentResult, Document, DocumentPage, ListDocumentsParams } from "./types";
|
|
3
|
+
export interface WaitOptions {
|
|
4
|
+
/** Initial delay between polls, ms. Default 1000. */
|
|
5
|
+
intervalMs?: number;
|
|
6
|
+
/** Cap the (backing-off) poll delay, ms. Default 5000. */
|
|
7
|
+
maxIntervalMs?: number;
|
|
8
|
+
/** Multiplier applied to the delay after each poll. Default 1.5. */
|
|
9
|
+
backoff?: number;
|
|
10
|
+
/** Give up after this long, ms. Default 60000. */
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
/** Throw {@link PageWeaverDocumentFailedError} if the document fails. Default true. */
|
|
13
|
+
throwOnFailure?: boolean;
|
|
14
|
+
/** Abort waiting early. */
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
export interface DownloadOptions {
|
|
18
|
+
/** The download password, for a download-protected document. */
|
|
19
|
+
password?: string;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
/** Operations on documents: the core of the API. */
|
|
23
|
+
export declare class DocumentsResource {
|
|
24
|
+
private readonly http;
|
|
25
|
+
constructor(http: HttpClient);
|
|
26
|
+
/**
|
|
27
|
+
* Create a document from a template (with a validated payload) or from inline HTML. Returns
|
|
28
|
+
* `202` immediately with the document id and status "queued". Poll {@link get}, call
|
|
29
|
+
* {@link waitFor}, or use {@link createAndWait} to block until it is ready.
|
|
30
|
+
*/
|
|
31
|
+
create(params: CreateDocumentParams, signal?: AbortSignal): Promise<CreateDocumentResult>;
|
|
32
|
+
/** Fetch the current state of a document. When `status` is "done" it carries a `download` block. */
|
|
33
|
+
get(id: string, signal?: AbortSignal): Promise<Document>;
|
|
34
|
+
/** One page of the document history, newest first. Use `nextCursor` to page. */
|
|
35
|
+
list(params?: ListDocumentsParams, signal?: AbortSignal): Promise<DocumentPage>;
|
|
36
|
+
/**
|
|
37
|
+
* Iterate every document across all pages, transparently following the cursor.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* for await (const doc of pw.documents.listAll({ status: "failed" })) { ... }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
listAll(params?: Omit<ListDocumentsParams, "cursor">, signal?: AbortSignal): AsyncGenerator<DocumentPage["items"][number]>;
|
|
44
|
+
/**
|
|
45
|
+
* Faithfully replay a prior document: the same version (or inline source), payload, options, and
|
|
46
|
+
* download protection. Returns a new document id (`202`); counts as a new render.
|
|
47
|
+
*/
|
|
48
|
+
regenerate(id: string, signal?: AbortSignal): Promise<CreateDocumentResult>;
|
|
49
|
+
/**
|
|
50
|
+
* Poll a document until it reaches a terminal state (or the timeout elapses). Resolves with the
|
|
51
|
+
* finished {@link Document}. By default it throws {@link PageWeaverDocumentFailedError} on failure;
|
|
52
|
+
* pass `throwOnFailure: false` to receive the failed document instead.
|
|
53
|
+
*/
|
|
54
|
+
waitFor(id: string, opts?: WaitOptions): Promise<Document>;
|
|
55
|
+
/** Convenience: {@link create} then {@link waitFor}. Resolves with the finished document. */
|
|
56
|
+
createAndWait(params: CreateDocumentParams, opts?: WaitOptions): Promise<Document>;
|
|
57
|
+
/**
|
|
58
|
+
* Download the finished PDF bytes. For a download-protected document, pass `{ password }`. For an
|
|
59
|
+
* unprotected document, the short-lived signed URL is resolved and fetched automatically.
|
|
60
|
+
*/
|
|
61
|
+
download(id: string, opts?: DownloadOptions): Promise<Uint8Array>;
|
|
62
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DocumentsResource = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
/** Statuses at which a document stops changing. */
|
|
6
|
+
const TERMINAL = new Set(["done", "failed"]);
|
|
7
|
+
/** Operations on documents: the core of the API. */
|
|
8
|
+
class DocumentsResource {
|
|
9
|
+
http;
|
|
10
|
+
constructor(http) {
|
|
11
|
+
this.http = http;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create a document from a template (with a validated payload) or from inline HTML. Returns
|
|
15
|
+
* `202` immediately with the document id and status "queued". Poll {@link get}, call
|
|
16
|
+
* {@link waitFor}, or use {@link createAndWait} to block until it is ready.
|
|
17
|
+
*/
|
|
18
|
+
create(params, signal) {
|
|
19
|
+
const { idempotencyKey, ...rest } = params;
|
|
20
|
+
const headers = idempotencyKey ? { "idempotency-key": idempotencyKey } : undefined;
|
|
21
|
+
return this.http.json("POST", "/v1/documents", {
|
|
22
|
+
body: rest,
|
|
23
|
+
headers,
|
|
24
|
+
signal,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** Fetch the current state of a document. When `status` is "done" it carries a `download` block. */
|
|
28
|
+
get(id, signal) {
|
|
29
|
+
return this.http.json("GET", `/v1/documents/${encodeURIComponent(id)}`, { signal });
|
|
30
|
+
}
|
|
31
|
+
/** One page of the document history, newest first. Use `nextCursor` to page. */
|
|
32
|
+
list(params = {}, signal) {
|
|
33
|
+
return this.http.json("GET", "/v1/documents", {
|
|
34
|
+
query: {
|
|
35
|
+
status: params.status,
|
|
36
|
+
templateId: params.templateId,
|
|
37
|
+
cursor: params.cursor,
|
|
38
|
+
limit: params.limit,
|
|
39
|
+
},
|
|
40
|
+
signal,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Iterate every document across all pages, transparently following the cursor.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* for await (const doc of pw.documents.listAll({ status: "failed" })) { ... }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
async *listAll(params = {}, signal) {
|
|
51
|
+
let cursor;
|
|
52
|
+
do {
|
|
53
|
+
const page = await this.list({ ...params, cursor: cursor ?? undefined }, signal);
|
|
54
|
+
for (const item of page.items)
|
|
55
|
+
yield item;
|
|
56
|
+
cursor = page.nextCursor;
|
|
57
|
+
} while (cursor);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Faithfully replay a prior document: the same version (or inline source), payload, options, and
|
|
61
|
+
* download protection. Returns a new document id (`202`); counts as a new render.
|
|
62
|
+
*/
|
|
63
|
+
regenerate(id, signal) {
|
|
64
|
+
return this.http.json("POST", `/v1/documents/${encodeURIComponent(id)}/regenerate`, { signal });
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Poll a document until it reaches a terminal state (or the timeout elapses). Resolves with the
|
|
68
|
+
* finished {@link Document}. By default it throws {@link PageWeaverDocumentFailedError} on failure;
|
|
69
|
+
* pass `throwOnFailure: false` to receive the failed document instead.
|
|
70
|
+
*/
|
|
71
|
+
async waitFor(id, opts = {}) {
|
|
72
|
+
const intervalMs = opts.intervalMs ?? 1000;
|
|
73
|
+
const maxIntervalMs = opts.maxIntervalMs ?? 5000;
|
|
74
|
+
const backoff = opts.backoff ?? 1.5;
|
|
75
|
+
const timeoutMs = opts.timeoutMs ?? 60_000;
|
|
76
|
+
const throwOnFailure = opts.throwOnFailure ?? true;
|
|
77
|
+
const deadline = Date.now() + timeoutMs;
|
|
78
|
+
let delay = intervalMs;
|
|
79
|
+
let last = await this.get(id, opts.signal);
|
|
80
|
+
while (!TERMINAL.has(last.status)) {
|
|
81
|
+
if (Date.now() >= deadline) {
|
|
82
|
+
throw new errors_1.PageWeaverTimeoutError(id, last.status, timeoutMs);
|
|
83
|
+
}
|
|
84
|
+
const remaining = deadline - Date.now();
|
|
85
|
+
await sleep(Math.min(delay, remaining), opts.signal);
|
|
86
|
+
delay = Math.min(delay * backoff, maxIntervalMs);
|
|
87
|
+
last = await this.get(id, opts.signal);
|
|
88
|
+
}
|
|
89
|
+
if (last.status === "failed" && throwOnFailure) {
|
|
90
|
+
throw new errors_1.PageWeaverDocumentFailedError(last);
|
|
91
|
+
}
|
|
92
|
+
return last;
|
|
93
|
+
}
|
|
94
|
+
/** Convenience: {@link create} then {@link waitFor}. Resolves with the finished document. */
|
|
95
|
+
async createAndWait(params, opts = {}) {
|
|
96
|
+
const created = await this.create(params, opts.signal);
|
|
97
|
+
return this.waitFor(created.id, opts);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Download the finished PDF bytes. For a download-protected document, pass `{ password }`. For an
|
|
101
|
+
* unprotected document, the short-lived signed URL is resolved and fetched automatically.
|
|
102
|
+
*/
|
|
103
|
+
async download(id, opts = {}) {
|
|
104
|
+
if (opts.password !== undefined) {
|
|
105
|
+
return this.http.bytes("GET", `/v1/documents/${encodeURIComponent(id)}/content`, {
|
|
106
|
+
headers: { "x-document-password": opts.password },
|
|
107
|
+
noAuth: true,
|
|
108
|
+
signal: opts.signal,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const doc = await this.get(id, opts.signal);
|
|
112
|
+
if (doc.status !== "done" || !doc.download?.url) {
|
|
113
|
+
throw new errors_1.PageWeaverDocumentFailedError(doc);
|
|
114
|
+
}
|
|
115
|
+
if (doc.download.protected) {
|
|
116
|
+
throw new errors_1.PageWeaverDocumentFailedError({
|
|
117
|
+
...doc,
|
|
118
|
+
error: "Document is download-protected; supply a `password` to download it.",
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return this.http.fetchUrlBytes(doc.download.url, opts.signal);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
exports.DocumentsResource = DocumentsResource;
|
|
125
|
+
/** Delay `ms`, rejecting early if the signal aborts. */
|
|
126
|
+
function sleep(ms, signal) {
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
if (signal?.aborted)
|
|
129
|
+
return reject(new Error("Aborted"));
|
|
130
|
+
const timer = setTimeout(() => {
|
|
131
|
+
signal?.removeEventListener("abort", onAbort);
|
|
132
|
+
resolve();
|
|
133
|
+
}, ms);
|
|
134
|
+
const onAbort = () => {
|
|
135
|
+
clearTimeout(timer);
|
|
136
|
+
reject(new Error("Aborted"));
|
|
137
|
+
};
|
|
138
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=documents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"documents.js","sourceRoot":"","sources":["../src/documents.ts"],"names":[],"mappings":";;;AACA,qCAAiF;AASjF,mDAAmD;AACnD,MAAM,QAAQ,GAAwB,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;AAuBlE,oDAAoD;AACpD,MAAa,iBAAiB;IACC;IAA7B,YAA6B,IAAgB;QAAhB,SAAI,GAAJ,IAAI,CAAY;IAAG,CAAC;IAEjD;;;;OAIG;IACH,MAAM,CAAC,MAA4B,EAAE,MAAoB;QACvD,MAAM,EAAE,cAAc,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;QAC3C,MAAM,OAAO,GAAG,cAAc,CAAC,CAAC,CAAC,EAAE,iBAAiB,EAAE,cAAc,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;QACnF,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAuB,MAAM,EAAE,eAAe,EAAE;YACnE,IAAI,EAAE,IAAI;YACV,OAAO;YACP,MAAM;SACP,CAAC,CAAC;IACL,CAAC;IAED,oGAAoG;IACpG,GAAG,CAAC,EAAU,EAAE,MAAoB;QAClC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAW,KAAK,EAAE,iBAAiB,kBAAkB,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;IAChG,CAAC;IAED,gFAAgF;IAChF,IAAI,CAAC,SAA8B,EAAE,EAAE,MAAoB;QACzD,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAe,KAAK,EAAE,eAAe,EAAE;YAC1D,KAAK,EAAE;gBACL,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,UA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AAE,CAAC,CAAC;QACP,MAAM,OAAO,GAAG,GAAS,EAAE;YACzB,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,CAAC,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;QAC/B,CAAC,CAAC;QACF,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Document } from "./types";
|
|
2
|
+
/** Base class for every error the SDK throws. Catch this to handle any SDK failure. */
|
|
3
|
+
export declare class PageWeaverError extends Error {
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The API returned a non-2xx response. `status` is the HTTP status; `code`/`errors` are
|
|
8
|
+
* pulled from the JSON body when present (e.g. payload validation errors on a 400), and
|
|
9
|
+
* `body` is the raw parsed body for anything the typed fields don't cover.
|
|
10
|
+
*/
|
|
11
|
+
export declare class PageWeaverApiError extends PageWeaverError {
|
|
12
|
+
readonly status: number;
|
|
13
|
+
readonly code?: string;
|
|
14
|
+
readonly errors?: unknown;
|
|
15
|
+
readonly body: unknown;
|
|
16
|
+
constructor(args: {
|
|
17
|
+
status: number;
|
|
18
|
+
message: string;
|
|
19
|
+
code?: string;
|
|
20
|
+
errors?: unknown;
|
|
21
|
+
body: unknown;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** A network-level failure: connection refused, DNS, or an aborted/timed-out request. */
|
|
25
|
+
export declare class PageWeaverConnectionError extends PageWeaverError {
|
|
26
|
+
readonly cause?: unknown;
|
|
27
|
+
constructor(message: string, cause?: unknown);
|
|
28
|
+
}
|
|
29
|
+
/** `waitFor`/`createAndWait` exceeded its timeout before the document reached a terminal state. */
|
|
30
|
+
export declare class PageWeaverTimeoutError extends PageWeaverError {
|
|
31
|
+
readonly documentId: string;
|
|
32
|
+
readonly lastStatus: string;
|
|
33
|
+
constructor(documentId: string, lastStatus: string, timeoutMs: number);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The document reached the terminal `failed` state while waiting. Thrown by
|
|
37
|
+
* `waitFor`/`createAndWait` unless `throwOnFailure: false` is set. `document` carries the
|
|
38
|
+
* final response (including its `error` string).
|
|
39
|
+
*/
|
|
40
|
+
export declare class PageWeaverDocumentFailedError extends PageWeaverError {
|
|
41
|
+
readonly document: Document;
|
|
42
|
+
constructor(document: Document);
|
|
43
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PageWeaverDocumentFailedError = exports.PageWeaverTimeoutError = exports.PageWeaverConnectionError = exports.PageWeaverApiError = exports.PageWeaverError = void 0;
|
|
4
|
+
/** Base class for every error the SDK throws. Catch this to handle any SDK failure. */
|
|
5
|
+
class PageWeaverError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "PageWeaverError";
|
|
9
|
+
// Restore the prototype chain when compiled to ES5-ish targets.
|
|
10
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.PageWeaverError = PageWeaverError;
|
|
14
|
+
/**
|
|
15
|
+
* The API returned a non-2xx response. `status` is the HTTP status; `code`/`errors` are
|
|
16
|
+
* pulled from the JSON body when present (e.g. payload validation errors on a 400), and
|
|
17
|
+
* `body` is the raw parsed body for anything the typed fields don't cover.
|
|
18
|
+
*/
|
|
19
|
+
class PageWeaverApiError extends PageWeaverError {
|
|
20
|
+
status;
|
|
21
|
+
code;
|
|
22
|
+
errors;
|
|
23
|
+
body;
|
|
24
|
+
constructor(args) {
|
|
25
|
+
super(args.message);
|
|
26
|
+
this.name = "PageWeaverApiError";
|
|
27
|
+
this.status = args.status;
|
|
28
|
+
this.code = args.code;
|
|
29
|
+
this.errors = args.errors;
|
|
30
|
+
this.body = args.body;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.PageWeaverApiError = PageWeaverApiError;
|
|
34
|
+
/** A network-level failure: connection refused, DNS, or an aborted/timed-out request. */
|
|
35
|
+
class PageWeaverConnectionError extends PageWeaverError {
|
|
36
|
+
cause;
|
|
37
|
+
constructor(message, cause) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "PageWeaverConnectionError";
|
|
40
|
+
this.cause = cause;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.PageWeaverConnectionError = PageWeaverConnectionError;
|
|
44
|
+
/** `waitFor`/`createAndWait` exceeded its timeout before the document reached a terminal state. */
|
|
45
|
+
class PageWeaverTimeoutError extends PageWeaverError {
|
|
46
|
+
documentId;
|
|
47
|
+
lastStatus;
|
|
48
|
+
constructor(documentId, lastStatus, timeoutMs) {
|
|
49
|
+
super(`Timed out after ${timeoutMs}ms waiting for document ${documentId} (last status: ${lastStatus}).`);
|
|
50
|
+
this.name = "PageWeaverTimeoutError";
|
|
51
|
+
this.documentId = documentId;
|
|
52
|
+
this.lastStatus = lastStatus;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.PageWeaverTimeoutError = PageWeaverTimeoutError;
|
|
56
|
+
/**
|
|
57
|
+
* The document reached the terminal `failed` state while waiting. Thrown by
|
|
58
|
+
* `waitFor`/`createAndWait` unless `throwOnFailure: false` is set. `document` carries the
|
|
59
|
+
* final response (including its `error` string).
|
|
60
|
+
*/
|
|
61
|
+
class PageWeaverDocumentFailedError extends PageWeaverError {
|
|
62
|
+
document;
|
|
63
|
+
constructor(document) {
|
|
64
|
+
super(`Document ${document.id} failed: ${document.error ?? "unknown error"}`);
|
|
65
|
+
this.name = "PageWeaverDocumentFailedError";
|
|
66
|
+
this.document = document;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.PageWeaverDocumentFailedError = PageWeaverDocumentFailedError;
|
|
70
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAEA,uFAAuF;AACvF,MAAa,eAAgB,SAAQ,KAAK;IACxC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,gEAAgE;QAChE,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AAPD,0CAOC;AAED;;;;GAIG;AACH,MAAa,kBAAmB,SAAQ,eAAe;IAC5C,MAAM,CAAS;IACf,IAAI,CAAU;IACd,MAAM,CAAW;IACjB,IAAI,CAAU;IAEvB,YAAY,IAAyF;QACnG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;IACxB,CAAC;CACF;AAdD,gDAcC;AAED,yFAAyF;AACzF,MAAa,yBAA0B,SAAQ,eAAe;IAC1C,KAAK,CAAW;IAClC,YAAY,OAAe,EAAE,KAAe;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF;AAPD,8DAOC;AAED,mGAAmG;AACnG,MAAa,sBAAuB,SAAQ,eAAe;IAChD,UAAU,CAAS;IACnB,UAAU,CAAS;IAC5B,YAAY,UAAkB,EAAE,UAAkB,EAAE,SAAiB;QACnE,KAAK,CACH,mBAAmB,SAAS,2BAA2B,UAAU,kBAAkB,UAAU,IAAI,CAClG,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;AAXD,wDAWC;AAED;;;;GAIG;AACH,MAAa,6BAA8B,SAAQ,eAAe;IACvD,QAAQ,CAAW;IAC5B,YAAY,QAAkB;QAC5B,KAAK,CAAC,YAAY,QAAQ,CAAC,EAAE,YAAY,QAAQ,CAAC,KAAK,IAAI,eAAe,EAAE,CAAC,CAAC;QAC9E,IAAI,CAAC,IAAI,GAAG,+BAA+B,CAAC;QAC5C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAPD,sEAOC"}
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** A `fetch` implementation compatible with the global one (Node 18+, browsers, undici). */
|
|
2
|
+
export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
3
|
+
export interface HttpClientOptions {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
/** API base URL. Defaults to https://api.pageweaver.io; point it at http://localhost:4000 in dev. */
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
/** Per-request timeout in milliseconds. Default 30000. */
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
/** Inject a custom fetch (defaults to the global). */
|
|
10
|
+
fetch?: FetchLike;
|
|
11
|
+
}
|
|
12
|
+
export interface RequestInitLite {
|
|
13
|
+
query?: Record<string, string | number | undefined>;
|
|
14
|
+
body?: unknown;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
/** Skip attaching the `x-api-key` header (used by the recipient-facing content endpoint). */
|
|
17
|
+
noAuth?: boolean;
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Thin fetch wrapper: attaches the API key, serializes JSON, applies a timeout, and maps
|
|
22
|
+
* non-2xx responses to {@link PageWeaverApiError} and transport failures to
|
|
23
|
+
* {@link PageWeaverConnectionError}. Every resource is built on this.
|
|
24
|
+
*/
|
|
25
|
+
export declare class HttpClient {
|
|
26
|
+
private readonly apiKey;
|
|
27
|
+
private readonly baseUrl;
|
|
28
|
+
private readonly timeoutMs;
|
|
29
|
+
private readonly fetchImpl;
|
|
30
|
+
constructor(opts: HttpClientOptions);
|
|
31
|
+
/** Perform a request and parse a JSON response into `T`. */
|
|
32
|
+
json<T>(method: string, path: string, init?: RequestInitLite): Promise<T>;
|
|
33
|
+
/** Perform a request and return the raw response body as bytes (for PDF downloads). */
|
|
34
|
+
bytes(method: string, path: string, init?: RequestInitLite): Promise<Uint8Array>;
|
|
35
|
+
/** Fetch an absolute URL (e.g. a signed download URL) and return its bytes. */
|
|
36
|
+
fetchUrlBytes(url: string, signal?: AbortSignal): Promise<Uint8Array>;
|
|
37
|
+
private send;
|
|
38
|
+
/** Compose a timeout AbortController with an optional caller-supplied signal. */
|
|
39
|
+
private withTimeout;
|
|
40
|
+
private wrapTransport;
|
|
41
|
+
}
|