lazypock 0.19.2 → 0.20.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/README.md +17 -0
- package/dist/index.cjs +155 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +186 -2
- package/dist/index.d.ts +186 -2
- package/dist/index.global.js +154 -0
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +154 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/http.ts +16 -0
- package/src/index.ts +12 -0
- package/src/lazypock.ts +27 -0
- package/src/mail.ts +290 -0
- package/src/types.ts +1 -1
package/package.json
CHANGED
package/src/http.ts
CHANGED
|
@@ -469,6 +469,22 @@ export class HttpClient {
|
|
|
469
469
|
return this.request<T>("POST", path, body, options);
|
|
470
470
|
}
|
|
471
471
|
|
|
472
|
+
/**
|
|
473
|
+
* HTTP PUT — a full replacement. Used where the server models a resource as a
|
|
474
|
+
* complete unit rather than a set of patches (e.g. a mail template, which only
|
|
475
|
+
* exists once all three of its parts do).
|
|
476
|
+
* @param path URL path.
|
|
477
|
+
* @param body Request body.
|
|
478
|
+
* @param options Optional request options.
|
|
479
|
+
*/
|
|
480
|
+
put<T = unknown>(
|
|
481
|
+
path: string,
|
|
482
|
+
body?: unknown,
|
|
483
|
+
options?: RequestOptions,
|
|
484
|
+
): Promise<T | null> {
|
|
485
|
+
return this.request<T>("PUT", path, body, options);
|
|
486
|
+
}
|
|
487
|
+
|
|
472
488
|
/**
|
|
473
489
|
* HTTP PATCH.
|
|
474
490
|
* @param path URL path.
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ export {
|
|
|
15
15
|
RealtimeService,
|
|
16
16
|
sseUrlFromBaseUrl,
|
|
17
17
|
FilesService,
|
|
18
|
+
MailService,
|
|
18
19
|
getFileUrl,
|
|
19
20
|
getThumbUrl,
|
|
20
21
|
getScaleUrl,
|
|
@@ -44,6 +45,17 @@ export type {
|
|
|
44
45
|
SystemFields,
|
|
45
46
|
RequestOptions,
|
|
46
47
|
FileRecord,
|
|
48
|
+
// mail
|
|
49
|
+
MailTemplate,
|
|
50
|
+
MailTemplateParts,
|
|
51
|
+
MailMessage,
|
|
52
|
+
MailSendResult,
|
|
53
|
+
MailPreviewDraft,
|
|
54
|
+
MailPreviewResult,
|
|
55
|
+
MailTemplateDeleteResult,
|
|
56
|
+
MailTemplateLayer,
|
|
57
|
+
MailPart,
|
|
58
|
+
MailAssigns,
|
|
47
59
|
// schema
|
|
48
60
|
CollectionSchema,
|
|
49
61
|
SchemaField,
|
package/src/lazypock.ts
CHANGED
|
@@ -43,6 +43,19 @@ import {
|
|
|
43
43
|
type DirectUpload,
|
|
44
44
|
} from "./files";
|
|
45
45
|
import { CollectionsService } from "./collections";
|
|
46
|
+
import {
|
|
47
|
+
MailService,
|
|
48
|
+
type MailTemplate,
|
|
49
|
+
type MailTemplateParts,
|
|
50
|
+
type MailMessage,
|
|
51
|
+
type MailSendResult,
|
|
52
|
+
type MailPreviewDraft,
|
|
53
|
+
type MailPreviewResult,
|
|
54
|
+
type MailTemplateDeleteResult,
|
|
55
|
+
type MailTemplateLayer,
|
|
56
|
+
type MailPart,
|
|
57
|
+
type MailAssigns,
|
|
58
|
+
} from "./mail";
|
|
46
59
|
import type { CollectionSchema, SchemaField } from "./schema";
|
|
47
60
|
import { generateTypes, collectionTypeName } from "./codegen";
|
|
48
61
|
import { fieldTypeScriptType, fieldTypeKind, schemaFieldType } from "./typegen";
|
|
@@ -82,6 +95,7 @@ export {
|
|
|
82
95
|
getVariantUrl,
|
|
83
96
|
CollectionService,
|
|
84
97
|
CollectionsService,
|
|
98
|
+
MailService,
|
|
85
99
|
generateTypes,
|
|
86
100
|
collectionTypeName,
|
|
87
101
|
fieldTypeScriptType,
|
|
@@ -102,6 +116,17 @@ export type {
|
|
|
102
116
|
// realtime
|
|
103
117
|
RealtimeConnectOpts,
|
|
104
118
|
RealtimeTokenProvider,
|
|
119
|
+
// mail
|
|
120
|
+
MailTemplate,
|
|
121
|
+
MailTemplateParts,
|
|
122
|
+
MailMessage,
|
|
123
|
+
MailSendResult,
|
|
124
|
+
MailPreviewDraft,
|
|
125
|
+
MailPreviewResult,
|
|
126
|
+
MailTemplateDeleteResult,
|
|
127
|
+
MailTemplateLayer,
|
|
128
|
+
MailPart,
|
|
129
|
+
MailAssigns,
|
|
105
130
|
CollectionSchema,
|
|
106
131
|
SchemaField,
|
|
107
132
|
// schema-driven query typing
|
|
@@ -167,6 +192,7 @@ export class LazypockClient {
|
|
|
167
192
|
readonly realtime: RealtimeService;
|
|
168
193
|
readonly collections: CollectionsService;
|
|
169
194
|
readonly files: FilesService;
|
|
195
|
+
readonly mail: MailService;
|
|
170
196
|
private collectionCache = new Map<string, CollectionService>();
|
|
171
197
|
private schemaByName?: Map<string, CollectionSchema>;
|
|
172
198
|
private schemaById?: Map<string, CollectionSchema>;
|
|
@@ -205,6 +231,7 @@ export class LazypockClient {
|
|
|
205
231
|
this.authStore.onChange(() => this.realtime.refresh());
|
|
206
232
|
this.authReady.then(() => this.realtime.refresh());
|
|
207
233
|
this.files = new FilesService(this.http);
|
|
234
|
+
this.mail = new MailService(this.http);
|
|
208
235
|
this.collections = new CollectionsService(this.http, this.realtime);
|
|
209
236
|
if (options.types?.schemas) {
|
|
210
237
|
this.schemaByName = new Map(
|
package/src/mail.ts
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// ── Mail Service ────────────────────────────────────────
|
|
2
|
+
// Send application email through the deployment's SMTP settings, and manage
|
|
3
|
+
// the templates it renders (`POST /api/mail`, `/api/mail/templates`).
|
|
4
|
+
//
|
|
5
|
+
// Access via {@link LazypockClient.mail}.
|
|
6
|
+
//
|
|
7
|
+
// Sending **queues** a job rather than delivering inline: an SMTP provider that
|
|
8
|
+
// is slow or down must not hold a request open, and the queue already retries
|
|
9
|
+
// with backoff. The response therefore means "queued", not "delivered" —
|
|
10
|
+
// delivery failures surface on the job (`GET /api/logs`).
|
|
11
|
+
//
|
|
12
|
+
// A template is rendered against the *variables* of a message, and one of the
|
|
13
|
+
// built-ins renders a password-reset token. That is why templates are
|
|
14
|
+
// superuser-only and deliberately not API-keyable: whoever can write a template
|
|
15
|
+
// can mail themselves somebody else's token. Sending an already-defined
|
|
16
|
+
// template stays API-keyable.
|
|
17
|
+
|
|
18
|
+
import type { HttpClient } from "./http";
|
|
19
|
+
import type { RequestOptions } from "./types";
|
|
20
|
+
|
|
21
|
+
/** The three parts a mail template is made of. */
|
|
22
|
+
export type MailPart = "subject" | "html" | "text";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Which layer a template body came from, newest first.
|
|
26
|
+
*
|
|
27
|
+
* - `database` — saved through the API (or Studio), the one in effect
|
|
28
|
+
* - `file` — a file in the deployment's template directory
|
|
29
|
+
* - `builtin` — shipped with the server
|
|
30
|
+
*
|
|
31
|
+
* Reported because an edit that is in effect and one that is not look identical
|
|
32
|
+
* otherwise.
|
|
33
|
+
*/
|
|
34
|
+
export type MailTemplateLayer = "database" | "file" | "builtin";
|
|
35
|
+
|
|
36
|
+
/** The template context handed to a send or a preview. */
|
|
37
|
+
export type MailAssigns = Record<string, unknown>;
|
|
38
|
+
|
|
39
|
+
/** A template as the server resolves it. */
|
|
40
|
+
export interface MailTemplate {
|
|
41
|
+
/** The template name, e.g. `verification` or `receipt`. */
|
|
42
|
+
name: string;
|
|
43
|
+
/** The strongest layer across the three parts (`database` wins over `file`). */
|
|
44
|
+
source: MailTemplateLayer;
|
|
45
|
+
/** The layer each part resolved from. */
|
|
46
|
+
parts: Record<MailPart, MailTemplateLayer>;
|
|
47
|
+
/** The subject, as a template. */
|
|
48
|
+
subject: string;
|
|
49
|
+
/** The HTML body. */
|
|
50
|
+
html: string;
|
|
51
|
+
/** The plain-text alternative. */
|
|
52
|
+
text: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The bodies of one template. All three parts are required. */
|
|
56
|
+
export interface MailTemplateParts {
|
|
57
|
+
subject: string;
|
|
58
|
+
html: string;
|
|
59
|
+
text: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** One message to queue. */
|
|
63
|
+
export type MailMessage =
|
|
64
|
+
| {
|
|
65
|
+
/** The recipient's address. */
|
|
66
|
+
to: string;
|
|
67
|
+
/** The recipient's display name, rendered as `name`. */
|
|
68
|
+
toName?: string;
|
|
69
|
+
/** A built-in or deployment-saved template name. */
|
|
70
|
+
template: string;
|
|
71
|
+
/** The template context. */
|
|
72
|
+
assigns?: MailAssigns;
|
|
73
|
+
}
|
|
74
|
+
| {
|
|
75
|
+
/** The recipient's address. */
|
|
76
|
+
to: string;
|
|
77
|
+
/** The recipient's display name. */
|
|
78
|
+
toName?: string;
|
|
79
|
+
/** A raw subject, instead of a template. */
|
|
80
|
+
subject: string;
|
|
81
|
+
/** The HTML body (required when `text` is omitted). */
|
|
82
|
+
html?: string;
|
|
83
|
+
/** The plain-text body (required when `html` is omitted). Derived from the HTML when omitted. */
|
|
84
|
+
text?: string;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** What `send()` answers: the queued job, not a delivery. */
|
|
88
|
+
export interface MailSendResult {
|
|
89
|
+
queued: boolean;
|
|
90
|
+
/** The job id — follow it in `GET /api/logs` for delivery failures. */
|
|
91
|
+
id: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A draft to render in place of the stored parts. Nothing is saved or sent. */
|
|
95
|
+
export interface MailPreviewDraft {
|
|
96
|
+
/** The template context. `name` and `app_name` are filled in when absent. */
|
|
97
|
+
assigns?: MailAssigns;
|
|
98
|
+
/** The recipient display name that `name` renders as. */
|
|
99
|
+
toName?: string;
|
|
100
|
+
/** A draft subject to render instead of the stored one. */
|
|
101
|
+
subject?: string;
|
|
102
|
+
/** A draft HTML body to render instead of the stored one. */
|
|
103
|
+
html?: string;
|
|
104
|
+
/** A draft text body to render instead of the stored one. */
|
|
105
|
+
text?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The rendered result of a preview. */
|
|
109
|
+
export interface MailPreviewResult {
|
|
110
|
+
name: string;
|
|
111
|
+
subject: string;
|
|
112
|
+
html: string;
|
|
113
|
+
text: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** What `deleteTemplate()` answers: how many stored parts went away. */
|
|
117
|
+
export interface MailTemplateDeleteResult {
|
|
118
|
+
name: string;
|
|
119
|
+
/** `0` when the template had never been saved (it was already at its default). */
|
|
120
|
+
deleted: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Path for one template, encoding a name so it survives the URL path. */
|
|
124
|
+
function templatePath(name: string): string {
|
|
125
|
+
return "/mail/templates/" + encodeURIComponent(name.trim());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Service for application email.
|
|
130
|
+
* Access via {@link LazypockClient.mail}.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* await client.mail.send({ to: 'ada@example.com', template: 'verification', assigns: { token: '…' } });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export class MailService {
|
|
138
|
+
constructor(private http: HttpClient) {}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Queue one message for delivery.
|
|
142
|
+
*
|
|
143
|
+
* Either a `template` or a `subject` with a body. The type requires one of
|
|
144
|
+
* them — a message with neither does not compile — while the server refuses a
|
|
145
|
+
* message that carries both, and a template with a raw body.
|
|
146
|
+
*
|
|
147
|
+
* The address is shape-checked server-side; the SMTP server remains the
|
|
148
|
+
* authority on whether it exists.
|
|
149
|
+
*
|
|
150
|
+
* @param message The template or raw message to send.
|
|
151
|
+
* @param options Optional request options (signal, custom fetch).
|
|
152
|
+
* @returns The queued job (`{ queued: true, id }`).
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* // A template
|
|
157
|
+
* await client.mail.send({ to: 'ada@example.com', template: 'receipt', assigns: { total: 42 } });
|
|
158
|
+
*
|
|
159
|
+
* // A raw message
|
|
160
|
+
* await client.mail.send({ to: 'ada@example.com', subject: 'Hello', text: 'Hi there' });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
async send(message: MailMessage, options?: RequestOptions): Promise<MailSendResult> {
|
|
164
|
+
const body =
|
|
165
|
+
"template" in message
|
|
166
|
+
? {
|
|
167
|
+
to: message.to,
|
|
168
|
+
toName: message.toName,
|
|
169
|
+
template: message.template,
|
|
170
|
+
assigns: message.assigns,
|
|
171
|
+
}
|
|
172
|
+
: {
|
|
173
|
+
to: message.to,
|
|
174
|
+
toName: message.toName,
|
|
175
|
+
subject: message.subject,
|
|
176
|
+
html: message.html,
|
|
177
|
+
text: message.text,
|
|
178
|
+
};
|
|
179
|
+
const data = await this.http.request<MailSendResult>("POST", "/mail", body, options);
|
|
180
|
+
return (data ?? { queued: false, id: "" }) as MailSendResult;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* List every template the server can send, with the body each part
|
|
185
|
+
* currently resolves to and the layer it came from (superuser).
|
|
186
|
+
*
|
|
187
|
+
* Unpaginated: there are as many templates as the deployment defined, not
|
|
188
|
+
* as many as it has sent.
|
|
189
|
+
*/
|
|
190
|
+
async listTemplates(options?: RequestOptions): Promise<MailTemplate[]> {
|
|
191
|
+
const data = await this.http.request<{ items: MailTemplate[] }>(
|
|
192
|
+
"GET",
|
|
193
|
+
"/mail/templates",
|
|
194
|
+
undefined,
|
|
195
|
+
options,
|
|
196
|
+
);
|
|
197
|
+
return data?.items ?? [];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Fetch one template by name (superuser). */
|
|
201
|
+
async getTemplate(name: string, options?: RequestOptions): Promise<MailTemplate> {
|
|
202
|
+
const data = await this.http.request<MailTemplate>(
|
|
203
|
+
"GET",
|
|
204
|
+
templatePath(name),
|
|
205
|
+
undefined,
|
|
206
|
+
options,
|
|
207
|
+
);
|
|
208
|
+
return data as MailTemplate;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Save all three parts of a template, replacing whatever was stored for them.
|
|
213
|
+
*
|
|
214
|
+
* All three at once rather than one at a time: a template is only allowed to
|
|
215
|
+
* exist once it is complete, so a part-at-a-time write could leave a subject
|
|
216
|
+
* saved against a body that does not parse. The server parses the bodies
|
|
217
|
+
* before storing them, so a syntax error is a rejected save rather than a
|
|
218
|
+
* queued email that fails where nobody is watching (superuser).
|
|
219
|
+
*/
|
|
220
|
+
async saveTemplate(
|
|
221
|
+
name: string,
|
|
222
|
+
parts: MailTemplateParts,
|
|
223
|
+
options?: RequestOptions,
|
|
224
|
+
): Promise<MailTemplate> {
|
|
225
|
+
const data = await this.http.put<MailTemplate>(
|
|
226
|
+
templatePath(name),
|
|
227
|
+
{ subject: parts.subject, html: parts.html, text: parts.text },
|
|
228
|
+
options,
|
|
229
|
+
);
|
|
230
|
+
return data as MailTemplate;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Remove the stored bodies, so the template falls back to its file or its
|
|
235
|
+
* built-in (superuser).
|
|
236
|
+
*
|
|
237
|
+
* Deleting one that was never saved succeeds with `deleted: 0` — it is
|
|
238
|
+
* already at its default — but an unknown *name* is a 404, so a typo cannot
|
|
239
|
+
* look like a reset that worked.
|
|
240
|
+
*/
|
|
241
|
+
async deleteTemplate(
|
|
242
|
+
name: string,
|
|
243
|
+
options?: RequestOptions,
|
|
244
|
+
): Promise<MailTemplateDeleteResult> {
|
|
245
|
+
const data = await this.http.request<MailTemplateDeleteResult>(
|
|
246
|
+
"DELETE",
|
|
247
|
+
templatePath(name),
|
|
248
|
+
undefined,
|
|
249
|
+
options,
|
|
250
|
+
);
|
|
251
|
+
return (data ?? { name: name.trim(), deleted: 0 }) as MailTemplateDeleteResult;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Render a template the way a send would, without storing or sending
|
|
256
|
+
* anything (superuser).
|
|
257
|
+
*
|
|
258
|
+
* Any of `subject`/`html`/`text` given in `draft` is rendered in place of the
|
|
259
|
+
* stored part, which is what makes this useful while a body is still being
|
|
260
|
+
* written. A draft over a name the server does not know is previewed as the
|
|
261
|
+
* new template it is being written as, so such a draft has to arrive
|
|
262
|
+
* complete (all three parts).
|
|
263
|
+
*
|
|
264
|
+
* Previews share one auto-cancellation key (`POST /mail/templates/<name>/preview`),
|
|
265
|
+
* so a newer preview aborts an older one: the older promise rejects with an
|
|
266
|
+
* `ApiError` whose `isAbort` is `true`. That is what an editor wants when a
|
|
267
|
+
* keystroke supersedes the last render, but pass `{ requestKey: null }` to
|
|
268
|
+
* keep every preview if you would rather have them all.
|
|
269
|
+
*/
|
|
270
|
+
async preview(
|
|
271
|
+
name: string,
|
|
272
|
+
draft?: MailPreviewDraft,
|
|
273
|
+
options?: RequestOptions,
|
|
274
|
+
): Promise<MailPreviewResult> {
|
|
275
|
+
const body = {
|
|
276
|
+
assigns: draft?.assigns,
|
|
277
|
+
toName: draft?.toName,
|
|
278
|
+
subject: draft?.subject,
|
|
279
|
+
html: draft?.html,
|
|
280
|
+
text: draft?.text,
|
|
281
|
+
};
|
|
282
|
+
const data = await this.http.request<MailPreviewResult>(
|
|
283
|
+
"POST",
|
|
284
|
+
templatePath(name) + "/preview",
|
|
285
|
+
body,
|
|
286
|
+
options,
|
|
287
|
+
);
|
|
288
|
+
return data as MailPreviewResult;
|
|
289
|
+
}
|
|
290
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -56,7 +56,7 @@ export interface ListResult<T = ApiRecord> {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/** HTTP method supported by the client. */
|
|
59
|
-
export type Method = "GET" | "POST" | "PATCH" | "DELETE";
|
|
59
|
+
export type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
60
60
|
|
|
61
61
|
export interface RequestOptions {
|
|
62
62
|
/** Search/filter params */
|