@schemavaults/send-email 0.0.12 → 0.0.13
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 +185 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# @schemavaults/send-email
|
|
2
|
+
|
|
3
|
+
TypeScript client for sending emails via the `@schemavaults/mail-server` API. Provides request body validation (via Zod), environment-aware server URL resolution, and helpers for sending to individual recipients, mailing lists, and listing available email templates.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @schemavaults/send-email
|
|
9
|
+
# or
|
|
10
|
+
npm install @schemavaults/send-email
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Environment variables
|
|
14
|
+
|
|
15
|
+
| Variable | Required | Description |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| `SCHEMAVAULTS_MAIL_API_KEY` | Yes | Bearer token for the mail-server API. Starts with `svlts_mail_pk_`. |
|
|
18
|
+
| `SCHEMAVAULTS_MAILING_LIST_ID` | For mailing list sends | UUID of the target mailing list. |
|
|
19
|
+
| `SCHEMAVAULTS_APP_ENVIRONMENT` | No | `"production"` (default), `"development"`, or `"staging"`. Controls which mail-server instance is targeted. |
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Send an email
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { sendEmail } from "@schemavaults/send-email";
|
|
27
|
+
|
|
28
|
+
// Using a registered template
|
|
29
|
+
await sendEmail({
|
|
30
|
+
body: {
|
|
31
|
+
to: "user@example.com",
|
|
32
|
+
subject: "Welcome!",
|
|
33
|
+
message: {
|
|
34
|
+
template_id: "welcome-email",
|
|
35
|
+
template_props: { name: "Alice" },
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Using raw text/html
|
|
41
|
+
await sendEmail({
|
|
42
|
+
body: {
|
|
43
|
+
to: "user@example.com",
|
|
44
|
+
subject: "Hello",
|
|
45
|
+
message: {
|
|
46
|
+
text: "Hello from SchemaVaults.",
|
|
47
|
+
html: "<p>Hello from SchemaVaults.</p>",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Send to a mailing list
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { sendEmailToMailingList } from "@schemavaults/send-email";
|
|
57
|
+
|
|
58
|
+
await sendEmailToMailingList({
|
|
59
|
+
body: {
|
|
60
|
+
subject: "Weekly update",
|
|
61
|
+
message: {
|
|
62
|
+
text: "Here's what happened this week.",
|
|
63
|
+
html: "<p>Here's what happened this week.</p>",
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The mailing list UUID is read from `SCHEMAVAULTS_MAILING_LIST_ID` by default. You can override it per-call:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
await sendEmailToMailingList({
|
|
73
|
+
mailingListId: "00000000-0000-0000-0000-000000000000",
|
|
74
|
+
body: { subject: "...", message: { text: "...", html: "..." } },
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### List available email templates
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { listEmailTemplates } from "@schemavaults/send-email";
|
|
82
|
+
|
|
83
|
+
const templates = await listEmailTemplates();
|
|
84
|
+
// => [{ id: "welcome-email", description: "..." }, ...]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Validate a request body
|
|
88
|
+
|
|
89
|
+
The package exports the Zod schema used by both this client and the mail-server to validate request bodies:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { createSendEmailRequestBodySchema } from "@schemavaults/send-email";
|
|
93
|
+
|
|
94
|
+
// Pass `true` to allow mailing list UUIDs in the `to` field
|
|
95
|
+
const schema = createSendEmailRequestBodySchema(true);
|
|
96
|
+
const result = schema.safeParse(body);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## API
|
|
100
|
+
|
|
101
|
+
### `sendEmail(opts)`
|
|
102
|
+
|
|
103
|
+
Sends an email to one or more recipients.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
interface ISendEmailOpts {
|
|
107
|
+
body: SendEmailRequestBody;
|
|
108
|
+
bearerToken?: string; // overrides SCHEMAVAULTS_MAIL_API_KEY
|
|
109
|
+
mailServerUrl?: string; // overrides resolved server URL
|
|
110
|
+
environment?: "production" | "development" | "staging";
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `sendEmailToMailingList(opts)`
|
|
115
|
+
|
|
116
|
+
Sends an email to a mailing list audience. The `to`, `cc`, and `bcc` fields are not accepted -- the audience is the mailing list.
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
interface ISendEmailToMailingListOpts {
|
|
120
|
+
body: Omit<SendEmailRequestBody, "to" | "cc" | "bcc">;
|
|
121
|
+
mailingListId?: string; // overrides SCHEMAVAULTS_MAILING_LIST_ID
|
|
122
|
+
bearerToken?: string;
|
|
123
|
+
mailServerUrl?: string;
|
|
124
|
+
environment?: "production" | "development" | "staging";
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### `listEmailTemplates(opts?)`
|
|
129
|
+
|
|
130
|
+
Returns the list of registered email templates from the mail-server catalog.
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
interface IListEmailTemplatesOpts {
|
|
134
|
+
bearerToken?: string;
|
|
135
|
+
mailServerUrl?: string;
|
|
136
|
+
environment?: "production" | "development" | "staging";
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface EmailTemplate {
|
|
140
|
+
id: string;
|
|
141
|
+
description: string;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### `getSchemaVaultsMailApiKey()`
|
|
146
|
+
|
|
147
|
+
Reads and returns `SCHEMAVAULTS_MAIL_API_KEY` from the environment. Throws if not set.
|
|
148
|
+
|
|
149
|
+
### `createSendEmailRequestBodySchema(allowMailingListIds?)`
|
|
150
|
+
|
|
151
|
+
Returns a Zod schema for validating send-email request bodies. Pass `true` to allow UUIDs (mailing list IDs) in the `to` field.
|
|
152
|
+
|
|
153
|
+
### `emailTemplateIdSchema`
|
|
154
|
+
|
|
155
|
+
Zod schema for validating template IDs: lowercase alphanumeric with hyphens/underscores, 1-64 characters, must start with a letter.
|
|
156
|
+
|
|
157
|
+
## Request body shape
|
|
158
|
+
|
|
159
|
+
The `message` field accepts either a template reference or raw content:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
type Message =
|
|
163
|
+
| { template_id: string; template_props?: unknown }
|
|
164
|
+
| { text: string; html: string }; // both required
|
|
165
|
+
|
|
166
|
+
type SendEmailRequestBody = {
|
|
167
|
+
to: string | string[]; // email address(es) or mailing list UUID
|
|
168
|
+
subject: string;
|
|
169
|
+
message: Message;
|
|
170
|
+
from?: string; // defaults to mail-server's configured sender
|
|
171
|
+
replyTo?: string;
|
|
172
|
+
cc?: string | string[];
|
|
173
|
+
bcc?: string | string[];
|
|
174
|
+
};
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Development
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
bun install
|
|
181
|
+
bun run build
|
|
182
|
+
bun test
|
|
183
|
+
bun run typecheck
|
|
184
|
+
bun run lint
|
|
185
|
+
```
|
package/package.json
CHANGED