nuxt-mail
Version:
Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.
28 lines • 915 B
JavaScript
import defu from "defu";
import { omit } from "lodash-es";
const resolveConfig = (bodyInput, options) => {
const bodyWithDefaults = defu(bodyInput, {
config: 0
});
if (typeof bodyWithDefaults.config === "string") {
const configIndex = options.message.findIndex(_ => _.name === bodyWithDefaults.config);
if (configIndex === -1) {
throw new Error(`Message config with name '${bodyWithDefaults.config}' not found.`);
}
return {
...bodyWithDefaults,
config: configIndex
};
}
return bodyWithDefaults;
};
export default async (bodyInput, options, transport) => {
const body = resolveConfig(bodyInput, options);
if (!options.message[body.config]) {
throw new Error(`Message config not found at index ${body.config}.`);
}
await transport.sendMail({
...omit(body, ["config", "to", "cc", "bcc"]),
...omit(options.message[body.config], ["name"])
});
};