@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
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `assets` resource: upload, list and delete the images used by templates.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Asset, type AssetUpload, type Deleted, type ImageContentType, type Page } from '../schemas.js';
|
|
7
|
+
import { type Transport } from '../transport.js';
|
|
8
|
+
import type { ListOptions } from './templates.js';
|
|
9
|
+
/** Options of {@link Assets.upload}. */
|
|
10
|
+
export interface UploadOptions {
|
|
11
|
+
/** The image type. */
|
|
12
|
+
readonly contentType: ImageContentType;
|
|
13
|
+
}
|
|
14
|
+
/** Options of {@link Assets.createUpload}. */
|
|
15
|
+
export interface CreateUploadOptions {
|
|
16
|
+
/** The image type. */
|
|
17
|
+
readonly contentType: ImageContentType;
|
|
18
|
+
/** The exact size of the file, in bytes (at most 5 MiB). */
|
|
19
|
+
readonly sizeBytes: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Images of the workspace.
|
|
23
|
+
*
|
|
24
|
+
* {@link Assets.upload} uploads an image in one call. It takes three steps,
|
|
25
|
+
* which can also be run one by one: {@link Assets.createUpload}, sending the
|
|
26
|
+
* file to the returned presigned URL, then {@link Assets.confirm}.
|
|
27
|
+
*/
|
|
28
|
+
export declare class Assets {
|
|
29
|
+
#private;
|
|
30
|
+
/** @internal */
|
|
31
|
+
constructor(transport: Transport);
|
|
32
|
+
/** Lists the confirmed images, newest first. Requires a `read` or `write` API key. */
|
|
33
|
+
list(options?: ListOptions): Promise<Page<Asset>>;
|
|
34
|
+
/**
|
|
35
|
+
* Uploads an image: starts the upload, sends the file to the storage bucket
|
|
36
|
+
* and confirms it. Requires a `write` API key.
|
|
37
|
+
*
|
|
38
|
+
* @param file - The image, at most 5 MiB.
|
|
39
|
+
* @returns The confirmed image, with its public URL.
|
|
40
|
+
* @throws PayloadTooLargeError when the file is larger than 5 MiB.
|
|
41
|
+
* @throws UploadError when the storage bucket rejects the file.
|
|
42
|
+
*/
|
|
43
|
+
upload(file: Blob | Uint8Array<ArrayBuffer>, options: UploadOptions): Promise<Asset>;
|
|
44
|
+
/**
|
|
45
|
+
* Starts an image upload. Requires a `write` API key.
|
|
46
|
+
*
|
|
47
|
+
* Send `upload.fields`, then the file as the last field, as
|
|
48
|
+
* `multipart/form-data` to `upload.url` before `expiresAt`; then call
|
|
49
|
+
* {@link Assets.confirm}.
|
|
50
|
+
*/
|
|
51
|
+
createUpload(options: CreateUploadOptions): Promise<AssetUpload>;
|
|
52
|
+
/**
|
|
53
|
+
* Confirms an upload once the file is uploaded. Confirming twice is harmless. Requires a `write` API key.
|
|
54
|
+
*
|
|
55
|
+
* @throws ConflictError when the file is not uploaded yet (`upload_missing`).
|
|
56
|
+
* @throws NotFoundError when the upload does not exist.
|
|
57
|
+
*/
|
|
58
|
+
confirm(assetId: string): Promise<Asset>;
|
|
59
|
+
/**
|
|
60
|
+
* Deletes an image. Templates that use it will show a broken image. Requires a `write` API key.
|
|
61
|
+
*
|
|
62
|
+
* @throws NotFoundError when the image does not exist.
|
|
63
|
+
*/
|
|
64
|
+
delete(assetId: string): Promise<Deleted>;
|
|
65
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `assets` resource: upload, list and delete the images used by templates.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { assetSchema, assetUploadSchema, deletedSchema, pageSchema } from '../schemas.js';
|
|
7
|
+
import { pathId } from '../transport.js';
|
|
8
|
+
const assetPage = pageSchema(assetSchema);
|
|
9
|
+
/**
|
|
10
|
+
* Images of the workspace.
|
|
11
|
+
*
|
|
12
|
+
* {@link Assets.upload} uploads an image in one call. It takes three steps,
|
|
13
|
+
* which can also be run one by one: {@link Assets.createUpload}, sending the
|
|
14
|
+
* file to the returned presigned URL, then {@link Assets.confirm}.
|
|
15
|
+
*/
|
|
16
|
+
export class Assets {
|
|
17
|
+
#transport;
|
|
18
|
+
/** @internal */
|
|
19
|
+
constructor(transport) {
|
|
20
|
+
this.#transport = transport;
|
|
21
|
+
}
|
|
22
|
+
/** Lists the confirmed images, newest first. Requires a `read` or `write` API key. */
|
|
23
|
+
list(options = {}) {
|
|
24
|
+
return this.#transport.send({ method: 'GET', path: '/api/v1/assets', query: { cursor: options.cursor, limit: options.limit } }, assetPage);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Uploads an image: starts the upload, sends the file to the storage bucket
|
|
28
|
+
* and confirms it. Requires a `write` API key.
|
|
29
|
+
*
|
|
30
|
+
* @param file - The image, at most 5 MiB.
|
|
31
|
+
* @returns The confirmed image, with its public URL.
|
|
32
|
+
* @throws PayloadTooLargeError when the file is larger than 5 MiB.
|
|
33
|
+
* @throws UploadError when the storage bucket rejects the file.
|
|
34
|
+
*/
|
|
35
|
+
async upload(file, options) {
|
|
36
|
+
const sizeBytes = file instanceof Blob ? file.size : file.byteLength;
|
|
37
|
+
const upload = await this.createUpload({ contentType: options.contentType, sizeBytes });
|
|
38
|
+
await this.#transport.upload({ contentType: options.contentType, fields: upload.upload.fields, file, url: upload.upload.url });
|
|
39
|
+
return this.confirm(upload.assetId);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Starts an image upload. Requires a `write` API key.
|
|
43
|
+
*
|
|
44
|
+
* Send `upload.fields`, then the file as the last field, as
|
|
45
|
+
* `multipart/form-data` to `upload.url` before `expiresAt`; then call
|
|
46
|
+
* {@link Assets.confirm}.
|
|
47
|
+
*/
|
|
48
|
+
createUpload(options) {
|
|
49
|
+
return this.#transport.send({ body: { contentType: options.contentType, sizeBytes: options.sizeBytes }, method: 'POST', path: '/api/v1/assets' }, assetUploadSchema);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Confirms an upload once the file is uploaded. Confirming twice is harmless. Requires a `write` API key.
|
|
53
|
+
*
|
|
54
|
+
* @throws ConflictError when the file is not uploaded yet (`upload_missing`).
|
|
55
|
+
* @throws NotFoundError when the upload does not exist.
|
|
56
|
+
*/
|
|
57
|
+
confirm(assetId) {
|
|
58
|
+
return this.#transport.send({ method: 'POST', path: `/api/v1/assets/${pathId(assetId)}/confirm` }, assetSchema);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Deletes an image. Templates that use it will show a broken image. Requires a `write` API key.
|
|
62
|
+
*
|
|
63
|
+
* @throws NotFoundError when the image does not exist.
|
|
64
|
+
*/
|
|
65
|
+
delete(assetId) {
|
|
66
|
+
return this.#transport.send({ method: 'DELETE', path: `/api/v1/assets/${pathId(assetId)}` }, deletedSchema);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `templates` resource: list, get and render templates, and manage their variables.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Page, type RenderedTemplate, type Template, type TemplateSummary, type TemplateVariable, type TemplateVersion, type TemplateVersionSummary } from '../schemas.js';
|
|
7
|
+
import { type Transport } from '../transport.js';
|
|
8
|
+
/** Options of the list methods. */
|
|
9
|
+
export interface ListOptions {
|
|
10
|
+
/** The `nextCursor` of the previous page; omit it for the first page. */
|
|
11
|
+
readonly cursor?: string;
|
|
12
|
+
/** Items per page, from 1 to 100 (the API defaults to 20). */
|
|
13
|
+
readonly limit?: number;
|
|
14
|
+
}
|
|
15
|
+
/** The order of the template lists: newest first (`desc`, the default) or oldest first (`asc`). */
|
|
16
|
+
export type SortOrder = 'desc' | 'asc';
|
|
17
|
+
/** Options of {@link Templates.list} and {@link Templates.listDrafts}. */
|
|
18
|
+
export interface ListTemplatesOptions extends ListOptions {
|
|
19
|
+
/** `asc` lists the oldest first; keep the same order when passing `cursor`. The API defaults to `desc`. */
|
|
20
|
+
readonly order?: SortOrder;
|
|
21
|
+
}
|
|
22
|
+
/** Options of {@link Templates.render}. */
|
|
23
|
+
export interface RenderOptions {
|
|
24
|
+
/** Render the draft instead of the latest published version. */
|
|
25
|
+
readonly draft?: boolean;
|
|
26
|
+
/** Values for the placeholders, by key; missing keys use the placeholder's fallback. */
|
|
27
|
+
readonly values?: Readonly<Record<string, string>>;
|
|
28
|
+
}
|
|
29
|
+
/** Options of {@link Templates.listVariables}. */
|
|
30
|
+
export interface ListVariablesOptions {
|
|
31
|
+
/** List the variables of the draft instead of the latest published version. */
|
|
32
|
+
readonly draft?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** A change to a variable of the draft; give `key`, `fallback` or both. */
|
|
35
|
+
export interface UpdateVariableOptions {
|
|
36
|
+
/** What renders without a value; `null` for nothing. Omit it to keep the current one. */
|
|
37
|
+
readonly fallback?: string | null;
|
|
38
|
+
/** The new key: letters, digits and `_`, starting with a letter or `_`. */
|
|
39
|
+
readonly key?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Templates of the workspace. Requires a `read` or `write` API key.
|
|
43
|
+
*
|
|
44
|
+
* By default the API serves the latest **published** version of each template;
|
|
45
|
+
* the `*Draft*` methods and `draft: true` serve the draft editors work on.
|
|
46
|
+
*/
|
|
47
|
+
export declare class Templates {
|
|
48
|
+
#private;
|
|
49
|
+
/** @internal */
|
|
50
|
+
constructor(transport: Transport);
|
|
51
|
+
/** Lists the published templates, each with its latest version, most recently published first (oldest first with `order: 'asc'`). */
|
|
52
|
+
list(options?: ListTemplatesOptions): Promise<Page<TemplateVersionSummary>>;
|
|
53
|
+
/** Lists every template with its draft, most recently updated first (oldest first with `order: 'asc'`). */
|
|
54
|
+
listDrafts(options?: ListTemplatesOptions): Promise<Page<TemplateSummary>>;
|
|
55
|
+
/**
|
|
56
|
+
* Gets the latest published version of a template, with its document.
|
|
57
|
+
*
|
|
58
|
+
* @throws NotFoundError when the template does not exist or was never published.
|
|
59
|
+
*/
|
|
60
|
+
get(templateId: string): Promise<TemplateVersion>;
|
|
61
|
+
/**
|
|
62
|
+
* Gets the draft of a template, with its document.
|
|
63
|
+
*
|
|
64
|
+
* @throws NotFoundError when the template does not exist.
|
|
65
|
+
*/
|
|
66
|
+
getDraft(templateId: string): Promise<Template>;
|
|
67
|
+
/**
|
|
68
|
+
* Renders a template to email HTML and plain text.
|
|
69
|
+
*
|
|
70
|
+
* @throws NotFoundError when the template does not exist, or was never published and `draft` is not set.
|
|
71
|
+
*/
|
|
72
|
+
render(templateId: string, options?: RenderOptions): Promise<RenderedTemplate>;
|
|
73
|
+
/**
|
|
74
|
+
* Lists the variables of a template: the values {@link Templates.render} takes.
|
|
75
|
+
* Each key appears once, in document order; the list is not paginated.
|
|
76
|
+
*
|
|
77
|
+
* @throws NotFoundError when the template does not exist, or was never published and `draft` is not set.
|
|
78
|
+
*/
|
|
79
|
+
listVariables(templateId: string, options?: ListVariablesOptions): Promise<readonly TemplateVariable[]>;
|
|
80
|
+
/**
|
|
81
|
+
* Renames a variable of the draft, changes its fallback or both, at every
|
|
82
|
+
* occurrence, and returns the variable as it is now. Published versions are
|
|
83
|
+
* not changed. Requires a `write` API key.
|
|
84
|
+
*
|
|
85
|
+
* @throws BadRequestError when neither `key` nor `fallback` is given, or the new key is invalid.
|
|
86
|
+
* @throws NotFoundError when the template or the variable does not exist.
|
|
87
|
+
* @throws ConflictError when another variable already has the new key (`conflict`).
|
|
88
|
+
*/
|
|
89
|
+
updateVariable(templateId: string, key: string, change: UpdateVariableOptions): Promise<TemplateVariable>;
|
|
90
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `templates` resource: list, get and render templates, and manage their variables.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { pageSchema, renderedTemplateSchema, templateSchema, templateSummarySchema, templateVariableListSchema, templateVariableSchema, templateVersionSchema, templateVersionSummarySchema, } from '../schemas.js';
|
|
7
|
+
import { pathId } from '../transport.js';
|
|
8
|
+
const versionPage = pageSchema(templateVersionSummarySchema);
|
|
9
|
+
const draftPage = pageSchema(templateSummarySchema);
|
|
10
|
+
/**
|
|
11
|
+
* Templates of the workspace. Requires a `read` or `write` API key.
|
|
12
|
+
*
|
|
13
|
+
* By default the API serves the latest **published** version of each template;
|
|
14
|
+
* the `*Draft*` methods and `draft: true` serve the draft editors work on.
|
|
15
|
+
*/
|
|
16
|
+
export class Templates {
|
|
17
|
+
#transport;
|
|
18
|
+
/** @internal */
|
|
19
|
+
constructor(transport) {
|
|
20
|
+
this.#transport = transport;
|
|
21
|
+
}
|
|
22
|
+
/** Lists the published templates, each with its latest version, most recently published first (oldest first with `order: 'asc'`). */
|
|
23
|
+
list(options = {}) {
|
|
24
|
+
return this.#transport.send({ method: 'GET', path: '/api/v1/templates', query: { cursor: options.cursor, limit: options.limit, order: options.order } }, versionPage);
|
|
25
|
+
}
|
|
26
|
+
/** Lists every template with its draft, most recently updated first (oldest first with `order: 'asc'`). */
|
|
27
|
+
listDrafts(options = {}) {
|
|
28
|
+
return this.#transport.send({ method: 'GET', path: '/api/v1/templates', query: { cursor: options.cursor, draft: 'true', limit: options.limit, order: options.order } }, draftPage);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Gets the latest published version of a template, with its document.
|
|
32
|
+
*
|
|
33
|
+
* @throws NotFoundError when the template does not exist or was never published.
|
|
34
|
+
*/
|
|
35
|
+
get(templateId) {
|
|
36
|
+
return this.#transport.send({ method: 'GET', path: `/api/v1/templates/${pathId(templateId)}` }, templateVersionSchema);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Gets the draft of a template, with its document.
|
|
40
|
+
*
|
|
41
|
+
* @throws NotFoundError when the template does not exist.
|
|
42
|
+
*/
|
|
43
|
+
getDraft(templateId) {
|
|
44
|
+
return this.#transport.send({ method: 'GET', path: `/api/v1/templates/${pathId(templateId)}`, query: { draft: 'true' } }, templateSchema);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Renders a template to email HTML and plain text.
|
|
48
|
+
*
|
|
49
|
+
* @throws NotFoundError when the template does not exist, or was never published and `draft` is not set.
|
|
50
|
+
*/
|
|
51
|
+
render(templateId, options = {}) {
|
|
52
|
+
return this.#transport.send({
|
|
53
|
+
body: options.values ? { values: options.values } : {},
|
|
54
|
+
method: 'POST',
|
|
55
|
+
path: `/api/v1/templates/${pathId(templateId)}/render`,
|
|
56
|
+
query: { draft: options.draft ? 'true' : undefined },
|
|
57
|
+
}, renderedTemplateSchema);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Lists the variables of a template: the values {@link Templates.render} takes.
|
|
61
|
+
* Each key appears once, in document order; the list is not paginated.
|
|
62
|
+
*
|
|
63
|
+
* @throws NotFoundError when the template does not exist, or was never published and `draft` is not set.
|
|
64
|
+
*/
|
|
65
|
+
async listVariables(templateId, options = {}) {
|
|
66
|
+
const list = await this.#transport.send({ method: 'GET', path: `/api/v1/templates/${pathId(templateId)}/variables`, query: { draft: options.draft ? 'true' : undefined } }, templateVariableListSchema);
|
|
67
|
+
return list.items;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Renames a variable of the draft, changes its fallback or both, at every
|
|
71
|
+
* occurrence, and returns the variable as it is now. Published versions are
|
|
72
|
+
* not changed. Requires a `write` API key.
|
|
73
|
+
*
|
|
74
|
+
* @throws BadRequestError when neither `key` nor `fallback` is given, or the new key is invalid.
|
|
75
|
+
* @throws NotFoundError when the template or the variable does not exist.
|
|
76
|
+
* @throws ConflictError when another variable already has the new key (`conflict`).
|
|
77
|
+
*/
|
|
78
|
+
updateVariable(templateId, key, change) {
|
|
79
|
+
return this.#transport.send({
|
|
80
|
+
body: { ...(change.key === undefined ? {} : { key: change.key }), ...(change.fallback === undefined ? {} : { fallback: change.fallback }) },
|
|
81
|
+
method: 'PATCH',
|
|
82
|
+
path: `/api/v1/templates/${pathId(templateId)}/variables/${pathId(key)}`,
|
|
83
|
+
}, templateVariableSchema);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schemas of the Templr REST API v1 responses, and the types inferred
|
|
3
|
+
* from them. Every response is validated before it reaches the caller.
|
|
4
|
+
* Unknown fields are dropped, so newer servers that add fields keep working
|
|
5
|
+
* with this SDK. Results are frozen. Timestamps become `Date` objects.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
/** A JSON value. */
|
|
11
|
+
export type JsonValue = string | number | boolean | null | readonly JsonValue[] | {
|
|
12
|
+
readonly [key: string]: JsonValue;
|
|
13
|
+
};
|
|
14
|
+
/** The image types the API accepts for uploads. */
|
|
15
|
+
export type ImageContentType = 'image/gif' | 'image/jpeg' | 'image/png' | 'image/webp';
|
|
16
|
+
/** A mark applied to text in a template document, e.g. bold or a link. */
|
|
17
|
+
export interface TemplateMark {
|
|
18
|
+
readonly attrs?: Readonly<Record<string, JsonValue>> | undefined;
|
|
19
|
+
readonly type: string;
|
|
20
|
+
}
|
|
21
|
+
/** A node of a template document, e.g. a paragraph, an image or a text run. */
|
|
22
|
+
export interface TemplateNode {
|
|
23
|
+
readonly attrs?: Readonly<Record<string, JsonValue>> | undefined;
|
|
24
|
+
readonly content?: readonly TemplateNode[] | undefined;
|
|
25
|
+
readonly marks?: readonly TemplateMark[] | undefined;
|
|
26
|
+
readonly text?: string | undefined;
|
|
27
|
+
readonly type: string;
|
|
28
|
+
}
|
|
29
|
+
/** The editable document of a template. */
|
|
30
|
+
export interface TemplateDocument {
|
|
31
|
+
readonly content: readonly TemplateNode[];
|
|
32
|
+
readonly type: 'doc';
|
|
33
|
+
}
|
|
34
|
+
/** A template without its document (the draft editors work on). */
|
|
35
|
+
export declare const templateSummarySchema: z.ZodReadonly<z.ZodObject<{
|
|
36
|
+
createdAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
37
|
+
id: z.ZodString;
|
|
38
|
+
name: z.ZodString;
|
|
39
|
+
updatedAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
40
|
+
workspaceId: z.ZodString;
|
|
41
|
+
}, z.core.$strip>>;
|
|
42
|
+
/** A template with its draft document. */
|
|
43
|
+
export declare const templateSchema: z.ZodReadonly<z.ZodObject<{
|
|
44
|
+
createdAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
45
|
+
id: z.ZodString;
|
|
46
|
+
name: z.ZodString;
|
|
47
|
+
updatedAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
48
|
+
workspaceId: z.ZodString;
|
|
49
|
+
document: z.ZodType<TemplateDocument, unknown, z.core.$ZodTypeInternals<TemplateDocument, unknown>>;
|
|
50
|
+
}, z.core.$strip>>;
|
|
51
|
+
/** A published version of a template, without its document. */
|
|
52
|
+
export declare const templateVersionSummarySchema: z.ZodReadonly<z.ZodObject<{
|
|
53
|
+
id: z.ZodString;
|
|
54
|
+
name: z.ZodString;
|
|
55
|
+
publishedAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
56
|
+
templateId: z.ZodString;
|
|
57
|
+
version: z.ZodNumber;
|
|
58
|
+
workspaceId: z.ZodString;
|
|
59
|
+
}, z.core.$strip>>;
|
|
60
|
+
/** A published version of a template, with its document. */
|
|
61
|
+
export declare const templateVersionSchema: z.ZodReadonly<z.ZodObject<{
|
|
62
|
+
id: z.ZodString;
|
|
63
|
+
name: z.ZodString;
|
|
64
|
+
publishedAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
65
|
+
templateId: z.ZodString;
|
|
66
|
+
version: z.ZodNumber;
|
|
67
|
+
workspaceId: z.ZodString;
|
|
68
|
+
document: z.ZodType<TemplateDocument, unknown, z.core.$ZodTypeInternals<TemplateDocument, unknown>>;
|
|
69
|
+
}, z.core.$strip>>;
|
|
70
|
+
/** A rendered email: the HTML and its plain text version. */
|
|
71
|
+
export declare const renderedTemplateSchema: z.ZodReadonly<z.ZodObject<{
|
|
72
|
+
html: z.ZodString;
|
|
73
|
+
text: z.ZodString;
|
|
74
|
+
}, z.core.$strip>>;
|
|
75
|
+
/** A variable of a template: the key its value is sent with, and what renders without one (`null` for nothing). */
|
|
76
|
+
export declare const templateVariableSchema: z.ZodReadonly<z.ZodObject<{
|
|
77
|
+
fallback: z.ZodNullable<z.ZodString>;
|
|
78
|
+
key: z.ZodString;
|
|
79
|
+
}, z.core.$strip>>;
|
|
80
|
+
/** Every variable of a template, once per key, in document order. */
|
|
81
|
+
export declare const templateVariableListSchema: z.ZodReadonly<z.ZodObject<{
|
|
82
|
+
items: z.ZodReadonly<z.ZodArray<z.ZodReadonly<z.ZodObject<{
|
|
83
|
+
fallback: z.ZodNullable<z.ZodString>;
|
|
84
|
+
key: z.ZodString;
|
|
85
|
+
}, z.core.$strip>>>>;
|
|
86
|
+
}, z.core.$strip>>;
|
|
87
|
+
/** An uploaded image with its public URL. */
|
|
88
|
+
export declare const assetSchema: z.ZodReadonly<z.ZodObject<{
|
|
89
|
+
contentType: z.ZodString;
|
|
90
|
+
createdAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
91
|
+
id: z.ZodString;
|
|
92
|
+
sizeBytes: z.ZodNumber;
|
|
93
|
+
url: z.ZodString;
|
|
94
|
+
workspaceId: z.ZodString;
|
|
95
|
+
}, z.core.$strip>>;
|
|
96
|
+
/** Where and how to upload the file of a pending image upload. */
|
|
97
|
+
export declare const presignedUploadSchema: z.ZodReadonly<z.ZodObject<{
|
|
98
|
+
fields: z.ZodReadonly<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
99
|
+
url: z.ZodString;
|
|
100
|
+
}, z.core.$strip>>;
|
|
101
|
+
/** A pending image upload. */
|
|
102
|
+
export declare const assetUploadSchema: z.ZodReadonly<z.ZodObject<{
|
|
103
|
+
assetId: z.ZodString;
|
|
104
|
+
expiresAt: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
105
|
+
upload: z.ZodReadonly<z.ZodObject<{
|
|
106
|
+
fields: z.ZodReadonly<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
107
|
+
url: z.ZodString;
|
|
108
|
+
}, z.core.$strip>>;
|
|
109
|
+
url: z.ZodString;
|
|
110
|
+
}, z.core.$strip>>;
|
|
111
|
+
/** The id of a deleted resource. */
|
|
112
|
+
export declare const deletedSchema: z.ZodReadonly<z.ZodObject<{
|
|
113
|
+
id: z.ZodString;
|
|
114
|
+
}, z.core.$strip>>;
|
|
115
|
+
/** The OpenAPI document served by an instance. */
|
|
116
|
+
export declare const openApiDocumentSchema: z.ZodReadonly<z.ZodRecord<z.ZodString, z.ZodType<JsonValue, unknown, z.core.$ZodTypeInternals<JsonValue, unknown>>>>;
|
|
117
|
+
/**
|
|
118
|
+
* The schema of a page of a list.
|
|
119
|
+
*
|
|
120
|
+
* @param item - The schema of an item.
|
|
121
|
+
*/
|
|
122
|
+
export declare function pageSchema<Item extends z.ZodType>(item: Item): z.ZodReadonly<z.ZodObject<{
|
|
123
|
+
items: z.ZodReadonly<z.ZodArray<Item>>;
|
|
124
|
+
nextCursor: z.ZodNullable<z.ZodString>;
|
|
125
|
+
total: z.ZodNumber;
|
|
126
|
+
}, z.core.$strip>>;
|
|
127
|
+
/** The body of every error response. */
|
|
128
|
+
export declare const errorBodySchema: z.ZodObject<{
|
|
129
|
+
error: z.ZodObject<{
|
|
130
|
+
code: z.ZodString;
|
|
131
|
+
issues: z.ZodArray<z.ZodString>;
|
|
132
|
+
message: z.ZodString;
|
|
133
|
+
}, z.core.$strip>;
|
|
134
|
+
}, z.core.$strip>;
|
|
135
|
+
/**
|
|
136
|
+
* A page of a list. Pass `nextCursor` as `cursor` to get the next page; it is
|
|
137
|
+
* `null` on the last page. `total` counts the whole list.
|
|
138
|
+
*/
|
|
139
|
+
export interface Page<Item> {
|
|
140
|
+
readonly items: readonly Item[];
|
|
141
|
+
readonly nextCursor: string | null;
|
|
142
|
+
readonly total: number;
|
|
143
|
+
}
|
|
144
|
+
/** A template without its document (the draft editors work on). */
|
|
145
|
+
export type TemplateSummary = z.output<typeof templateSummarySchema>;
|
|
146
|
+
/** A template with its draft document. */
|
|
147
|
+
export type Template = z.output<typeof templateSchema>;
|
|
148
|
+
/** A published version of a template, without its document. */
|
|
149
|
+
export type TemplateVersionSummary = z.output<typeof templateVersionSummarySchema>;
|
|
150
|
+
/** A published version of a template, with its document. */
|
|
151
|
+
export type TemplateVersion = z.output<typeof templateVersionSchema>;
|
|
152
|
+
/** A variable of a template: the key its value is sent with, and what renders without one (`null` for nothing). */
|
|
153
|
+
export type TemplateVariable = z.output<typeof templateVariableSchema>;
|
|
154
|
+
/** A rendered email: `html` and its plain `text` version. */
|
|
155
|
+
export type RenderedTemplate = z.output<typeof renderedTemplateSchema>;
|
|
156
|
+
/** An uploaded image with its public URL. */
|
|
157
|
+
export type Asset = z.output<typeof assetSchema>;
|
|
158
|
+
/**
|
|
159
|
+
* Where and how to upload the file of a pending image upload: send every
|
|
160
|
+
* entry of `fields`, then the file as the last field, as `multipart/form-data`
|
|
161
|
+
* to `url`.
|
|
162
|
+
*/
|
|
163
|
+
export type PresignedUpload = z.output<typeof presignedUploadSchema>;
|
|
164
|
+
/**
|
|
165
|
+
* A pending image upload: upload the file with `upload` before `expiresAt`,
|
|
166
|
+
* then confirm it with `assetId`. `url` is the public URL the image will have.
|
|
167
|
+
*/
|
|
168
|
+
export type AssetUpload = z.output<typeof assetUploadSchema>;
|
|
169
|
+
/** The id of a deleted resource. */
|
|
170
|
+
export type Deleted = z.output<typeof deletedSchema>;
|
|
171
|
+
/** The OpenAPI 3.1 document served by an instance. */
|
|
172
|
+
export type OpenApiDocument = z.output<typeof openApiDocumentSchema>;
|
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schemas of the Templr REST API v1 responses, and the types inferred
|
|
3
|
+
* from them. Every response is validated before it reaches the caller.
|
|
4
|
+
* Unknown fields are dropped, so newer servers that add fields keep working
|
|
5
|
+
* with this SDK. Results are frozen. Timestamps become `Date` objects.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
const jsonValue = z.json();
|
|
11
|
+
const timestamp = z.iso.datetime({ offset: true }).transform(value => new Date(value));
|
|
12
|
+
const attrs = z.record(z.string(), jsonValue).readonly();
|
|
13
|
+
const templateMark = z.object({ attrs: attrs.optional(), type: z.string() }).readonly();
|
|
14
|
+
const templateNode = z
|
|
15
|
+
.object({
|
|
16
|
+
attrs: attrs.optional(),
|
|
17
|
+
get content() {
|
|
18
|
+
return z.array(templateNode).readonly().optional();
|
|
19
|
+
},
|
|
20
|
+
marks: z.array(templateMark).readonly().optional(),
|
|
21
|
+
text: z.string().optional(),
|
|
22
|
+
type: z.string(),
|
|
23
|
+
})
|
|
24
|
+
.readonly();
|
|
25
|
+
const templateDocument = z.object({ content: z.array(templateNode).readonly(), type: z.literal('doc') }).readonly();
|
|
26
|
+
const templateSummaryShape = { createdAt: timestamp, id: z.string(), name: z.string(), updatedAt: timestamp, workspaceId: z.string() };
|
|
27
|
+
const templateVersionSummaryShape = {
|
|
28
|
+
id: z.string(),
|
|
29
|
+
name: z.string(),
|
|
30
|
+
publishedAt: timestamp,
|
|
31
|
+
templateId: z.string(),
|
|
32
|
+
version: z.number().int(),
|
|
33
|
+
workspaceId: z.string(),
|
|
34
|
+
};
|
|
35
|
+
/** A template without its document (the draft editors work on). */
|
|
36
|
+
export const templateSummarySchema = z.object(templateSummaryShape).readonly();
|
|
37
|
+
/** A template with its draft document. */
|
|
38
|
+
export const templateSchema = z.object({ ...templateSummaryShape, document: templateDocument }).readonly();
|
|
39
|
+
/** A published version of a template, without its document. */
|
|
40
|
+
export const templateVersionSummarySchema = z.object(templateVersionSummaryShape).readonly();
|
|
41
|
+
/** A published version of a template, with its document. */
|
|
42
|
+
export const templateVersionSchema = z.object({ ...templateVersionSummaryShape, document: templateDocument }).readonly();
|
|
43
|
+
/** A rendered email: the HTML and its plain text version. */
|
|
44
|
+
export const renderedTemplateSchema = z.object({ html: z.string(), text: z.string() }).readonly();
|
|
45
|
+
/** A variable of a template: the key its value is sent with, and what renders without one (`null` for nothing). */
|
|
46
|
+
export const templateVariableSchema = z.object({ fallback: z.string().nullable(), key: z.string() }).readonly();
|
|
47
|
+
/** Every variable of a template, once per key, in document order. */
|
|
48
|
+
export const templateVariableListSchema = z.object({ items: z.array(templateVariableSchema).readonly() }).readonly();
|
|
49
|
+
/** An uploaded image with its public URL. */
|
|
50
|
+
export const assetSchema = z.object({ contentType: z.string(), createdAt: timestamp, id: z.string(), sizeBytes: z.number().int(), url: z.string(), workspaceId: z.string() }).readonly();
|
|
51
|
+
/** Where and how to upload the file of a pending image upload. */
|
|
52
|
+
export const presignedUploadSchema = z.object({ fields: z.record(z.string(), z.string()).readonly(), url: z.string() }).readonly();
|
|
53
|
+
/** A pending image upload. */
|
|
54
|
+
export const assetUploadSchema = z.object({ assetId: z.string(), expiresAt: timestamp, upload: presignedUploadSchema, url: z.string() }).readonly();
|
|
55
|
+
/** The id of a deleted resource. */
|
|
56
|
+
export const deletedSchema = z.object({ id: z.string() }).readonly();
|
|
57
|
+
/** The OpenAPI document served by an instance. */
|
|
58
|
+
export const openApiDocumentSchema = z.record(z.string(), jsonValue).readonly();
|
|
59
|
+
/**
|
|
60
|
+
* The schema of a page of a list.
|
|
61
|
+
*
|
|
62
|
+
* @param item - The schema of an item.
|
|
63
|
+
*/
|
|
64
|
+
export function pageSchema(item) {
|
|
65
|
+
return z.object({ items: z.array(item).readonly(), nextCursor: z.string().nullable(), total: z.number().int() }).readonly();
|
|
66
|
+
}
|
|
67
|
+
/** The body of every error response. */
|
|
68
|
+
export const errorBodySchema = z.object({ error: z.object({ code: z.string(), issues: z.array(z.string()), message: z.string() }) });
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP plumbing of the client: builds requests, sends them with `fetch`,
|
|
3
|
+
* validates responses and maps errors. Internal; not exported by the package.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { z } from 'zod';
|
|
8
|
+
import { APIError } from './errors.js';
|
|
9
|
+
import { type JsonValue } from './schemas.js';
|
|
10
|
+
/** The header that carries the workspace API key. */
|
|
11
|
+
export declare const API_KEY_HEADER = "x-api-key";
|
|
12
|
+
/** Milliseconds to wait for each request by default. */
|
|
13
|
+
export declare const DEFAULT_TIMEOUT_MS = 30000;
|
|
14
|
+
/** The value of a query parameter. */
|
|
15
|
+
export type QueryValue = string | number;
|
|
16
|
+
/** One call to the API. */
|
|
17
|
+
export interface ApiRequest {
|
|
18
|
+
readonly body?: JsonValue;
|
|
19
|
+
readonly method: 'DELETE' | 'GET' | 'PATCH' | 'POST';
|
|
20
|
+
/** The path, starting with `/api/v1`. */
|
|
21
|
+
readonly path: string;
|
|
22
|
+
readonly query?: Readonly<Record<string, QueryValue | undefined>>;
|
|
23
|
+
}
|
|
24
|
+
/** A file sent to a presigned storage form, outside the API. */
|
|
25
|
+
export interface FileUpload {
|
|
26
|
+
readonly contentType: string;
|
|
27
|
+
readonly fields: Readonly<Record<string, string>>;
|
|
28
|
+
readonly file: Blob | Uint8Array<ArrayBuffer>;
|
|
29
|
+
readonly url: string;
|
|
30
|
+
}
|
|
31
|
+
/** A `fetch` implementation: the global `fetch` or any compatible function. */
|
|
32
|
+
export type Fetch = (input: URL, init: RequestInit) => Promise<Response>;
|
|
33
|
+
/** How to reach the API. */
|
|
34
|
+
export interface TransportOptions {
|
|
35
|
+
readonly apiKey: string;
|
|
36
|
+
readonly baseUrl: string;
|
|
37
|
+
readonly fetch: Fetch;
|
|
38
|
+
readonly timeoutMs: number;
|
|
39
|
+
}
|
|
40
|
+
/** Formats an id for a URL path, escaping anything that is not a plain path segment. */
|
|
41
|
+
export declare function pathId(id: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Validates the base URL of a Templr instance and strips its trailing slashes.
|
|
44
|
+
*
|
|
45
|
+
* @throws TypeError when the URL is not an absolute `http` or `https` URL.
|
|
46
|
+
*/
|
|
47
|
+
export declare function normalizeBaseUrl(baseUrl: string): string;
|
|
48
|
+
/**
|
|
49
|
+
* Rejects an empty API key.
|
|
50
|
+
*
|
|
51
|
+
* @throws TypeError when the API key is empty.
|
|
52
|
+
*/
|
|
53
|
+
export declare function validateApiKey(apiKey: string): string;
|
|
54
|
+
/** Builds the {@link APIError} subclass that matches an error response. */
|
|
55
|
+
export declare function errorFromResponse(response: Response, text: string): APIError;
|
|
56
|
+
/** Sends requests with `fetch` and validates their responses. */
|
|
57
|
+
export declare class Transport {
|
|
58
|
+
#private;
|
|
59
|
+
constructor(options: TransportOptions);
|
|
60
|
+
/**
|
|
61
|
+
* Sends a request and returns its body, validated by `schema`.
|
|
62
|
+
*
|
|
63
|
+
* @throws APIError when the API answers with an error status.
|
|
64
|
+
* @throws APIConnectionError when the server cannot be reached, or {@link APITimeoutError} on timeout.
|
|
65
|
+
* @throws UnexpectedResponseError when the body does not match `schema`.
|
|
66
|
+
*/
|
|
67
|
+
send<Schema extends z.ZodType>(request: ApiRequest, schema: Schema): Promise<z.output<Schema>>;
|
|
68
|
+
/**
|
|
69
|
+
* Sends a file to a presigned storage form: the fields, then the file as the
|
|
70
|
+
* last field. The form is for the bucket, not the API, so it never carries the API key.
|
|
71
|
+
*
|
|
72
|
+
* @throws UploadError when the bucket rejects the file.
|
|
73
|
+
* @throws APIConnectionError when the bucket cannot be reached, or {@link APITimeoutError} on timeout.
|
|
74
|
+
*/
|
|
75
|
+
upload(upload: FileUpload): Promise<void>;
|
|
76
|
+
}
|