@classytic/notifications 1.2.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,280 @@
1
+ //#region src/providers/email/presets.ts
2
+ const EMAIL_PRESETS = {
3
+ resend: {
4
+ displayName: "Resend",
5
+ description: "Transactional + broadcast email via Resend SMTP relay.",
6
+ host: "smtp.resend.com",
7
+ port: 465,
8
+ secure: true,
9
+ userSource: "literal:resend",
10
+ passSource: "apiKey",
11
+ setupHint: "Verify your sending domain in resend.com → Domains. Create an API key (full access) and paste it below."
12
+ },
13
+ gmail: {
14
+ displayName: "Gmail (App Password)",
15
+ description: "Send via a personal Gmail account using a 16-character app password.",
16
+ host: "smtp.gmail.com",
17
+ port: 465,
18
+ secure: true,
19
+ userSource: "user",
20
+ passSource: "password",
21
+ setupHint: "Enable 2-Step Verification, then generate an App Password at myaccount.google.com/apppasswords. The from-address must match the Gmail account."
22
+ },
23
+ sendgrid: {
24
+ displayName: "SendGrid",
25
+ description: "SendGrid SMTP relay (username is literally \"apikey\").",
26
+ host: "smtp.sendgrid.net",
27
+ port: 587,
28
+ secure: false,
29
+ userSource: "literal:apikey",
30
+ passSource: "apiKey",
31
+ setupHint: "Verify your sending domain in app.sendgrid.com → Sender Authentication. Create an API key with Mail Send permission."
32
+ },
33
+ mailgun: {
34
+ displayName: "Mailgun",
35
+ description: "Mailgun SMTP relay using per-domain SMTP credentials.",
36
+ host: "smtp.mailgun.org",
37
+ port: 465,
38
+ secure: true,
39
+ userSource: "user",
40
+ passSource: "password",
41
+ setupHint: "Verify your sending domain in app.mailgun.com. The SMTP credentials shown on the domain page (user + password) go below — these are NOT your account password."
42
+ },
43
+ ses: {
44
+ displayName: "AWS SES (SMTP)",
45
+ description: "Amazon SES via SMTP. Host defaults to us-east-1; override for other regions.",
46
+ host: "email-smtp.us-east-1.amazonaws.com",
47
+ port: 465,
48
+ secure: true,
49
+ userSource: "user",
50
+ passSource: "password",
51
+ allowHostOverride: true,
52
+ setupHint: "In the SES console (your region), Account dashboard → SMTP settings → Create SMTP credentials. The IAM user gets an SMTP username and password — those go below (NOT your AWS access key). To use a non-default region, set host to email-smtp.<region>.amazonaws.com."
53
+ },
54
+ smtp: {
55
+ displayName: "Custom SMTP",
56
+ description: "Any SMTP relay — supply host, port, user, and password manually.",
57
+ host: null,
58
+ port: 587,
59
+ secure: false,
60
+ userSource: "user",
61
+ passSource: "password",
62
+ setupHint: "Use for SMTP servers without a preset. Standard auth-SMTP only — no OAuth-XOAUTH2."
63
+ }
64
+ };
65
+ /** Convenience dropdown options for the credential UI. */
66
+ const EMAIL_PROVIDER_OPTIONS = Object.keys(EMAIL_PRESETS).map((name) => ({
67
+ value: name,
68
+ label: EMAIL_PRESETS[name].displayName
69
+ }));
70
+
71
+ //#endregion
72
+ //#region src/providers/email/transport.ts
73
+ function resolveUser(data, userSource) {
74
+ if (userSource.startsWith("literal:")) return userSource.slice(8);
75
+ if (userSource === "user") {
76
+ if (!data.user) throw new Error("SMTP user is required for this provider");
77
+ return data.user;
78
+ }
79
+ throw new Error(`Unknown userSource: ${userSource}`);
80
+ }
81
+ function resolvePass(data, passSource) {
82
+ const value = passSource === "apiKey" ? data.apiKey : data.password;
83
+ if (!value) throw new Error(passSource === "apiKey" ? "API key is required for this provider" : "Password is required for this provider");
84
+ return value;
85
+ }
86
+ /**
87
+ * Build a `nodemailer` `Transporter` from the credential blob.
88
+ *
89
+ * @throws `Error` when required fields are missing for the chosen provider.
90
+ * @throws `Error` when `nodemailer` is not installed (optional peer).
91
+ */
92
+ async function buildEmailTransport(data) {
93
+ const preset = EMAIL_PRESETS[data.provider];
94
+ if (!preset) throw new Error(`Unknown email provider: ${data.provider}`);
95
+ const allowOverride = preset.host === null || preset.allowHostOverride === true;
96
+ const overrideHost = data.host?.trim();
97
+ const host = allowOverride && overrideHost ? overrideHost : preset.host;
98
+ if (!host) throw new Error("SMTP host is required");
99
+ const port = allowOverride && data.port !== void 0 && data.port !== "" ? typeof data.port === "number" ? data.port : parseInt(String(data.port), 10) : preset.port;
100
+ let secure = preset.secure;
101
+ if (allowOverride && data.secure !== void 0 && data.secure !== "") {
102
+ if (typeof data.secure === "boolean") secure = data.secure;
103
+ else if (typeof data.secure === "string") secure = data.secure === "true";
104
+ }
105
+ let nodemailer;
106
+ try {
107
+ nodemailer = await import("nodemailer");
108
+ } catch {
109
+ throw new Error("nodemailer is required to build an email transport — install it: `npm install nodemailer`");
110
+ }
111
+ return (nodemailer.default ?? nodemailer).createTransport({
112
+ host,
113
+ port,
114
+ secure,
115
+ auth: {
116
+ user: resolveUser(data, preset.userSource),
117
+ pass: resolvePass(data, preset.passSource)
118
+ }
119
+ });
120
+ }
121
+ /**
122
+ * Compose the RFC-5322 `From` header value from the credential blob.
123
+ * Returns `"Display Name" <email@example.com>` when `fromName` is set,
124
+ * otherwise just `email@example.com`.
125
+ */
126
+ function buildFromHeader(data) {
127
+ const name = data.fromName?.trim();
128
+ return name ? `"${name}" <${data.fromEmail}>` : data.fromEmail;
129
+ }
130
+
131
+ //#endregion
132
+ //#region src/providers/email/schema.ts
133
+ /**
134
+ * Credential field schema for the email provider.
135
+ *
136
+ * Hosts that ship a dynamic credential form (driven by a provider's
137
+ * `getCredentialSchema()`-style method) read this to render the right
138
+ * fields. Returning the UNION of all provider-specific fields keeps the
139
+ * form generic — the UI hides irrelevant fields based on the chosen
140
+ * `provider`. Only relevant fields get saved into the encrypted blob.
141
+ *
142
+ * Shape is host-agnostic (no `@classytic/social` import). Hosts that
143
+ * use the social registry's `CredentialField` type can structurally cast.
144
+ */
145
+ /** Union-of-fields schema — every field appears; the UI hides what's
146
+ * not relevant for the chosen `provider`. */
147
+ function getEmailCredentialSchema() {
148
+ return [
149
+ {
150
+ name: "provider",
151
+ displayName: "Provider",
152
+ type: "select",
153
+ required: true,
154
+ options: EMAIL_PROVIDER_OPTIONS,
155
+ description: "Which SMTP relay handles your sends."
156
+ },
157
+ {
158
+ name: "fromEmail",
159
+ displayName: "From Email",
160
+ type: "text",
161
+ required: true,
162
+ placeholder: "hello@yourdomain.com",
163
+ description: "Must be verified with your provider — sends from unverified addresses are rejected."
164
+ },
165
+ {
166
+ name: "fromName",
167
+ displayName: "From Name",
168
+ type: "text",
169
+ required: false,
170
+ placeholder: "Your Brand",
171
+ description: "Optional. Shown as the sender name in inboxes."
172
+ },
173
+ {
174
+ name: "apiKey",
175
+ displayName: "API Key",
176
+ type: "password",
177
+ required: false,
178
+ description: "Resend or SendGrid only."
179
+ },
180
+ {
181
+ name: "user",
182
+ displayName: "SMTP Username",
183
+ type: "text",
184
+ required: false,
185
+ description: "Gmail address, Mailgun SMTP user, or custom SMTP user."
186
+ },
187
+ {
188
+ name: "password",
189
+ displayName: "SMTP Password",
190
+ type: "password",
191
+ required: false,
192
+ description: "Gmail app password, Mailgun SMTP password, or custom SMTP password."
193
+ },
194
+ {
195
+ name: "host",
196
+ displayName: "SMTP Host",
197
+ type: "text",
198
+ required: false,
199
+ placeholder: "smtp.example.com",
200
+ description: "Custom SMTP only — preset hosts are filled automatically."
201
+ },
202
+ {
203
+ name: "port",
204
+ displayName: "SMTP Port",
205
+ type: "text",
206
+ required: false,
207
+ placeholder: "587",
208
+ description: "Custom SMTP only."
209
+ },
210
+ {
211
+ name: "secure",
212
+ displayName: "Secure (implicit TLS)",
213
+ type: "select",
214
+ required: false,
215
+ options: [{
216
+ value: "true",
217
+ label: "true (port 465 / implicit TLS)"
218
+ }, {
219
+ value: "false",
220
+ label: "false (port 587 / STARTTLS)"
221
+ }],
222
+ description: "Custom SMTP only."
223
+ }
224
+ ];
225
+ }
226
+
227
+ //#endregion
228
+ //#region src/providers/email/test.ts
229
+ /**
230
+ * Validate an email credential by building its transport and asking the
231
+ * underlying SMTP server to verify the auth tuple. Used by hosts during
232
+ * "Test Connection" flows.
233
+ *
234
+ * Returns a typed result rather than throwing — UI consumers usually
235
+ * want to surface the message + status pair regardless of outcome.
236
+ */
237
+ async function testEmailCredential(data) {
238
+ if (!data.provider) return {
239
+ status: "Error",
240
+ message: "Provider is required"
241
+ };
242
+ if (!data.fromEmail) return {
243
+ status: "Error",
244
+ message: "From email is required"
245
+ };
246
+ if (!Object.keys(EMAIL_PRESETS).includes(data.provider)) return {
247
+ status: "Error",
248
+ message: `Unknown provider: ${data.provider}`
249
+ };
250
+ let transport;
251
+ try {
252
+ transport = await buildEmailTransport(data);
253
+ } catch (err) {
254
+ return {
255
+ status: "Error",
256
+ message: err?.message ?? "Failed to build SMTP transport"
257
+ };
258
+ }
259
+ try {
260
+ if (typeof transport.verify === "function") await transport.verify();
261
+ return {
262
+ status: "OK",
263
+ message: `SMTP connection verified for ${data.fromEmail}`,
264
+ data: {
265
+ provider: data.provider,
266
+ fromEmail: data.fromEmail
267
+ }
268
+ };
269
+ } catch (err) {
270
+ return {
271
+ status: "Error",
272
+ message: err?.message ?? "SMTP verification failed"
273
+ };
274
+ } finally {
275
+ if (typeof transport.close === "function") transport.close();
276
+ }
277
+ }
278
+
279
+ //#endregion
280
+ export { EMAIL_PRESETS, EMAIL_PROVIDER_OPTIONS, buildEmailTransport, buildFromHeader, getEmailCredentialSchema, testEmailCredential };