diff --git a/jsconfig.json b/jsconfig.json index d15c7ca..ed4318a 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -6,8 +6,8 @@ "lib": ["es2016"], // silences wrong TS error, we don't compile, we only typecheck "outDir": "./irrelevant/unused", - "allowJs": true, - "checkJs": true, + // "allowJs": true, + // "checkJs": true, "noImplicitThis": true, "noUnusedLocals": true, "noUnusedParameters": true, diff --git a/lib/private/mail/send.js b/lib/private/mail/send.js index 02727a9..0446157 100644 --- a/lib/private/mail/send.js +++ b/lib/private/mail/send.js @@ -79,7 +79,8 @@ module.exports = { 'An override for the default "from" email that\'s been configured.', example: 'anne.martin@example.com', isEmail: true, - defaultsTo: sails.config.mail.from.address || process.env.MAIL_FROM_ADDRESS + defaultsTo: + sails.config.mail.from.address || process.env.MAIL_FROM_ADDRESS }, fromName: { @@ -91,7 +92,7 @@ module.exports = { replyTo: { description: 'An email address that will appear on the Reply-To: field', example: 'example@example.com', - defaultsTo: sails.config.mail.replyTo || process.env.MAIL_REPLY_TO + defaultsTo: sails.config.mail.replyTo || process.env.MAIL_REPLY_TO }, layout: { @@ -111,6 +112,28 @@ module.exports = { }, text: { example: 'Hello world' + }, + // Mailtrap transport specific options + templateUuid: { + type: 'string', + description: 'The UUID of the template to use.' + }, + templateVariables: { + type: 'ref', + description: 'An object with the variables to use in the template.' + }, + category: { + type: 'string', + description: 'The category of the email.' + }, + customVariables: { + type: 'ref', + description: 'An object with the custom variables to use in the email.' + }, + testInboxId: { + type: 'string', + description: 'The ID of the test inbox to use.', + defaultsTo: process.env.MAILTRAP_TEST_INBOX_ID } }, @@ -133,7 +156,13 @@ module.exports = { text, cc, bcc, - attachments + attachments, + // Mailtrap transport specific options + templateUuid, + templateVariables, + category, + customVariables, + testInboxId }) { if (template && !template.startsWith('email-')) { sails.log.warn( @@ -227,6 +256,43 @@ module.exports = { }) sails.log.debug('Email sent: %s', smtpInfo.messageId) break + case 'mailtrap': + const { MailtrapClient } = getModule('mailtrap') + testInboxId = testInboxId + ? testInboxId + : sails.config.mail.mailers[mailer]?.testInboxId + const mailtrapClientOptions = { + token: + sails.config.mail.mailers[mailer]?.token || + process.env.MAILTRAP_TOKEN, + testInboxId, + accountId: + sails.config.mail.mailers[mailer]?.accountId || + process.env.MAILTRAP_ACCOUNT_ID + } + const mailtrap = new MailtrapClient({ ...mailtrapClientOptions }) + const mail = { + category, + from: { name: fromName, email: fromAddress }, + to: [{ email: to }], + subject, + text, + html, + attachments, + template_uuid: templateUuid, + template_variables: templateVariables, + custom_variables: customVariables + } + let mailtrapInfo + if (testInboxId) { + mailtrapInfo = await mailtrap.testing.send({ ...mail }) + } else { + mailtrapInfo = await mailtrap.send({ ...mail }) + } + + mailtrapInfo = await mailtrap.send({ ...mail }) + sails.log.debug('Email sent: %s', mailtrapInfo.message_ids.join(', ')) + break case 'resend': const { Resend } = getModule('resend') const apiKey = diff --git a/types/index.d.ts b/types/index.d.ts deleted file mode 100644 index 7284d31..0000000 --- a/types/index.d.ts +++ /dev/null @@ -1,72 +0,0 @@ -interface Sails { - log: LogMethod & LogObject - helpers: Helper - on(event: string, listener: (...args: any[]) => void): void - off(event: string, listener: (...args: any[]) => void): void - emit(event: string, ...args: any[]): void - lift(cb?: (err: Error, sails: Sails) => void): Sails - lower(cb?: (err?: Error) => void): void - load(): Sails - config: Config - renderView: ( - relPathToView: string, - _options: Dictionary, - optionalCb?: (err: Error | null, compiledHtml: string) => void - ) => Sails & Promise - intercept(callback: (err: Error) => Error): Sails & Promise -} - -interface Helper { - mail: { - send: { - with: (params: EmailParams) => Promise - } - } -} -interface EmailParams { - mailer?: string - to: string - subject?: string - template?: string - templateData?: object -} - -interface Hook { - inertia: Inertia -} -interface LogMethod { - (...args: any[]): void -} - -interface LogObject { - info: LogMethod - warn: LogMethod - error: LogMethod - debug: LogMethod - silly: LogMethod - verbose: LogMethod -} - -interface Config { - mailer: string - mail: { - default: string - mailers: { - log: object - smtp: { - transport: 'smtp' - host: string - port: number - encryption: 'tls' | 'ssl' - username?: string - password?: string - } - } - from: { - name: string - address: string - } - } -} - -declare const sails: Sails