@templr/sdk 0.0.0-stage → 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/CHANGELOG.md ADDED
@@ -0,0 +1,26 @@
1
+ # Changelog
2
+
3
+ All notable changes to the Templr TypeScript SDK are documented here.
4
+
5
+ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the SDK follows
6
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Releases are tagged `sdks/typescript/vX.Y.Z`.
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ - Templates: `listVariables` and `updateVariable`, with the `TemplateVariable` type.
13
+ - Assets: `upload`, which starts an upload, sends the file to the storage bucket and confirms it in one call.
14
+ - `UploadError`, thrown when the storage bucket rejects the file of an upload.
15
+
16
+ ## [0.1.0]
17
+
18
+ ### Added
19
+
20
+ - `Templr` client for the Templr REST API v1, authenticated with a workspace API key. It runs anywhere
21
+ `fetch` exists: Node.js 20.19+, Bun, Deno and edge runtimes.
22
+ - Templates: `list`, `listDrafts`, `get`, `getDraft` and `render`.
23
+ - Assets: `list`, `createUpload`, `confirm` and `delete`.
24
+ - `getOpenApiDocument` to read the OpenAPI document served by an instance.
25
+ - Responses validated with Zod, returned as frozen objects with `Date` timestamps.
26
+ - Typed errors with a machine-readable `code`, one class per HTTP status family.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sebastian Marat Urdanegui Bisalaya
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 CHANGED
@@ -1,3 +1,235 @@
1
- # Temporary Holding Version
1
+ # Templr TypeScript SDK
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The official TypeScript client for the [Templr](https://github.com/SebastianUrdaneguiBisalaya/templr) REST API.
4
+ Render the email templates your team designs in Templr from any JavaScript backend, with typed results
5
+ and typed errors.
6
+
7
+ - Written in TypeScript, with type declarations included
8
+ - Runs anywhere `fetch` exists: Node.js 20.19+, Bun, Deno, Cloudflare Workers and other edge runtimes
9
+ - ES module, with no dependency other than [Zod](https://zod.dev/)
10
+ - Every response is validated and returned as a frozen object, with `Date` timestamps
11
+
12
+ Templr does not send emails. Render a template with this SDK, then send the HTML and text with the email
13
+ provider you already use.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @templr/sdk
19
+ # or
20
+ bun add @templr/sdk
21
+ # or
22
+ pnpm add @templr/sdk
23
+ ```
24
+
25
+ The package is ESM only. Node.js 20.19+ can also load it with `require()` from CommonJS code.
26
+
27
+ ## Quick start
28
+
29
+ ```ts
30
+ import { Templr } from '@templr/sdk';
31
+
32
+ const templr = new Templr({
33
+ apiKey: process.env.TEMPLR_API_KEY ?? '',
34
+ baseUrl: 'https://templr.example.com',
35
+ });
36
+
37
+ const email = await templr.templates.render('2b0f3a0e-6a1c-4d43-9a57-1f1f6b7d5e01', {
38
+ values: { name: 'Ada' },
39
+ });
40
+
41
+ console.log(email.html); // The email HTML
42
+ console.log(email.text); // The plain text version
43
+ ```
44
+
45
+ ## Authentication
46
+
47
+ Every request is authenticated with a workspace API key and scoped to that workspace. Create keys in
48
+ Templr under the workspace settings.
49
+
50
+ - A `read` key can list, get and render templates, and list images.
51
+ - A `write` key can also upload and delete images.
52
+
53
+ Keys have a fixed expiration and are never renewed automatically. Keep them secret: use the SDK only on
54
+ your server, never in a browser or mobile app, and read the key from the environment instead of
55
+ hard-coding it.
56
+
57
+ `baseUrl` is the URL of your Templr instance, since Templr is self-hosted. If Templr is served under a
58
+ path, include it: `https://example.com/templr`.
59
+
60
+ ## Usage
61
+
62
+ ### Templates
63
+
64
+ By default the API serves the latest **published** version of each template. `listDrafts`, `getDraft`
65
+ and `draft: true` serve the draft editors are working on.
66
+
67
+ ```ts
68
+ // Published templates, most recently published first
69
+ const page = await templr.templates.list({ limit: 50 });
70
+ for (const version of page.items) {
71
+ console.log(version.templateId, version.name, version.version, version.publishedAt);
72
+ }
73
+
74
+ // Every template with its draft, most recently updated first
75
+ const drafts = await templr.templates.listDrafts();
76
+
77
+ // One template, with its document
78
+ const version = await templr.templates.get(templateId); // latest published version
79
+ const draft = await templr.templates.getDraft(templateId); // draft
80
+
81
+ // Render to HTML and plain text; missing values use the placeholder's fallback
82
+ const email = await templr.templates.render(templateId, { values: { name: 'Ada', plan: 'Pro' } });
83
+ const preview = await templr.templates.render(templateId, { draft: true, values: { name: 'Ada' } });
84
+ ```
85
+
86
+ ### Variables
87
+
88
+ The variables of a template are the values `render` takes. Each key appears once, in document order.
89
+
90
+ ```ts
91
+ const variables = await templr.templates.listVariables(templateId); // { draft: true } for the draft
92
+ for (const variable of variables) {
93
+ console.log(variable.key, variable.fallback); // fallback is null when nothing renders without a value
94
+ }
95
+
96
+ // Rename a variable of the draft, change its fallback (null for nothing) or both, at every occurrence.
97
+ // Requires a `write` key; published versions are not changed.
98
+ const variable = await templr.templates.updateVariable(templateId, 'name', { fallback: 'there', key: 'first_name' });
99
+ ```
100
+
101
+ ### Pagination
102
+
103
+ Lists return a `Page` with `items`, `total` (the size of the whole list) and `nextCursor`, which is
104
+ `null` on the last page.
105
+
106
+ ```ts
107
+ let cursor: string | undefined;
108
+ do {
109
+ const page = await templr.templates.list({ cursor, limit: 100 });
110
+ for (const version of page.items) {
111
+ // ...
112
+ }
113
+ cursor = page.nextCursor ?? undefined;
114
+ } while (cursor);
115
+ ```
116
+
117
+ ### Images
118
+
119
+ `upload` starts the upload, sends the file straight to the storage bucket and confirms it. It requires a
120
+ `write` key; accepted types are gif, jpeg, png and webp, up to 5 MiB. The file can be a `Uint8Array`
121
+ (such as a Node.js `Buffer`) or a `Blob`.
122
+
123
+ ```ts
124
+ import { readFile } from 'node:fs/promises';
125
+
126
+ const asset = await templr.assets.upload(await readFile('logo.png'), { contentType: 'image/png' });
127
+ console.log(asset.url);
128
+ ```
129
+
130
+ The same three steps can also be run one by one:
131
+
132
+ ```ts
133
+ const file = await readFile('logo.png');
134
+
135
+ // 1. Start the upload
136
+ const upload = await templr.assets.createUpload({ contentType: 'image/png', sizeBytes: file.byteLength });
137
+
138
+ // 2. Send the fields, then the file as the last field, to the bucket before `upload.expiresAt`
139
+ const form = new FormData();
140
+ for (const [name, value] of Object.entries(upload.upload.fields)) form.append(name, value);
141
+ form.append('file', new Blob([file], { type: 'image/png' }), 'logo.png');
142
+ const response = await fetch(upload.upload.url, { body: form, method: 'POST' });
143
+ if (!response.ok) throw new Error(`Upload failed with ${response.status}`);
144
+
145
+ // 3. Confirm it; the image is now listed, with its public URL
146
+ const confirmed = await templr.assets.confirm(upload.assetId);
147
+ ```
148
+
149
+ ```ts
150
+ const images = await templr.assets.list();
151
+ await templr.assets.delete(asset.id); // Templates that use it will show a broken image
152
+ ```
153
+
154
+ ## Error handling
155
+
156
+ Every error thrown by the SDK extends `TemplrError` and has a machine-readable `code`. Errors returned by
157
+ the API are `APIError` subclasses chosen by HTTP status, with the `code`, `message` and validation
158
+ `issues` sent by the API.
159
+
160
+ ```ts
161
+ import { APIConnectionError, APIError, NotFoundError, RateLimitError } from '@templr/sdk';
162
+
163
+ try {
164
+ const email = await templr.templates.render(templateId, { values: { name: 'Ada' } });
165
+ } catch (error) {
166
+ if (error instanceof NotFoundError) {
167
+ // The template does not exist, belongs to another workspace, or was never published
168
+ } else if (error instanceof RateLimitError) {
169
+ // Retry after `error.retryAfter` seconds
170
+ } else if (error instanceof APIError) {
171
+ console.error(error.status, error.code, error.message, error.issues);
172
+ } else if (error instanceof APIConnectionError) {
173
+ // The server could not be reached, or the request timed out (`APITimeoutError`)
174
+ } else {
175
+ throw error;
176
+ }
177
+ }
178
+ ```
179
+
180
+ | Error | Status | API codes |
181
+ | ------------------------- | ------ | ---------------------------------------------------------------------------------------------- |
182
+ | `BadRequestError` | 400 | `invalid_input`, `validation_error` |
183
+ | `AuthenticationError` | 401 | `unauthorized` |
184
+ | `PermissionDeniedError` | 403 | `forbidden` |
185
+ | `NotFoundError` | 404 | `not_found` |
186
+ | `ConflictError` | 409 | `upload_missing`, `conflict` |
187
+ | `PayloadTooLargeError` | 413 | `payload_too_large` |
188
+ | `RateLimitError` | 429 | `rate_limited` |
189
+ | `InternalServerError` | 5xx | `internal_error`, `database_error`, `render_failed`, `storage_error`, `storage_not_configured` |
190
+ | `UploadError` | bucket | `upload_failed`: the storage bucket rejected the file sent by `assets.upload` |
191
+ | `APIConnectionError` | none | `connection_error` |
192
+ | `APITimeoutError` | none | `timeout` |
193
+ | `UnexpectedResponseError` | 2xx | `unexpected_response`: the server answered with a body this SDK does not understand |
194
+
195
+ An error response without a Templr error body (for example, an HTML page from a proxy) throws the class
196
+ of its status with the code `unknown_error`. Invalid options (an empty `apiKey`, or a `baseUrl` that is
197
+ not an http(s) URL) throw a `TypeError` from the constructor.
198
+
199
+ ## Configuration
200
+
201
+ ```ts
202
+ const templr = new Templr({
203
+ apiKey: process.env.TEMPLR_API_KEY ?? '',
204
+ baseUrl: 'https://templr.example.com',
205
+ timeoutMs: 10_000, // Milliseconds per request (default: 30 000)
206
+ fetch: myFetch, // Optional: any fetch-compatible function (proxies, retries, logging...)
207
+ });
208
+ ```
209
+
210
+ ## Versioning
211
+
212
+ The SDK follows [Semantic Versioning](https://semver.org/) and is versioned independently of the Templr
213
+ app. Each release is tagged `sdks/typescript/vX.Y.Z`; see the [changelog](CHANGELOG.md).
214
+
215
+ ## Development
216
+
217
+ The SDK lives in `sdks/typescript` of the Templr repository, outside the app's Bun workspaces, and uses
218
+ [Bun](https://bun.sh/) for installing and testing.
219
+
220
+ ```bash
221
+ cd sdks/typescript
222
+ bun install --frozen-lockfile # Install the exact versions of bun.lock
223
+ bun run lint # Biome, with the repository's rules
224
+ bun run typecheck # Strict type checking
225
+ bun test # Tests (local only, no network)
226
+ bun run build # Compile to dist/
227
+ ```
228
+
229
+ From the repository root, `make sdk-typescript-check` runs all of them, and `make sdk-typescript-spec`
230
+ refreshes `tests/openapi.json`, the copy of the API's OpenAPI document that the contract tests check the
231
+ SDK against. See [RELEASING.md](RELEASING.md) to publish a new version.
232
+
233
+ ## License
234
+
235
+ [MIT](LICENSE). The Templr app itself is licensed under the PolyForm Noncommercial License 1.0.0 (see the root LICENSE).
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The Templr client.
3
+ *
4
+ * @module
5
+ */
6
+ import { Assets } from './resources/assets.js';
7
+ import { Templates } from './resources/templates.js';
8
+ import { type OpenApiDocument } from './schemas.js';
9
+ import { type Fetch } from './transport.js';
10
+ /** Options of {@link Templr}. */
11
+ export interface TemplrOptions {
12
+ /** A workspace API key. Keep it secret: never ship it to a browser or mobile app. */
13
+ readonly apiKey: string;
14
+ /** The URL of your Templr instance, e.g. `https://templr.example.com`. */
15
+ readonly baseUrl: string;
16
+ /** The `fetch` implementation to send requests with; defaults to the global `fetch`. */
17
+ readonly fetch?: Fetch;
18
+ /** Milliseconds to wait for each request (default: 30 000). */
19
+ readonly timeoutMs?: number;
20
+ }
21
+ /**
22
+ * Client of the Templr REST API v1. Every request is scoped to the workspace
23
+ * of the API key.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { Templr } from '@templr/sdk';
28
+ *
29
+ * const templr = new Templr({ apiKey: process.env.TEMPLR_API_KEY ?? '', baseUrl: 'https://templr.example.com' });
30
+ * const email = await templr.templates.render(templateId, { values: { name: 'Ada' } });
31
+ * ```
32
+ */
33
+ export declare class Templr {
34
+ #private;
35
+ /** List, get and render templates. */
36
+ readonly templates: Templates;
37
+ /** Upload, list and delete images. */
38
+ readonly assets: Assets;
39
+ /**
40
+ * Creates a client.
41
+ *
42
+ * @throws TypeError when `apiKey` is empty or `baseUrl` is not an http(s) URL.
43
+ */
44
+ constructor(options: TemplrOptions);
45
+ /** Gets the OpenAPI 3.1 document of the API served by this instance. */
46
+ getOpenApiDocument(): Promise<OpenApiDocument>;
47
+ }
package/dist/client.js ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The Templr client.
3
+ *
4
+ * @module
5
+ */
6
+ import { Assets } from './resources/assets.js';
7
+ import { Templates } from './resources/templates.js';
8
+ import { openApiDocumentSchema } from './schemas.js';
9
+ import { DEFAULT_TIMEOUT_MS, Transport } from './transport.js';
10
+ /**
11
+ * Client of the Templr REST API v1. Every request is scoped to the workspace
12
+ * of the API key.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { Templr } from '@templr/sdk';
17
+ *
18
+ * const templr = new Templr({ apiKey: process.env.TEMPLR_API_KEY ?? '', baseUrl: 'https://templr.example.com' });
19
+ * const email = await templr.templates.render(templateId, { values: { name: 'Ada' } });
20
+ * ```
21
+ */
22
+ export class Templr {
23
+ /** List, get and render templates. */
24
+ templates;
25
+ /** Upload, list and delete images. */
26
+ assets;
27
+ #transport;
28
+ /**
29
+ * Creates a client.
30
+ *
31
+ * @throws TypeError when `apiKey` is empty or `baseUrl` is not an http(s) URL.
32
+ */
33
+ constructor(options) {
34
+ this.#transport = new Transport({
35
+ apiKey: options.apiKey,
36
+ baseUrl: options.baseUrl,
37
+ fetch: options.fetch ?? ((input, init) => globalThis.fetch(input, init)),
38
+ timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
39
+ });
40
+ this.templates = new Templates(this.#transport);
41
+ this.assets = new Assets(this.#transport);
42
+ }
43
+ /** Gets the OpenAPI 3.1 document of the API served by this instance. */
44
+ getOpenApiDocument() {
45
+ return this.#transport.send({ method: 'GET', path: '/api/v1/openapi.json' }, openApiDocumentSchema);
46
+ }
47
+ }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Errors thrown by the Templr SDK.
3
+ *
4
+ * Every error extends {@link TemplrError} and carries a machine-readable
5
+ * `code`. Errors returned by the API are {@link APIError} subclasses chosen by
6
+ * HTTP status, with the `code`, `message` and `issues` sent by the API.
7
+ *
8
+ * @module
9
+ */
10
+ /** Base class of every error thrown by the SDK. */
11
+ export declare class TemplrError extends Error {
12
+ /** A machine-readable error code. */
13
+ readonly code: string;
14
+ constructor(message: string, code: string, options?: ErrorOptions);
15
+ }
16
+ /** The request could not reach the Templr server (DNS, TLS, refused connection...). */
17
+ export declare class APIConnectionError extends TemplrError {
18
+ constructor(message?: string, options?: ErrorOptions, code?: string);
19
+ }
20
+ /** The request took longer than the configured timeout. */
21
+ export declare class APITimeoutError extends APIConnectionError {
22
+ constructor(options?: ErrorOptions);
23
+ }
24
+ /**
25
+ * The API answered with a success status but a body the SDK does not
26
+ * understand. This usually means the server runs an incompatible version of
27
+ * the API.
28
+ */
29
+ export declare class UnexpectedResponseError extends TemplrError {
30
+ /** The HTTP status of the response. */
31
+ readonly status: number;
32
+ /** The raw response body. */
33
+ readonly body: string;
34
+ constructor(message: string, status: number, body: string, options?: ErrorOptions);
35
+ }
36
+ /**
37
+ * The storage bucket rejected the file of an image upload: a file of another
38
+ * type or size than the upload was started with, or an expired upload.
39
+ */
40
+ export declare class UploadError extends TemplrError {
41
+ /** The HTTP status of the bucket response. */
42
+ readonly status: number;
43
+ /** The raw response body. */
44
+ readonly body: string;
45
+ constructor(status: number, body: string);
46
+ }
47
+ /** What the API sent with an error status. */
48
+ export interface APIErrorDetails {
49
+ /** The error code sent by the API; `unknown_error` when the response has no Templr error body. */
50
+ readonly code: string;
51
+ /** Every validation problem found; empty for other errors. */
52
+ readonly issues: readonly string[];
53
+ readonly message: string;
54
+ /** The HTTP status of the response. */
55
+ readonly status: number;
56
+ }
57
+ /** The API answered with an error status. */
58
+ export declare class APIError extends TemplrError {
59
+ /** The HTTP status of the response. */
60
+ readonly status: number;
61
+ /** Every validation problem found; empty for other errors. */
62
+ readonly issues: readonly string[];
63
+ constructor(details: APIErrorDetails);
64
+ }
65
+ /** 400: the request is invalid (`invalid_input`, `validation_error`). */
66
+ export declare class BadRequestError extends APIError {
67
+ }
68
+ /** 401: the API key is missing, unknown or expired (`unauthorized`). */
69
+ export declare class AuthenticationError extends APIError {
70
+ }
71
+ /** 403: the API key scope does not allow this operation (`forbidden`). */
72
+ export declare class PermissionDeniedError extends APIError {
73
+ }
74
+ /** 404: the resource does not exist or belongs to another workspace (`not_found`). */
75
+ export declare class NotFoundError extends APIError {
76
+ }
77
+ /**
78
+ * 409: the resource is not in the expected state, e.g. the file is not uploaded
79
+ * yet (`upload_missing`), or another variable already has the new key (`conflict`).
80
+ */
81
+ export declare class ConflictError extends APIError {
82
+ }
83
+ /** 413: the request body is too large (`payload_too_large`). */
84
+ export declare class PayloadTooLargeError extends APIError {
85
+ }
86
+ /** 429: the API key is over its rate limit (`rate_limited`). */
87
+ export declare class RateLimitError extends APIError {
88
+ /** Seconds to wait before retrying, from the `Retry-After` header, when sent. */
89
+ readonly retryAfter: number | null;
90
+ constructor(details: APIErrorDetails, retryAfter: number | null);
91
+ }
92
+ /** 5xx: the server or its storage failed (`internal_error`, `storage_error`, `storage_not_configured`...). */
93
+ export declare class InternalServerError extends APIError {
94
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Errors thrown by the Templr SDK.
3
+ *
4
+ * Every error extends {@link TemplrError} and carries a machine-readable
5
+ * `code`. Errors returned by the API are {@link APIError} subclasses chosen by
6
+ * HTTP status, with the `code`, `message` and `issues` sent by the API.
7
+ *
8
+ * @module
9
+ */
10
+ /** Base class of every error thrown by the SDK. */
11
+ export class TemplrError extends Error {
12
+ /** A machine-readable error code. */
13
+ code;
14
+ constructor(message, code, options) {
15
+ super(message, options);
16
+ this.name = new.target.name;
17
+ this.code = code;
18
+ }
19
+ }
20
+ /** The request could not reach the Templr server (DNS, TLS, refused connection...). */
21
+ export class APIConnectionError extends TemplrError {
22
+ constructor(message = 'Could not connect to the Templr API.', options, code = 'connection_error') {
23
+ super(message, code, options);
24
+ }
25
+ }
26
+ /** The request took longer than the configured timeout. */
27
+ export class APITimeoutError extends APIConnectionError {
28
+ constructor(options) {
29
+ super('The request to the Templr API timed out.', options, 'timeout');
30
+ }
31
+ }
32
+ /**
33
+ * The API answered with a success status but a body the SDK does not
34
+ * understand. This usually means the server runs an incompatible version of
35
+ * the API.
36
+ */
37
+ export class UnexpectedResponseError extends TemplrError {
38
+ /** The HTTP status of the response. */
39
+ status;
40
+ /** The raw response body. */
41
+ body;
42
+ constructor(message, status, body, options) {
43
+ super(message, 'unexpected_response', options);
44
+ this.status = status;
45
+ this.body = body;
46
+ }
47
+ }
48
+ /**
49
+ * The storage bucket rejected the file of an image upload: a file of another
50
+ * type or size than the upload was started with, or an expired upload.
51
+ */
52
+ export class UploadError extends TemplrError {
53
+ /** The HTTP status of the bucket response. */
54
+ status;
55
+ /** The raw response body. */
56
+ body;
57
+ constructor(status, body) {
58
+ super(`The storage bucket rejected the file with HTTP ${status}.`, 'upload_failed');
59
+ this.status = status;
60
+ this.body = body;
61
+ }
62
+ }
63
+ /** The API answered with an error status. */
64
+ export class APIError extends TemplrError {
65
+ /** The HTTP status of the response. */
66
+ status;
67
+ /** Every validation problem found; empty for other errors. */
68
+ issues;
69
+ constructor(details) {
70
+ super(`${details.status} ${details.code}: ${details.message}`, details.code);
71
+ this.status = details.status;
72
+ this.issues = details.issues;
73
+ }
74
+ }
75
+ /** 400: the request is invalid (`invalid_input`, `validation_error`). */
76
+ export class BadRequestError extends APIError {
77
+ }
78
+ /** 401: the API key is missing, unknown or expired (`unauthorized`). */
79
+ export class AuthenticationError extends APIError {
80
+ }
81
+ /** 403: the API key scope does not allow this operation (`forbidden`). */
82
+ export class PermissionDeniedError extends APIError {
83
+ }
84
+ /** 404: the resource does not exist or belongs to another workspace (`not_found`). */
85
+ export class NotFoundError extends APIError {
86
+ }
87
+ /**
88
+ * 409: the resource is not in the expected state, e.g. the file is not uploaded
89
+ * yet (`upload_missing`), or another variable already has the new key (`conflict`).
90
+ */
91
+ export class ConflictError extends APIError {
92
+ }
93
+ /** 413: the request body is too large (`payload_too_large`). */
94
+ export class PayloadTooLargeError extends APIError {
95
+ }
96
+ /** 429: the API key is over its rate limit (`rate_limited`). */
97
+ export class RateLimitError extends APIError {
98
+ /** Seconds to wait before retrying, from the `Retry-After` header, when sent. */
99
+ retryAfter;
100
+ constructor(details, retryAfter) {
101
+ super(details);
102
+ this.retryAfter = retryAfter;
103
+ }
104
+ }
105
+ /** 5xx: the server or its storage failed (`internal_error`, `storage_error`, `storage_not_configured`...). */
106
+ export class InternalServerError extends APIError {
107
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Official TypeScript SDK for the Templr REST API.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { Templr } from '@templr/sdk';
7
+ *
8
+ * const templr = new Templr({ apiKey: process.env.TEMPLR_API_KEY ?? '', baseUrl: 'https://templr.example.com' });
9
+ * const email = await templr.templates.render(templateId, { values: { name: 'Ada' } });
10
+ * console.log(email.html);
11
+ * ```
12
+ *
13
+ * @module
14
+ */
15
+ export { Templr, type TemplrOptions } from './client.js';
16
+ export { APIConnectionError, APIError, type APIErrorDetails, APITimeoutError, AuthenticationError, BadRequestError, ConflictError, InternalServerError, NotFoundError, PayloadTooLargeError, PermissionDeniedError, RateLimitError, TemplrError, UnexpectedResponseError, UploadError, } from './errors.js';
17
+ export type { Assets, CreateUploadOptions, UploadOptions } from './resources/assets.js';
18
+ export type { ListOptions, ListVariablesOptions, RenderOptions, Templates, UpdateVariableOptions } from './resources/templates.js';
19
+ export type { Asset, AssetUpload, Deleted, ImageContentType, JsonValue, OpenApiDocument, Page, PresignedUpload, RenderedTemplate, Template, TemplateDocument, TemplateMark, TemplateNode, TemplateSummary, TemplateVariable, TemplateVersion, TemplateVersionSummary, } from './schemas.js';
20
+ export type { Fetch } from './transport.js';
21
+ export { VERSION } from './version.js';
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Official TypeScript SDK for the Templr REST API.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { Templr } from '@templr/sdk';
7
+ *
8
+ * const templr = new Templr({ apiKey: process.env.TEMPLR_API_KEY ?? '', baseUrl: 'https://templr.example.com' });
9
+ * const email = await templr.templates.render(templateId, { values: { name: 'Ada' } });
10
+ * console.log(email.html);
11
+ * ```
12
+ *
13
+ * @module
14
+ */
15
+ export { Templr } from './client.js';
16
+ export { APIConnectionError, APIError, APITimeoutError, AuthenticationError, BadRequestError, ConflictError, InternalServerError, NotFoundError, PayloadTooLargeError, PermissionDeniedError, RateLimitError, TemplrError, UnexpectedResponseError, UploadError, } from './errors.js';
17
+ export { VERSION } from './version.js';