UNPKG

nuxt-mailchannels

Version:

MailChannels Email API integration for nuxt

43 lines (42 loc) 1.49 kB
import { MailChannelsClient } from "@yizack/mailchannels"; import { Emails } from "@yizack/mailchannels/modules"; import { createError } from "h3"; import { overrideRecipient } from "../utils/helpers.js"; import { useRuntimeConfig } from "#imports"; export const useMailChannels = (event) => { const config = useRuntimeConfig(event).mailchannels; if (!config.apiKey) { throw createError({ statusCode: 500, message: "Missing MailChannels API key" }); } const mailchannels = new MailChannelsClient(config.apiKey); const emails = new Emails(mailchannels); const send = async (options, dryRun) => { const overrides = { ...options, // @ts-expect-error optional since can receive a default value and package will handle the error to: overrideRecipient(config.to, options.to), // @ts-expect-error optional since can receive a default value and package will handle the error from: overrideRecipient(config.from, options.from), cc: overrideRecipient(config.cc, options.cc), bcc: overrideRecipient(config.bcc, options.bcc), dkim: config.dkim }; const { success, data, error } = await emails.send(overrides, dryRun).catch((error2) => { throw createError({ statusCode: 500, message: error2.message }); }); if (!success && error) { throw createError({ statusCode: 500, message: error }); } return { success, data }; }; return { send }; };