@templr/sdk 0.0.0-stage → 0.2.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 +29 -0
- package/LICENSE +21 -0
- package/README.md +238 -2
- package/dist/client.d.ts +47 -0
- package/dist/client.js +47 -0
- package/dist/errors.d.ts +94 -0
- package/dist/errors.js +107 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +17 -0
- package/dist/resources/assets.d.ts +65 -0
- package/dist/resources/assets.js +68 -0
- package/dist/resources/templates.d.ts +90 -0
- package/dist/resources/templates.js +85 -0
- package/dist/schemas.d.ts +172 -0
- package/dist/schemas.js +68 -0
- package/dist/transport.d.ts +76 -0
- package/dist/transport.js +160 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +49 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
## [0.2.0] - 2026-12-03
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- `order` option (`'desc'`, the default, or `'asc'`) in `templates.list` and `templates.listDrafts` to list the
|
|
15
|
+
oldest templates first, with the `SortOrder` and `ListTemplatesOptions` types.
|
|
16
|
+
|
|
17
|
+
## [0.1.0] - 2026-12-03
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- `Templr` client for the Templr REST API v1, authenticated with a workspace API key. It runs anywhere
|
|
22
|
+
`fetch` exists: Node.js 20.19+, Bun, Deno and edge runtimes.
|
|
23
|
+
- Templates: `list`, `listDrafts`, `get`, `getDraft`, `render`, `listVariables` and `updateVariable`.
|
|
24
|
+
- Assets: `list`, `upload`, `createUpload`, `confirm` and `delete`. `upload` starts an upload, sends the file
|
|
25
|
+
to the storage bucket and confirms it in one call.
|
|
26
|
+
- `getOpenApiDocument` to read the OpenAPI document served by an instance.
|
|
27
|
+
- Responses validated with Zod, returned as frozen objects with `Date` timestamps.
|
|
28
|
+
- Typed errors with a machine-readable `code`, one class per HTTP status family.
|
|
29
|
+
- `UploadError`, thrown when the storage bucket rejects the file of an upload.
|
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,239 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Templr TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
// Oldest first: `order: 'asc'` (the default is 'desc')
|
|
78
|
+
const oldest = await templr.templates.list({ order: 'asc' });
|
|
79
|
+
|
|
80
|
+
// One template, with its document
|
|
81
|
+
const version = await templr.templates.get(templateId); // latest published version
|
|
82
|
+
const draft = await templr.templates.getDraft(templateId); // draft
|
|
83
|
+
|
|
84
|
+
// Render to HTML and plain text; missing values use the placeholder's fallback
|
|
85
|
+
const email = await templr.templates.render(templateId, { values: { name: 'Ada', plan: 'Pro' } });
|
|
86
|
+
const preview = await templr.templates.render(templateId, { draft: true, values: { name: 'Ada' } });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Variables
|
|
90
|
+
|
|
91
|
+
The variables of a template are the values `render` takes. Each key appears once, in document order.
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
const variables = await templr.templates.listVariables(templateId); // { draft: true } for the draft
|
|
95
|
+
for (const variable of variables) {
|
|
96
|
+
console.log(variable.key, variable.fallback); // fallback is null when nothing renders without a value
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Rename a variable of the draft, change its fallback (null for nothing) or both, at every occurrence.
|
|
100
|
+
// Requires a `write` key; published versions are not changed.
|
|
101
|
+
const variable = await templr.templates.updateVariable(templateId, 'name', { fallback: 'there', key: 'first_name' });
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Pagination
|
|
105
|
+
|
|
106
|
+
Lists return a `Page` with `items`, `total` (the size of the whole list) and `nextCursor`, which is
|
|
107
|
+
`null` on the last page. When listing templates with `order`, pass the same `order` with each
|
|
108
|
+
`cursor`.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
let cursor: string | undefined;
|
|
112
|
+
do {
|
|
113
|
+
const page = await templr.templates.list({ cursor, limit: 100 });
|
|
114
|
+
for (const version of page.items) {
|
|
115
|
+
// ...
|
|
116
|
+
}
|
|
117
|
+
cursor = page.nextCursor ?? undefined;
|
|
118
|
+
} while (cursor);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Images
|
|
122
|
+
|
|
123
|
+
`upload` starts the upload, sends the file straight to the storage bucket and confirms it. It requires a
|
|
124
|
+
`write` key; accepted types are gif, jpeg, png and webp, up to 5 MiB. The file can be a `Uint8Array`
|
|
125
|
+
(such as a Node.js `Buffer`) or a `Blob`.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { readFile } from 'node:fs/promises';
|
|
129
|
+
|
|
130
|
+
const asset = await templr.assets.upload(await readFile('logo.png'), { contentType: 'image/png' });
|
|
131
|
+
console.log(asset.url);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The same three steps can also be run one by one:
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const file = await readFile('logo.png');
|
|
138
|
+
|
|
139
|
+
// 1. Start the upload
|
|
140
|
+
const upload = await templr.assets.createUpload({ contentType: 'image/png', sizeBytes: file.byteLength });
|
|
141
|
+
|
|
142
|
+
// 2. Send the fields, then the file as the last field, to the bucket before `upload.expiresAt`
|
|
143
|
+
const form = new FormData();
|
|
144
|
+
for (const [name, value] of Object.entries(upload.upload.fields)) form.append(name, value);
|
|
145
|
+
form.append('file', new Blob([file], { type: 'image/png' }), 'logo.png');
|
|
146
|
+
const response = await fetch(upload.upload.url, { body: form, method: 'POST' });
|
|
147
|
+
if (!response.ok) throw new Error(`Upload failed with ${response.status}`);
|
|
148
|
+
|
|
149
|
+
// 3. Confirm it; the image is now listed, with its public URL
|
|
150
|
+
const confirmed = await templr.assets.confirm(upload.assetId);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
const images = await templr.assets.list();
|
|
155
|
+
await templr.assets.delete(asset.id); // Templates that use it will show a broken image
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Error handling
|
|
159
|
+
|
|
160
|
+
Every error thrown by the SDK extends `TemplrError` and has a machine-readable `code`. Errors returned by
|
|
161
|
+
the API are `APIError` subclasses chosen by HTTP status, with the `code`, `message` and validation
|
|
162
|
+
`issues` sent by the API.
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import { APIConnectionError, APIError, NotFoundError, RateLimitError } from '@templr/sdk';
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
const email = await templr.templates.render(templateId, { values: { name: 'Ada' } });
|
|
169
|
+
} catch (error) {
|
|
170
|
+
if (error instanceof NotFoundError) {
|
|
171
|
+
// The template does not exist, belongs to another workspace, or was never published
|
|
172
|
+
} else if (error instanceof RateLimitError) {
|
|
173
|
+
// Retry after `error.retryAfter` seconds
|
|
174
|
+
} else if (error instanceof APIError) {
|
|
175
|
+
console.error(error.status, error.code, error.message, error.issues);
|
|
176
|
+
} else if (error instanceof APIConnectionError) {
|
|
177
|
+
// The server could not be reached, or the request timed out (`APITimeoutError`)
|
|
178
|
+
} else {
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
| Error | Status | API codes |
|
|
185
|
+
| ------------------------- | ------ | ---------------------------------------------------------------------------------------------- |
|
|
186
|
+
| `BadRequestError` | 400 | `invalid_input`, `validation_error` |
|
|
187
|
+
| `AuthenticationError` | 401 | `unauthorized` |
|
|
188
|
+
| `PermissionDeniedError` | 403 | `forbidden` |
|
|
189
|
+
| `NotFoundError` | 404 | `not_found` |
|
|
190
|
+
| `ConflictError` | 409 | `upload_missing`, `conflict` |
|
|
191
|
+
| `PayloadTooLargeError` | 413 | `payload_too_large` |
|
|
192
|
+
| `RateLimitError` | 429 | `rate_limited` |
|
|
193
|
+
| `InternalServerError` | 5xx | `internal_error`, `database_error`, `render_failed`, `storage_error`, `storage_not_configured` |
|
|
194
|
+
| `UploadError` | bucket | `upload_failed`: the storage bucket rejected the file sent by `assets.upload` |
|
|
195
|
+
| `APIConnectionError` | none | `connection_error` |
|
|
196
|
+
| `APITimeoutError` | none | `timeout` |
|
|
197
|
+
| `UnexpectedResponseError` | 2xx | `unexpected_response`: the server answered with a body this SDK does not understand |
|
|
198
|
+
|
|
199
|
+
An error response without a Templr error body (for example, an HTML page from a proxy) throws the class
|
|
200
|
+
of its status with the code `unknown_error`. Invalid options (an empty `apiKey`, or a `baseUrl` that is
|
|
201
|
+
not an http(s) URL) throw a `TypeError` from the constructor.
|
|
202
|
+
|
|
203
|
+
## Configuration
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
const templr = new Templr({
|
|
207
|
+
apiKey: process.env.TEMPLR_API_KEY ?? '',
|
|
208
|
+
baseUrl: 'https://templr.example.com',
|
|
209
|
+
timeoutMs: 10_000, // Milliseconds per request (default: 30 000)
|
|
210
|
+
fetch: myFetch, // Optional: any fetch-compatible function (proxies, retries, logging...)
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Versioning
|
|
215
|
+
|
|
216
|
+
The SDK follows [Semantic Versioning](https://semver.org/) and is versioned independently of the Templr
|
|
217
|
+
app. Each release is tagged `sdks/typescript/vX.Y.Z`; see the [changelog](CHANGELOG.md).
|
|
218
|
+
|
|
219
|
+
## Development
|
|
220
|
+
|
|
221
|
+
The SDK lives in `sdks/typescript` of the Templr repository, outside the app's Bun workspaces, and uses
|
|
222
|
+
[Bun](https://bun.sh/) for installing and testing.
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
cd sdks/typescript
|
|
226
|
+
bun install --frozen-lockfile # Install the exact versions of bun.lock
|
|
227
|
+
bun run lint # Biome, with the repository's rules
|
|
228
|
+
bun run typecheck # Strict type checking
|
|
229
|
+
bun test # Tests (local only, no network)
|
|
230
|
+
bun run build # Compile to dist/
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
From the repository root, `make sdk-typescript-check` runs all of them, and `make sdk-typescript-spec`
|
|
234
|
+
refreshes `tests/openapi.json`, the copy of the API's OpenAPI document that the contract tests check the
|
|
235
|
+
SDK against. See [RELEASING.md](RELEASING.md) to publish a new version.
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
[MIT](LICENSE). The Templr app itself is licensed under the PolyForm Noncommercial License 1.0.0 (see the root LICENSE).
|
package/dist/client.d.ts
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 { 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
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -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
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -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, ListTemplatesOptions, ListVariablesOptions, RenderOptions, SortOrder, 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';
|