@formepdf/sdk 0.7.8

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/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # @formepdf/sdk
2
+
3
+ TypeScript client for the [Forme](https://formepdf.com) hosted PDF API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @formepdf/sdk
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ ```typescript
14
+ import { Forme } from '@formepdf/sdk';
15
+
16
+ const forme = new Forme('your-api-key');
17
+
18
+ // Render a template to PDF
19
+ const pdf = await forme.render('invoice', {
20
+ customerName: 'Acme Corp',
21
+ items: [{ name: 'Widget', price: 9.99 }],
22
+ });
23
+
24
+ // pdf is a Uint8Array — write to file, return as response, etc.
25
+ await fs.writeFile('invoice.pdf', pdf);
26
+
27
+ // Extract embedded data from a PDF
28
+ const data = await forme.extract(pdf);
29
+ // data === { customerName: 'Acme Corp', items: [...] }
30
+ ```
31
+
32
+ ## Constructor
33
+
34
+ ```typescript
35
+ new Forme(apiKey: string, options?: { baseUrl?: string })
36
+ ```
37
+
38
+ | Option | Default | Description |
39
+ | --------- | ---------------------------- | ------------------------ |
40
+ | `baseUrl` | `https://api.formepdf.com` | API base URL |
41
+
42
+ ## Methods
43
+
44
+ ### `render(slug, data?)`
45
+
46
+ Render a template to PDF bytes.
47
+
48
+ - **slug** — Template identifier
49
+ - **data** — Template data (sent as JSON body, defaults to `{}`)
50
+ - **Returns** `Promise<Uint8Array>`
51
+
52
+ ### `extract(pdf)`
53
+
54
+ Extract embedded data from a PDF generated by Forme.
55
+
56
+ - **pdf** — PDF file bytes
57
+ - **Returns** `Promise<unknown | null>` — `null` if the PDF has no embedded data
58
+
59
+ ## Error handling
60
+
61
+ ```typescript
62
+ import { Forme, FormeError } from '@formepdf/sdk';
63
+
64
+ try {
65
+ const pdf = await forme.render('invoice', data);
66
+ } catch (err) {
67
+ if (err instanceof FormeError) {
68
+ console.error(`API error ${err.status}: ${err.message}`);
69
+ }
70
+ }
71
+ ```
72
+
73
+ ## Requirements
74
+
75
+ Node.js 18+ (uses global `fetch`).
@@ -0,0 +1,42 @@
1
+ export interface FormeOptions {
2
+ baseUrl?: string;
3
+ }
4
+ export interface S3Options {
5
+ bucket: string;
6
+ key: string;
7
+ accessKeyId: string;
8
+ secretAccessKey: string;
9
+ region?: string;
10
+ endpoint?: string;
11
+ }
12
+ export interface RenderOptions {
13
+ s3?: S3Options;
14
+ }
15
+ export interface AsyncRenderOptions {
16
+ webhookUrl?: string;
17
+ }
18
+ export interface JobResult {
19
+ id: string;
20
+ status: string;
21
+ pdfBase64?: string;
22
+ error?: string;
23
+ completedAt?: string;
24
+ }
25
+ export declare class FormeError extends Error {
26
+ status: number;
27
+ constructor(status: number, message: string);
28
+ }
29
+ export declare class Forme {
30
+ private apiKey;
31
+ private baseUrl;
32
+ constructor(apiKey: string, options?: FormeOptions);
33
+ render(slug: string, data?: unknown, options?: RenderOptions): Promise<Uint8Array | {
34
+ url: string;
35
+ }>;
36
+ renderAsync(slug: string, data?: unknown, options?: AsyncRenderOptions): Promise<{
37
+ jobId: string;
38
+ status: string;
39
+ }>;
40
+ getJob(jobId: string): Promise<JobResult>;
41
+ extract(pdf: Uint8Array): Promise<unknown | null>;
42
+ }
package/dist/index.js ADDED
@@ -0,0 +1,101 @@
1
+ const DEFAULT_BASE_URL = 'https://api.formepdf.com';
2
+ export class FormeError extends Error {
3
+ status;
4
+ constructor(status, message) {
5
+ super(message);
6
+ this.name = 'FormeError';
7
+ this.status = status;
8
+ }
9
+ }
10
+ export class Forme {
11
+ apiKey;
12
+ baseUrl;
13
+ constructor(apiKey, options) {
14
+ this.apiKey = apiKey;
15
+ this.baseUrl = (options?.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '');
16
+ }
17
+ async render(slug, data, options) {
18
+ const body = options?.s3
19
+ ? { ...(data ?? {}), s3: options.s3 }
20
+ : (data ?? {});
21
+ const res = await fetch(`${this.baseUrl}/v1/render/${slug}`, {
22
+ method: 'POST',
23
+ headers: {
24
+ 'Authorization': `Bearer ${this.apiKey}`,
25
+ 'Content-Type': 'application/json',
26
+ },
27
+ body: JSON.stringify(body),
28
+ });
29
+ if (!res.ok) {
30
+ const message = await parseErrorMessage(res);
31
+ throw new FormeError(res.status, message);
32
+ }
33
+ if (options?.s3) {
34
+ const json = await res.json();
35
+ return { url: json.url };
36
+ }
37
+ return new Uint8Array(await res.arrayBuffer());
38
+ }
39
+ async renderAsync(slug, data, options) {
40
+ const body = options?.webhookUrl
41
+ ? { ...(data ?? {}), webhookUrl: options.webhookUrl }
42
+ : (data ?? {});
43
+ const res = await fetch(`${this.baseUrl}/v1/render/${slug}/async`, {
44
+ method: 'POST',
45
+ headers: {
46
+ 'Authorization': `Bearer ${this.apiKey}`,
47
+ 'Content-Type': 'application/json',
48
+ },
49
+ body: JSON.stringify(body),
50
+ });
51
+ if (!res.ok) {
52
+ const message = await parseErrorMessage(res);
53
+ throw new FormeError(res.status, message);
54
+ }
55
+ return res.json();
56
+ }
57
+ async getJob(jobId) {
58
+ const res = await fetch(`${this.baseUrl}/v1/jobs/${jobId}`, {
59
+ headers: {
60
+ 'Authorization': `Bearer ${this.apiKey}`,
61
+ },
62
+ });
63
+ if (!res.ok) {
64
+ const message = await parseErrorMessage(res);
65
+ throw new FormeError(res.status, message);
66
+ }
67
+ return res.json();
68
+ }
69
+ async extract(pdf) {
70
+ const res = await fetch(`${this.baseUrl}/v1/extract`, {
71
+ method: 'POST',
72
+ headers: {
73
+ 'Authorization': `Bearer ${this.apiKey}`,
74
+ 'Content-Type': 'application/pdf',
75
+ },
76
+ body: pdf,
77
+ });
78
+ if (res.status === 404) {
79
+ const body = await res.json().catch(() => null);
80
+ if (body?.error?.toLowerCase().includes('no embedded data')) {
81
+ return null;
82
+ }
83
+ throw new FormeError(404, body?.error ?? 'Not found');
84
+ }
85
+ if (!res.ok) {
86
+ const message = await parseErrorMessage(res);
87
+ throw new FormeError(res.status, message);
88
+ }
89
+ const body = await res.json();
90
+ return body.data;
91
+ }
92
+ }
93
+ async function parseErrorMessage(res) {
94
+ try {
95
+ const body = await res.json();
96
+ return body.error ?? body.message ?? `Request failed with status ${res.status}`;
97
+ }
98
+ catch {
99
+ return `Request failed with status ${res.status}`;
100
+ }
101
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@formepdf/sdk",
3
+ "version": "0.7.8",
4
+ "description": "TypeScript client for the Forme hosted PDF API",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "vitest run"
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "devDependencies": {
16
+ "@types/node": "^22.0.0",
17
+ "typescript": "^5.7.0",
18
+ "vitest": "^3.0.0"
19
+ },
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/formepdf/forme",
24
+ "directory": "packages/sdk"
25
+ }
26
+ }