@postxl/generators 1.24.0 → 1.26.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/dist/backend-actions/actions.generator.d.ts +11 -0
- package/dist/backend-actions/actions.generator.js.map +1 -1
- package/dist/backend-actions/generators/actions-module.generator.js +11 -3
- package/dist/backend-actions/generators/actions-module.generator.js.map +1 -1
- package/dist/backend-actions/generators/dispatcher-service.generator.js +12 -4
- package/dist/backend-actions/generators/dispatcher-service.generator.js.map +1 -1
- package/dist/backend-core/backend.generator.js +72 -40
- package/dist/backend-core/backend.generator.js.map +1 -1
- package/dist/backend-mail/generators/conditional-files.d.ts +15 -0
- package/dist/backend-mail/generators/conditional-files.js +410 -0
- package/dist/backend-mail/generators/conditional-files.js.map +1 -0
- package/dist/backend-mail/generators/env-config.generator.d.ts +16 -0
- package/dist/backend-mail/generators/env-config.generator.js +167 -0
- package/dist/backend-mail/generators/env-config.generator.js.map +1 -0
- package/dist/backend-mail/generators/mail.service.generator.d.ts +11 -0
- package/dist/backend-mail/generators/mail.service.generator.js +243 -0
- package/dist/backend-mail/generators/mail.service.generator.js.map +1 -0
- package/dist/backend-mail/index.d.ts +5 -0
- package/dist/backend-mail/index.js +41 -0
- package/dist/backend-mail/index.js.map +1 -0
- package/dist/backend-mail/mail.generator.d.ts +78 -0
- package/dist/backend-mail/mail.generator.js +162 -0
- package/dist/backend-mail/mail.generator.js.map +1 -0
- package/dist/backend-mail/template/src/adapters/log.adapter.ts +14 -0
- package/dist/backend-mail/template/src/adapters/mail-adapter.interface.ts +27 -0
- package/dist/backend-mail/template/src/mail.config.ts +42 -0
- package/dist/backend-mail/template/src/mail.module.ts +37 -0
- package/dist/backend-mail/template/src/mail.service.test.ts +195 -0
- package/dist/backend-mail/template/src/mail.types.ts +67 -0
- package/dist/backend-mail/template/src/templates/simple.renderer.test.ts +155 -0
- package/dist/backend-mail/template/src/templates/simple.renderer.ts +171 -0
- package/dist/backend-mail/template/src/templates/template-renderer.interface.ts +5 -0
- package/dist/backend-mail/template/tsconfig.lib.json +10 -0
- package/dist/decoders/field-decoder.helper.js +7 -0
- package/dist/decoders/field-decoder.helper.js.map +1 -1
- package/dist/frontend-admin/generators/admin-overview-page.generator.js +24 -2
- package/dist/frontend-admin/generators/admin-overview-page.generator.js.map +1 -1
- package/dist/frontend-forms/generators/discriminated-union/fields.generator.js +6 -4
- package/dist/frontend-forms/generators/discriminated-union/fields.generator.js.map +1 -1
- package/dist/frontend-forms/generators/model/forms.generator.js +6 -4
- package/dist/frontend-forms/generators/model/forms.generator.js.map +1 -1
- package/dist/generators.js +2 -0
- package/dist/generators.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/types/generators/model-type.generator.js +1 -1
- package/dist/types/generators/model-type.generator.js.map +1 -1
- package/dist/types/types.generator.js +3 -4
- package/dist/types/types.generator.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Content for the optional adapter / renderer source files.
|
|
4
|
+
*
|
|
5
|
+
* These files are emitted to the generated project only when the matching
|
|
6
|
+
* adapter or renderer is enabled via `configureBackendMailGenerator`.
|
|
7
|
+
* Keeping them as plain string constants (rather than in the `template/`
|
|
8
|
+
* folder) means the generator never copies files the project will not
|
|
9
|
+
* compile and lets us gate both file emission and `package.json`
|
|
10
|
+
* dependencies on the same boolean.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.emailmdRendererTestSource = exports.emailmdRendererSource = exports.smtpAdapterTestSource = exports.smtpAdapterSource = exports.sesAdapterSource = void 0;
|
|
14
|
+
exports.sesAdapterSource = `import { SendEmailCommand, SESClient } from '@aws-sdk/client-ses'
|
|
15
|
+
import { Logger } from '@nestjs/common'
|
|
16
|
+
|
|
17
|
+
import type { IMailAdapter, MailPayload, MailSendResult, MailSendResultErrorStatus } from './mail-adapter.interface'
|
|
18
|
+
|
|
19
|
+
export type SesAdapterConfig = {
|
|
20
|
+
region: string
|
|
21
|
+
useCredentials: boolean
|
|
22
|
+
accessKey?: string
|
|
23
|
+
secretAccessKey?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const SES_INVALID_ADDRESS_ERRORS = new Set([
|
|
27
|
+
'InvalidParameterValue',
|
|
28
|
+
'InvalidParameterCombination',
|
|
29
|
+
'InvalidEmailAddress',
|
|
30
|
+
'MailFromDomainNotVerifiedException',
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
export class SesAdapter implements IMailAdapter {
|
|
34
|
+
private readonly logger = new Logger(SesAdapter.name)
|
|
35
|
+
private readonly client: SESClient
|
|
36
|
+
|
|
37
|
+
constructor(config: SesAdapterConfig) {
|
|
38
|
+
this.client = new SESClient({
|
|
39
|
+
region: config.region,
|
|
40
|
+
credentials: config.useCredentials
|
|
41
|
+
? {
|
|
42
|
+
accessKeyId: config.accessKey ?? '',
|
|
43
|
+
secretAccessKey: config.secretAccessKey ?? '',
|
|
44
|
+
}
|
|
45
|
+
: undefined,
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public async send(payload: MailPayload): Promise<MailSendResult> {
|
|
50
|
+
try {
|
|
51
|
+
const result = await this.client.send(
|
|
52
|
+
new SendEmailCommand({
|
|
53
|
+
Source: payload.from,
|
|
54
|
+
Destination: {
|
|
55
|
+
ToAddresses: [payload.to],
|
|
56
|
+
BccAddresses: payload.bcc ?? [],
|
|
57
|
+
},
|
|
58
|
+
Message: {
|
|
59
|
+
Subject: { Charset: 'UTF-8', Data: payload.subject },
|
|
60
|
+
Body: {
|
|
61
|
+
Html: { Charset: 'UTF-8', Data: payload.html },
|
|
62
|
+
Text: { Charset: 'UTF-8', Data: payload.text ?? '' },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
return { success: true, status: 'sent', details: result.MessageId }
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const status = classifySesError(error)
|
|
70
|
+
const message = describeError(error)
|
|
71
|
+
if (status === 'errorInvalidEmail') {
|
|
72
|
+
this.logger.warn(\`SES rejected invalid address: \${message}\`)
|
|
73
|
+
} else if (status === 'errorRejected') {
|
|
74
|
+
this.logger.warn(\`SES rejected mail: \${message}\`)
|
|
75
|
+
} else {
|
|
76
|
+
this.logger.error(\`SES send failed: \${message}\`, error instanceof Error ? error.stack : undefined)
|
|
77
|
+
}
|
|
78
|
+
return { success: false, status, details: message }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function classifySesError(error: unknown): MailSendResultErrorStatus {
|
|
84
|
+
if (!(error instanceof Error)) {
|
|
85
|
+
return 'errorOther'
|
|
86
|
+
}
|
|
87
|
+
if (SES_INVALID_ADDRESS_ERRORS.has(error.name)) {
|
|
88
|
+
return 'errorInvalidEmail'
|
|
89
|
+
}
|
|
90
|
+
if (error.name === 'MessageRejected') {
|
|
91
|
+
return 'errorRejected'
|
|
92
|
+
}
|
|
93
|
+
return 'errorOther'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function describeError(error: unknown): string {
|
|
97
|
+
return error instanceof Error ? \`\${error.name}: \${error.message}\` : String(error)
|
|
98
|
+
}
|
|
99
|
+
`;
|
|
100
|
+
exports.smtpAdapterSource = `/** Runtime verified on Node 24; Bun: untested. */
|
|
101
|
+
import { Logger } from '@nestjs/common'
|
|
102
|
+
import nodemailer, { type Transporter } from 'nodemailer'
|
|
103
|
+
|
|
104
|
+
import type { IMailAdapter, MailPayload, MailSendResult, MailSendResultErrorStatus } from './mail-adapter.interface'
|
|
105
|
+
|
|
106
|
+
export type SmtpAdapterConfig = {
|
|
107
|
+
host: string
|
|
108
|
+
port: number
|
|
109
|
+
secure: boolean
|
|
110
|
+
user: string
|
|
111
|
+
password: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
type NodemailerTransportOptions = {
|
|
115
|
+
host: string
|
|
116
|
+
port: number
|
|
117
|
+
secure: boolean
|
|
118
|
+
auth?: { user: string; pass: string }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Builds the nodemailer \`createTransport\` options from our adapter config.
|
|
123
|
+
*
|
|
124
|
+
* Exported so the auth-gating logic can be unit-tested directly - passing a
|
|
125
|
+
* partial credential pair (only user, only password) must result in auth
|
|
126
|
+
* being omitted entirely rather than sent with an empty field.
|
|
127
|
+
*/
|
|
128
|
+
export function buildSmtpTransportOptions(config: SmtpAdapterConfig): NodemailerTransportOptions {
|
|
129
|
+
const hasAuth = config.user.length > 0 && config.password.length > 0
|
|
130
|
+
return {
|
|
131
|
+
host: config.host,
|
|
132
|
+
port: config.port,
|
|
133
|
+
secure: config.secure,
|
|
134
|
+
auth: hasAuth ? { user: config.user, pass: config.password } : undefined,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export class SmtpAdapter implements IMailAdapter {
|
|
139
|
+
private readonly logger = new Logger(SmtpAdapter.name)
|
|
140
|
+
private readonly transporter: Transporter
|
|
141
|
+
|
|
142
|
+
constructor(config: SmtpAdapterConfig, transporter?: Transporter) {
|
|
143
|
+
this.transporter = transporter ?? nodemailer.createTransport(buildSmtpTransportOptions(config))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public async send(payload: MailPayload): Promise<MailSendResult> {
|
|
147
|
+
try {
|
|
148
|
+
const info = (await this.transporter.sendMail({
|
|
149
|
+
from: payload.from,
|
|
150
|
+
to: payload.to,
|
|
151
|
+
bcc: payload.bcc,
|
|
152
|
+
subject: payload.subject,
|
|
153
|
+
html: payload.html,
|
|
154
|
+
text: payload.text,
|
|
155
|
+
})) as { messageId?: string }
|
|
156
|
+
return { success: true, status: 'sent', details: info.messageId }
|
|
157
|
+
} catch (error) {
|
|
158
|
+
const classified = classifySmtpError(error)
|
|
159
|
+
const message = describeError(error)
|
|
160
|
+
if (classified === 'errorRejected') {
|
|
161
|
+
this.logger.warn(\`SMTP rejected mail: \${message}\`)
|
|
162
|
+
} else if (classified === 'errorInvalidEmail') {
|
|
163
|
+
this.logger.warn(\`SMTP invalid address: \${message}\`)
|
|
164
|
+
} else {
|
|
165
|
+
this.logger.error(\`SMTP send failed: \${message}\`, error instanceof Error ? error.stack : undefined)
|
|
166
|
+
}
|
|
167
|
+
return { success: false, status: classified, details: message }
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Maps nodemailer / SMTP errors onto our adapter-agnostic status enum.
|
|
174
|
+
*
|
|
175
|
+
* \`errorInvalidEmail\` is reserved for errors that unambiguously point at a
|
|
176
|
+
* malformed or syntactically-invalid address (EENVELOPE, EMESSAGE, and the
|
|
177
|
+
* classic 501/553 response codes).
|
|
178
|
+
*
|
|
179
|
+
* Every other 5xx response is bucketed into \`errorRejected\`. Note that this
|
|
180
|
+
* deliberately groups "recipient does not exist" (550 mailbox unavailable)
|
|
181
|
+
* together with "your account is blocked" (550 spam-related) - SMTP does
|
|
182
|
+
* not give us enough signal to tell them apart reliably, and splitting them
|
|
183
|
+
* would make the enum less stable. Callers needing that distinction should
|
|
184
|
+
* inspect the \`details\` string.
|
|
185
|
+
*/
|
|
186
|
+
function classifySmtpError(error: unknown): MailSendResultErrorStatus {
|
|
187
|
+
if (!(error instanceof Error)) {
|
|
188
|
+
return 'errorOther'
|
|
189
|
+
}
|
|
190
|
+
const code = (error as Error & { code?: string; responseCode?: number }).code
|
|
191
|
+
const responseCode = (error as Error & { responseCode?: number }).responseCode
|
|
192
|
+
if (code === 'EENVELOPE' || code === 'EMESSAGE' || responseCode === 501 || responseCode === 553) {
|
|
193
|
+
return 'errorInvalidEmail'
|
|
194
|
+
}
|
|
195
|
+
if (responseCode && responseCode >= 500 && responseCode < 600) {
|
|
196
|
+
return 'errorRejected'
|
|
197
|
+
}
|
|
198
|
+
return 'errorOther'
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function describeError(error: unknown): string {
|
|
202
|
+
if (error instanceof Error) {
|
|
203
|
+
const withCode = error as Error & { code?: string; responseCode?: number }
|
|
204
|
+
const codeTag = withCode.code ?? (withCode.responseCode ? String(withCode.responseCode) : error.name)
|
|
205
|
+
return \`\${codeTag}: \${error.message}\`
|
|
206
|
+
}
|
|
207
|
+
return String(error)
|
|
208
|
+
}
|
|
209
|
+
`;
|
|
210
|
+
exports.smtpAdapterTestSource = `import { describe, expect, it, jest } from '@jest/globals'
|
|
211
|
+
import type { Transporter } from 'nodemailer'
|
|
212
|
+
|
|
213
|
+
import { buildSmtpTransportOptions, SmtpAdapter } from './smtp.adapter'
|
|
214
|
+
|
|
215
|
+
function createSpyTransporter(): { transporter: Transporter; sendMail: jest.Mock } {
|
|
216
|
+
const sendMail = jest.fn(() => Promise.resolve({ messageId: 'msg-123' }))
|
|
217
|
+
const transporter = { sendMail } as unknown as Transporter
|
|
218
|
+
return { transporter, sendMail }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
describe('buildSmtpTransportOptions', () => {
|
|
222
|
+
const base = { host: 'smtp.example.com', port: 587, secure: false }
|
|
223
|
+
|
|
224
|
+
it('includes auth when both user and password are set', () => {
|
|
225
|
+
expect(buildSmtpTransportOptions({ ...base, user: 'alice', password: 'secret' })).toEqual({
|
|
226
|
+
...base,
|
|
227
|
+
auth: { user: 'alice', pass: 'secret' },
|
|
228
|
+
})
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('omits auth entirely when only user is set', () => {
|
|
232
|
+
expect(buildSmtpTransportOptions({ ...base, user: 'alice', password: '' }).auth).toBeUndefined()
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it('omits auth entirely when only password is set', () => {
|
|
236
|
+
expect(buildSmtpTransportOptions({ ...base, user: '', password: 'secret' }).auth).toBeUndefined()
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
it('omits auth entirely when neither is set', () => {
|
|
240
|
+
expect(buildSmtpTransportOptions({ ...base, user: '', password: '' }).auth).toBeUndefined()
|
|
241
|
+
})
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
describe('SmtpAdapter', () => {
|
|
245
|
+
it('marshals the payload through the transporter and returns success with the messageId', async () => {
|
|
246
|
+
const { transporter, sendMail } = createSpyTransporter()
|
|
247
|
+
const adapter = new SmtpAdapter({ host: 'unused', port: 587, secure: false, user: '', password: '' }, transporter)
|
|
248
|
+
|
|
249
|
+
const result = await adapter.send({
|
|
250
|
+
from: 'noreply@example.com',
|
|
251
|
+
to: 'user@example.com',
|
|
252
|
+
bcc: ['admin@example.com'],
|
|
253
|
+
subject: 'Hi there',
|
|
254
|
+
html: '<p>hello</p>',
|
|
255
|
+
text: 'hello',
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
expect(result.success).toBe(true)
|
|
259
|
+
if (!result.success) {
|
|
260
|
+
throw new Error('unreachable')
|
|
261
|
+
}
|
|
262
|
+
expect(result.status).toBe('sent')
|
|
263
|
+
expect(result.details).toBe('msg-123')
|
|
264
|
+
|
|
265
|
+
expect(sendMail).toHaveBeenCalledTimes(1)
|
|
266
|
+
expect(sendMail).toHaveBeenCalledWith({
|
|
267
|
+
from: 'noreply@example.com',
|
|
268
|
+
to: 'user@example.com',
|
|
269
|
+
bcc: ['admin@example.com'],
|
|
270
|
+
subject: 'Hi there',
|
|
271
|
+
html: '<p>hello</p>',
|
|
272
|
+
text: 'hello',
|
|
273
|
+
})
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('classifies invalid-address errors and returns a serialized details string', async () => {
|
|
277
|
+
const invalidErr = Object.assign(new Error('Bad recipient'), { code: 'EENVELOPE', responseCode: 501 })
|
|
278
|
+
const sendMail = jest.fn(() => Promise.reject(invalidErr))
|
|
279
|
+
const adapter = new SmtpAdapter({ host: 'unused', port: 587, secure: false, user: '', password: '' }, {
|
|
280
|
+
sendMail,
|
|
281
|
+
} as unknown as Transporter)
|
|
282
|
+
|
|
283
|
+
const result = await adapter.send({
|
|
284
|
+
from: 'noreply@example.com',
|
|
285
|
+
to: 'bad',
|
|
286
|
+
subject: 'x',
|
|
287
|
+
html: '<p>x</p>',
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
expect(result).toEqual({
|
|
291
|
+
success: false,
|
|
292
|
+
status: 'errorInvalidEmail',
|
|
293
|
+
details: 'EENVELOPE: Bad recipient',
|
|
294
|
+
})
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
it('classifies 5xx rejects as errorRejected', async () => {
|
|
298
|
+
const rejected = Object.assign(new Error('Mailbox unavailable'), { responseCode: 550 })
|
|
299
|
+
const sendMail = jest.fn(() => Promise.reject(rejected))
|
|
300
|
+
const adapter = new SmtpAdapter({ host: 'unused', port: 587, secure: false, user: '', password: '' }, {
|
|
301
|
+
sendMail,
|
|
302
|
+
} as unknown as Transporter)
|
|
303
|
+
|
|
304
|
+
const result = await adapter.send({
|
|
305
|
+
from: 'noreply@example.com',
|
|
306
|
+
to: 'user@example.com',
|
|
307
|
+
subject: 'x',
|
|
308
|
+
html: '<p>x</p>',
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
expect(result.success).toBe(false)
|
|
312
|
+
if (result.success) {
|
|
313
|
+
throw new Error('unreachable')
|
|
314
|
+
}
|
|
315
|
+
expect(result.status).toBe('errorRejected')
|
|
316
|
+
expect(result.details).toBe('550: Mailbox unavailable')
|
|
317
|
+
})
|
|
318
|
+
})
|
|
319
|
+
`;
|
|
320
|
+
exports.emailmdRendererSource = `import { Logger } from '@nestjs/common'
|
|
321
|
+
import { render } from 'emailmd'
|
|
322
|
+
|
|
323
|
+
import { htmlEscape, stripTags, substituteVariables, substituteVariablesRaw } from './simple.renderer'
|
|
324
|
+
import type { ITemplateRenderer, RenderedMail } from './template-renderer.interface'
|
|
325
|
+
|
|
326
|
+
export class EmailmdRenderer implements ITemplateRenderer {
|
|
327
|
+
private readonly logger = new Logger(EmailmdRenderer.name)
|
|
328
|
+
|
|
329
|
+
public render(source: string, data: Record<string, unknown>): RenderedMail {
|
|
330
|
+
const substituted = substituteVariables(source, data)
|
|
331
|
+
try {
|
|
332
|
+
const { html, text } = render(substituted)
|
|
333
|
+
return { html, text }
|
|
334
|
+
} catch (error) {
|
|
335
|
+
// \`emailmd\` is a pre-1.0 package that parses markdown via MJML;
|
|
336
|
+
// malformed input, internal MJML bugs, or upstream API churn can
|
|
337
|
+
// surface as thrown errors. Swallowing them would lose the mail, but
|
|
338
|
+
// letting them propagate through the dispatcher would fail the
|
|
339
|
+
// action with a confusing stack trace. Log + fall back to a simple
|
|
340
|
+
// escaped HTML + raw plain-text render so the mail still goes out.
|
|
341
|
+
const message = error instanceof Error ? \`\${error.name}: \${error.message}\` : String(error)
|
|
342
|
+
this.logger.warn(\`emailmd render failed, falling back to plain text: \${message}\`)
|
|
343
|
+
const rawText = stripTags(substituteVariablesRaw(source, data))
|
|
344
|
+
return {
|
|
345
|
+
html: \`<pre>\${htmlEscape(rawText)}</pre>\`,
|
|
346
|
+
text: rawText,
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
`;
|
|
352
|
+
exports.emailmdRendererTestSource = `import { describe, expect, it, jest } from '@jest/globals'
|
|
353
|
+
|
|
354
|
+
jest.mock('emailmd', () => ({
|
|
355
|
+
render: jest.fn(),
|
|
356
|
+
}))
|
|
357
|
+
|
|
358
|
+
import { render as emailmdRender } from 'emailmd'
|
|
359
|
+
|
|
360
|
+
import { EmailmdRenderer } from './emailmd.renderer'
|
|
361
|
+
|
|
362
|
+
const mockedRender = emailmdRender as unknown as jest.Mock
|
|
363
|
+
|
|
364
|
+
describe('EmailmdRenderer', () => {
|
|
365
|
+
it('returns the emailmd render output on success', () => {
|
|
366
|
+
mockedRender.mockReturnValueOnce({
|
|
367
|
+
html: '<html><body>Hello</body></html>',
|
|
368
|
+
text: 'Hello',
|
|
369
|
+
meta: {},
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
const renderer = new EmailmdRenderer()
|
|
373
|
+
const result = renderer.render('Hello {{name}}', { name: 'Ada' })
|
|
374
|
+
|
|
375
|
+
expect(result).toEqual({
|
|
376
|
+
html: '<html><body>Hello</body></html>',
|
|
377
|
+
text: 'Hello',
|
|
378
|
+
})
|
|
379
|
+
expect(mockedRender).toHaveBeenCalledWith('Hello Ada')
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
it('falls back to a plaintext render when emailmd throws', () => {
|
|
383
|
+
mockedRender.mockImplementationOnce(() => {
|
|
384
|
+
throw new Error('MJML parse error')
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
const renderer = new EmailmdRenderer()
|
|
388
|
+
const result = renderer.render('Hello {{name}}', { name: 'Ada' })
|
|
389
|
+
|
|
390
|
+
expect(result.text).toBe('Hello Ada')
|
|
391
|
+
expect(result.html).toBe('<pre>Hello Ada</pre>')
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
it('preserves raw characters in fallback text and escapes them in fallback html', () => {
|
|
395
|
+
mockedRender.mockImplementationOnce(() => {
|
|
396
|
+
throw new Error('boom')
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
const renderer = new EmailmdRenderer()
|
|
400
|
+
const result = renderer.render('Plain {{name}}', { name: '<script>alert(1)</script>' })
|
|
401
|
+
|
|
402
|
+
// Text body is the plain-text MIME part — stripTags removes the
|
|
403
|
+
// literal \`<script>\` wrapper and \`alert(1)\` survives.
|
|
404
|
+
expect(result.text).toBe('Plain alert(1)')
|
|
405
|
+
// HTML body wraps the text in \`<pre>\` with all angle brackets escaped.
|
|
406
|
+
expect(result.html).toBe('<pre>Plain alert(1)</pre>')
|
|
407
|
+
})
|
|
408
|
+
})
|
|
409
|
+
`;
|
|
410
|
+
//# sourceMappingURL=conditional-files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditional-files.js","sourceRoot":"","sources":["../../../src/backend-mail/generators/conditional-files.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAEU,QAAA,gBAAgB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqF/B,CAAA;AAEY,QAAA,iBAAiB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6GhC,CAAA;AAEY,QAAA,qBAAqB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6GpC,CAAA;AAEY,QAAA,qBAAqB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BpC,CAAA;AAEY,QAAA,yBAAyB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyDxC,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EnvConfig } from '../../backend-core';
|
|
2
|
+
import type { MailDeliveryAdapter, MailTemplateRenderer } from '../mail.generator';
|
|
3
|
+
export type BuildEnvConfigOptions = {
|
|
4
|
+
adapters: readonly ('log' | MailDeliveryAdapter)[];
|
|
5
|
+
renderers: readonly ('simple' | MailTemplateRenderer)[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Builds the `dotEnvExample`, zod decoder, and transformer blocks for the
|
|
9
|
+
* mail module, only emitting the env vars, decoder entries, and
|
|
10
|
+
* transformer sub-objects for the adapters and renderers that were enabled
|
|
11
|
+
* via `configureBackendMailGenerator`.
|
|
12
|
+
*
|
|
13
|
+
* All three blocks are kept in sync through a single function so that
|
|
14
|
+
* adding a new adapter requires touching exactly one place.
|
|
15
|
+
*/
|
|
16
|
+
export declare function buildEnvConfig({ adapters, renderers }: BuildEnvConfigOptions): EnvConfig;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.buildEnvConfig = buildEnvConfig;
|
|
37
|
+
const Generator = __importStar(require("@postxl/generator"));
|
|
38
|
+
/**
|
|
39
|
+
* Builds the `dotEnvExample`, zod decoder, and transformer blocks for the
|
|
40
|
+
* mail module, only emitting the env vars, decoder entries, and
|
|
41
|
+
* transformer sub-objects for the adapters and renderers that were enabled
|
|
42
|
+
* via `configureBackendMailGenerator`.
|
|
43
|
+
*
|
|
44
|
+
* All three blocks are kept in sync through a single function so that
|
|
45
|
+
* adding a new adapter requires touching exactly one place.
|
|
46
|
+
*/
|
|
47
|
+
function buildEnvConfig({ adapters, renderers }) {
|
|
48
|
+
const hasSes = adapters.includes('ses');
|
|
49
|
+
const hasSmtp = adapters.includes('smtp');
|
|
50
|
+
const adapterEnumLiterals = adapters.map((a) => `'${a}'`).join(', ');
|
|
51
|
+
const rendererEnumLiterals = renderers.map((r) => `'${r}'`).join(', ');
|
|
52
|
+
const dotEnvAdapterHint = adapters.join(' | ');
|
|
53
|
+
const dotEnvRendererHint = renderers.join(' | ');
|
|
54
|
+
const dotEnvExample = [
|
|
55
|
+
` # Mail module configuration`,
|
|
56
|
+
` MAIL_ENABLED=false`,
|
|
57
|
+
` # Delivery adapter: ${dotEnvAdapterHint}`,
|
|
58
|
+
` MAIL_ADAPTER=log`,
|
|
59
|
+
` # Template renderer: ${dotEnvRendererHint}`,
|
|
60
|
+
` MAIL_TEMPLATE_RENDERER=simple`,
|
|
61
|
+
` MAIL_FROM=""`,
|
|
62
|
+
` # Comma-separated list of admin addresses used when informAdmins=true`,
|
|
63
|
+
` MAIL_ADMINS=""`,
|
|
64
|
+
hasSes
|
|
65
|
+
? `
|
|
66
|
+
# SES adapter (only when MAIL_ADAPTER=ses)
|
|
67
|
+
MAIL_SES_REGION="eu-central-1"
|
|
68
|
+
MAIL_SES_USE_CREDENTIALS=false
|
|
69
|
+
MAIL_SES_ACCESS_KEY=""
|
|
70
|
+
MAIL_SES_SECRET_ACCESS_KEY=""`
|
|
71
|
+
: null,
|
|
72
|
+
hasSmtp
|
|
73
|
+
? `
|
|
74
|
+
# SMTP adapter (only when MAIL_ADAPTER=smtp)
|
|
75
|
+
MAIL_SMTP_HOST=""
|
|
76
|
+
MAIL_SMTP_PORT=587
|
|
77
|
+
MAIL_SMTP_SECURE=false
|
|
78
|
+
MAIL_SMTP_USER=""
|
|
79
|
+
MAIL_SMTP_PASSWORD=""`
|
|
80
|
+
: null,
|
|
81
|
+
]
|
|
82
|
+
.filter((line) => line !== null)
|
|
83
|
+
.join('\n');
|
|
84
|
+
const decoderLines = [
|
|
85
|
+
` MAIL_ENABLED: zEnvBoolean.optional().default(false),`,
|
|
86
|
+
` MAIL_ADAPTER: z.enum([${adapterEnumLiterals}]).optional().default('log'),`,
|
|
87
|
+
` MAIL_TEMPLATE_RENDERER: z.enum([${rendererEnumLiterals}]).optional().default('simple'),`,
|
|
88
|
+
` MAIL_FROM: z.string().optional().default(''),`,
|
|
89
|
+
` MAIL_ADMINS: z.string().optional().default(''),`,
|
|
90
|
+
];
|
|
91
|
+
if (hasSes) {
|
|
92
|
+
decoderLines.push(` MAIL_SES_REGION: z.string().optional().default('eu-central-1'),`, ` MAIL_SES_USE_CREDENTIALS: zEnvBoolean.optional().default(false),`, ` MAIL_SES_ACCESS_KEY: z.string().optional(),`, ` MAIL_SES_SECRET_ACCESS_KEY: z.string().optional(),`);
|
|
93
|
+
}
|
|
94
|
+
if (hasSmtp) {
|
|
95
|
+
decoderLines.push(` MAIL_SMTP_HOST: z.string().optional().default(''),`, ` MAIL_SMTP_PORT: z.coerce.number().int().positive().optional().default(587),`, ` MAIL_SMTP_SECURE: zEnvBoolean.optional().default(false),`, ` MAIL_SMTP_USER: z.string().optional().default(''),`, ` MAIL_SMTP_PASSWORD: z.string().optional().default(''),`);
|
|
96
|
+
}
|
|
97
|
+
const decoder = decoderLines.join('\n');
|
|
98
|
+
// The transformer body validates the configured selection at boot
|
|
99
|
+
// (fail-fast) and emits ses/smtp sub-objects only when the matching
|
|
100
|
+
// adapter is enabled. Everything is wrapped in an IIFE so the
|
|
101
|
+
// validation throws land inside .transform().
|
|
102
|
+
const transformerValidations = [
|
|
103
|
+
` if (val.MAIL_ENABLED && from.length === 0) {
|
|
104
|
+
throw new Error('MAIL_ENABLED=true requires a non-empty MAIL_FROM')
|
|
105
|
+
}`,
|
|
106
|
+
];
|
|
107
|
+
if (hasSes) {
|
|
108
|
+
transformerValidations.push(` if (val.MAIL_ENABLED && adapter === 'ses' && val.MAIL_SES_USE_CREDENTIALS) {
|
|
109
|
+
const hasAccessKey = typeof val.MAIL_SES_ACCESS_KEY === 'string' && val.MAIL_SES_ACCESS_KEY.length > 0
|
|
110
|
+
const hasSecret =
|
|
111
|
+
typeof val.MAIL_SES_SECRET_ACCESS_KEY === 'string' && val.MAIL_SES_SECRET_ACCESS_KEY.length > 0
|
|
112
|
+
if (!hasAccessKey || !hasSecret) {
|
|
113
|
+
throw new Error(
|
|
114
|
+
'MAIL_SES_USE_CREDENTIALS=true requires both MAIL_SES_ACCESS_KEY and MAIL_SES_SECRET_ACCESS_KEY',
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
}`);
|
|
118
|
+
}
|
|
119
|
+
if (hasSmtp) {
|
|
120
|
+
transformerValidations.push(` if (val.MAIL_ENABLED && adapter === 'smtp' && val.MAIL_SMTP_HOST.trim().length === 0) {
|
|
121
|
+
throw new Error('MAIL_ENABLED=true with MAIL_ADAPTER=smtp requires MAIL_SMTP_HOST')
|
|
122
|
+
}`);
|
|
123
|
+
}
|
|
124
|
+
const transformerSubObjects = [];
|
|
125
|
+
if (hasSes) {
|
|
126
|
+
transformerSubObjects.push(` ses: {
|
|
127
|
+
region: val.MAIL_SES_REGION,
|
|
128
|
+
useCredentials: val.MAIL_SES_USE_CREDENTIALS,
|
|
129
|
+
accessKey: val.MAIL_SES_ACCESS_KEY,
|
|
130
|
+
secretAccessKey: val.MAIL_SES_SECRET_ACCESS_KEY,
|
|
131
|
+
},`);
|
|
132
|
+
}
|
|
133
|
+
if (hasSmtp) {
|
|
134
|
+
transformerSubObjects.push(` smtp: {
|
|
135
|
+
host: val.MAIL_SMTP_HOST,
|
|
136
|
+
port: val.MAIL_SMTP_PORT,
|
|
137
|
+
secure: val.MAIL_SMTP_SECURE,
|
|
138
|
+
user: val.MAIL_SMTP_USER,
|
|
139
|
+
password: val.MAIL_SMTP_PASSWORD,
|
|
140
|
+
},`);
|
|
141
|
+
}
|
|
142
|
+
const transformer = ` mail: (() => {
|
|
143
|
+
const from = val.MAIL_FROM.trim()
|
|
144
|
+
const adapter = val.MAIL_ADAPTER
|
|
145
|
+
|
|
146
|
+
${transformerValidations.join('\n\n')}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
isEnabled: val.MAIL_ENABLED,
|
|
150
|
+
adapter,
|
|
151
|
+
templateRenderer: val.MAIL_TEMPLATE_RENDERER,
|
|
152
|
+
from,
|
|
153
|
+
admins: val.MAIL_ADMINS
|
|
154
|
+
? val.MAIL_ADMINS.split(',')
|
|
155
|
+
.map((s) => s.trim())
|
|
156
|
+
.filter(Boolean)
|
|
157
|
+
: [],
|
|
158
|
+
${transformerSubObjects.join('\n')}
|
|
159
|
+
}
|
|
160
|
+
})(),`;
|
|
161
|
+
return {
|
|
162
|
+
dotEnvExample,
|
|
163
|
+
decoder: Generator.ts(decoder),
|
|
164
|
+
transformer: Generator.ts(transformer),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=env-config.generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-config.generator.js","sourceRoot":"","sources":["../../../src/backend-mail/generators/env-config.generator.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MailDeliveryAdapter, MailTemplateRenderer } from '../mail.generator';
|
|
2
|
+
export type GenerateMailServiceOptions = {
|
|
3
|
+
adapters: readonly ('log' | MailDeliveryAdapter)[];
|
|
4
|
+
renderers: readonly ('simple' | MailTemplateRenderer)[];
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Generates `mail.service.ts` with imports and factory switches that only
|
|
8
|
+
* reference the enabled adapters and renderers. Everything else in the
|
|
9
|
+
* file is static.
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateMailService({ adapters, renderers }: GenerateMailServiceOptions): string;
|