Skip to content

Commit

Permalink
refactor: rename drivers to transports
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 18, 2023
1 parent 10be8b2 commit 6eb6ee4
Show file tree
Hide file tree
Showing 34 changed files with 445 additions and 507 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<br />

<div align="center">
<h3>Mailer</h3>
<p>AdonisJS mailer built on top of nodemailer. Uses driver based approach to support multiple email providers.</p>
<h3>Mail</h3>
<p>
The AdonisJS Mail package is built on top of Nodemailer with support for queueing emails, better testing experience and an extensive suite of officially maintained transports.
</p>
</div>

<br />
Expand Down
16 changes: 8 additions & 8 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function configure(command: Configure) {
resend: ['RESEND_API_KEY'],
}

const drivers = await command.prompt.multiple('Select the mail services you want to use', [
const transports = await command.prompt.multiple('Select the mail services you want to use', [
'smtp',
'ses',
'resend',
Expand All @@ -33,7 +33,7 @@ export async function configure(command: Configure) {
* Publish config file
*/
await command.publishStub('config.stub', {
drivers: drivers,
transports: transports,
})

const codemods = await command.createCodemods()
Expand All @@ -47,24 +47,24 @@ export async function configure(command: Configure) {
})

/**
* Define env variables for the selected drivers
* Define env variables for the selected transports
*/
await codemods.defineEnvVariables(
drivers.reduce<Record<string, string>>((result, driver) => {
envVariables[driver].forEach((envVariable) => {
transports.reduce<Record<string, string>>((result, transport) => {
envVariables[transport].forEach((envVariable) => {
result[envVariable] = ''
})
return result
}, {})
)

/**
* Define env variables validation for the selected drivers
* Define env variables validation for the selected transports
*/
await codemods.defineEnvValidations({
leadingComment: 'Variables for configuring the mail package',
variables: drivers.reduce<Record<string, string>>((result, driver) => {
envVariables[driver].forEach((envVariable) => {
variables: transports.reduce<Record<string, string>>((result, transport) => {
envVariables[transport].forEach((envVariable) => {
result[envVariable] = 'Env.schema.string()'
})
return result
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export { BaseMail } from './src/base_mail.js'
export { FakeMailer } from './src/fake_mailer.js'
export { MailManager } from './src/mail_manager.js'
export { MailResponse } from './src/mail_response.js'
export { defineConfig, drivers } from './src/define_config.js'
export { defineConfig, transports } from './src/define_config.js'
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"./commands": "./build/commands/main.js",
"./services/main": "./build/services/main.js",
"./mail_provider": "./build/providers/mail_provider.js",
"./drivers/ses": "./build/src/drivers/ses/main.js",
"./drivers/json": "./build/src/drivers/json/main.js",
"./drivers/smtp": "./build/src/drivers/smtp/main.js",
"./drivers/resend": "./build/src/drivers/resend/main.js",
"./drivers/mailgun": "./build/src/drivers/mailgun/main.js",
"./drivers/sparkpost": "./build/src/drivers/sparkpost/main.js",
"./transports/ses": "./build/src/transports/ses.js",
"./transports/json": "./build/src/transports/json.js",
"./transports/smtp": "./build/src/transports/smtp.js",
"./transports/resend": "./build/src/transports/resend.js",
"./transports/mailgun": "./build/src/transports/mailgun.js",
"./transports/sparkpost": "./build/src/transports/sparkpost.js",
"./messengers/memory": "./build/src/messengers/memory_queue.js"
},
"scripts": {
Expand Down Expand Up @@ -152,12 +152,12 @@
"services/main.ts",
"commands/make_mail.ts",
"providers/mail_provider.ts",
"src/drivers/ses/main.ts",
"src/drivers/json/main.ts",
"src/drivers/resend/main.ts",
"src/drivers/smtp/main.ts",
"src/drivers/mailgun/main.ts",
"src/drivers/sparkpost/main.ts",
"src/transports/ses.ts",
"src/transports/json.ts",
"src/transports/resend.ts",
"src/transports/smtp.ts",
"src/transports/mailgun.ts",
"src/transports/sparkpost.ts",
"src/messengers/memory_queue.ts"
],
"outDir": "./build",
Expand Down
6 changes: 3 additions & 3 deletions src/base_mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { Mailer } from './mailer.js'
import { Message } from './message.js'
import { MailDriverContract, Recipient } from './types.js'
import { MailTransportContract, Recipient } from './types.js'

/**
* Class based emails are self contained dispatchable
Expand Down Expand Up @@ -106,7 +106,7 @@ export abstract class BaseMail {
/**
* Sends the mail
*/
async send<T extends MailDriverContract>(
async send<T extends MailTransportContract>(
mailer: Mailer<T>,
config?: Parameters<T['send']>[1]
): Promise<Awaited<ReturnType<T['send']>>> {
Expand All @@ -118,7 +118,7 @@ export abstract class BaseMail {
* Sends the mail by using the background
* messenger
*/
async sendLater<T extends MailDriverContract>(
async sendLater<T extends MailTransportContract>(
mailer: Mailer<T>,
config?: Parameters<T['send']>[1]
) {
Expand Down
62 changes: 31 additions & 31 deletions src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
import { configProvider } from '@adonisjs/core'
import type { ConfigProvider } from '@adonisjs/core/types'

import type { SESDriver } from './drivers/ses/main.js'
import type { SMTPDriver } from './drivers/smtp/main.js'
import type { ResendDriver } from './drivers/resend/main.js'
import type { MailgunDriver } from './drivers/mailgun/main.js'
import type { SparkPostDriver } from './drivers/sparkpost/main.js'
import type { SESTransport } from './transports/ses.js'
import type { SMTPTransport } from './transports/smtp.js'
import type { MailgunTransport } from './transports/mailgun.js'
import type { ResendTransport } from './transports/resend.js'
import type { SparkPostTransport } from './transports/sparkpost.js'
import type {
SESConfig,
SMTPConfig,
MailerConfig,
ResendConfig,
MailgunConfig,
SparkPostConfig,
MailManagerDriverFactory,
MailManagerTransportFactory,
} from './types.js'

/**
* Helper to remap known mailers to factory functions
*/
type ResolvedConfig<KnownMailers extends Record<string, MailManagerDriverFactory>> =
type ResolvedConfig<KnownMailers extends Record<string, MailManagerTransportFactory>> =
MailerConfig & {
default?: keyof KnownMailers
mailers: {
Expand All @@ -42,7 +42,7 @@ type ResolvedConfig<KnownMailers extends Record<string, MailManagerDriverFactory
* Helper function to define config for the mail
* service
*/
export function defineConfig<KnownMailers extends Record<string, MailManagerDriverFactory>>(
export function defineConfig<KnownMailers extends Record<string, MailManagerTransportFactory>>(
config: MailerConfig & {
default?: keyof KnownMailers
mailers: {
Expand All @@ -53,64 +53,64 @@ export function defineConfig<KnownMailers extends Record<string, MailManagerDriv
return configProvider.create(async (app) => {
const { mailers, default: defaultMailer, ...rest } = config
const mailersNames = Object.keys(mailers)
const drivers = {} as Record<string, MailManagerDriverFactory>
const transports = {} as Record<string, MailManagerTransportFactory>

for (let mailerName of mailersNames) {
const mailerDriver = mailers[mailerName]
if (typeof mailerDriver === 'function') {
drivers[mailerName] = mailerDriver
const mailerTransport = mailers[mailerName]
if (typeof mailerTransport === 'function') {
transports[mailerName] = mailerTransport
} else {
drivers[mailerName] = await mailerDriver.resolver(app)
transports[mailerName] = await mailerTransport.resolver(app)
}
}

return {
default: defaultMailer,
mailers: drivers,
mailers: transports,
...rest,
} as ResolvedConfig<KnownMailers>
})
}

/**
* Config helpers to create a reference for inbuilt
* mail drivers
* mail transports
*/
export const drivers: {
smtp: (config: SMTPConfig) => ConfigProvider<() => SMTPDriver>
ses: (config: SESConfig) => ConfigProvider<() => SESDriver>
mailgun: (config: MailgunConfig) => ConfigProvider<() => MailgunDriver>
sparkpost: (config: SparkPostConfig) => ConfigProvider<() => SparkPostDriver>
resend: (config: ResendConfig) => ConfigProvider<() => ResendDriver>
export const transports: {
smtp: (config: SMTPConfig) => ConfigProvider<() => SMTPTransport>
ses: (config: SESConfig) => ConfigProvider<() => SESTransport>
mailgun: (config: MailgunConfig) => ConfigProvider<() => MailgunTransport>
sparkpost: (config: SparkPostConfig) => ConfigProvider<() => SparkPostTransport>
resend: (config: ResendConfig) => ConfigProvider<() => ResendTransport>
} = {
smtp(config) {
return configProvider.create(async () => {
const { SMTPDriver } = await import('./drivers/smtp/main.js')
return () => new SMTPDriver(config)
const { SMTPTransport } = await import('./transports/smtp.js')
return () => new SMTPTransport(config)
})
},
ses(config) {
return configProvider.create(async () => {
const { SESDriver } = await import('./drivers/ses/main.js')
return () => new SESDriver(config)
const { SESTransport } = await import('./transports/ses.js')
return () => new SESTransport(config)
})
},
mailgun(config) {
return configProvider.create(async () => {
const { MailgunDriver } = await import('./drivers/mailgun/main.js')
return () => new MailgunDriver(config)
const { MailgunTransport } = await import('./transports/mailgun.js')
return () => new MailgunTransport(config)
})
},
sparkpost(config) {
return configProvider.create(async () => {
const { SparkPostDriver } = await import('./drivers/sparkpost/main.js')
return () => new SparkPostDriver(config)
const { SparkPostTransport } = await import('./transports/sparkpost.js')
return () => new SparkPostTransport(config)
})
},
resend(config) {
return configProvider.create(async () => {
const { ResendDriver } = await import('./drivers/resend/main.js')
return () => new ResendDriver(config)
const { ResendTransport } = await import('./transports/resend.js')
return () => new ResendTransport(config)
})
},
}
57 changes: 0 additions & 57 deletions src/drivers/json/main.ts

This file was deleted.

46 changes: 0 additions & 46 deletions src/drivers/mailgun/main.ts

This file was deleted.

Loading

0 comments on commit 6eb6ee4

Please sign in to comment.