UNPKG

alepha

Version:

Easy-to-use modern TypeScript framework for building many kind of applications.

126 lines (125 loc) 3.38 kB
import { $module, AlephaError, KIND, Primitive, createPrimitive } from "alepha"; import { $logger } from "alepha/logger"; //#region ../../src/email/core/providers/EmailProvider.ts /** * Email provider interface. * * All methods are asynchronous and return promises. */ var EmailProvider = class {}; //#endregion //#region ../../src/email/core/providers/MemoryEmailProvider.ts var MemoryEmailProvider = class { log = $logger(); records = []; async send(options) { const { to, subject, body } = options; this.log.debug("Sending email to memory store", { to, subject }); for (const recipient of Array.isArray(to) ? to : [to]) this.records.push({ to: recipient, subject, body, sentAt: /* @__PURE__ */ new Date() }); } /** * Get the last email sent (for testing purposes). */ get last() { return this.records[this.records.length - 1]; } }; //#endregion //#region ../../src/email/core/primitives/$email.ts const $email = (options = {}) => createPrimitive(EmailPrimitive, options); /** * Email primitive for sending emails through various providers. * * The primitive's `name` identifies the channel in the `email:sending` / * `email:sent` hooks; it does not select a template — `send()` expects an * already-rendered `subject` and `body`. * * Usage: * ```typescript * class MyService { * protected readonly welcomeEmail = $email({ name: "welcome" }); * * async sendWelcome(userEmail: string, userName: string) { * await this.welcomeEmail.send({ * to: userEmail, * subject: "Welcome!", * body: `<p>Hello ${userName}!</p>` * }); * } * } * ``` */ var EmailPrimitive = class extends Primitive { provider = this.$provider(); get name() { return this.options.name ?? `${this.config.propertyKey}`; } /** * Send an email using the configured provider. */ async send(options) { await this.alepha.events.emit("email:sending", { to: options.to, template: this.name, variables: {}, provider: this.provider, abort: () => { throw new AlephaError("Email sending aborted by hook"); } }); await this.provider.send(options); await this.alepha.events.emit("email:sent", { to: options.to, template: this.name, provider: this.provider }); } $provider() { if (!this.options.provider) return this.alepha.inject(EmailProvider); if (this.options.provider === "memory") return this.alepha.inject(MemoryEmailProvider); return this.alepha.inject(this.options.provider); } }; $email[KIND] = EmailPrimitive; //#endregion //#region ../../src/email/core/errors/EmailError.ts var EmailError = class extends AlephaError { constructor(message, cause) { super(message); this.name = "EmailError"; this.cause = cause; } }; //#endregion //#region ../../src/email/core/index.workerd.ts /** * Email delivery for Cloudflare Workers. * * Uses Memory provider by default. For production email delivery, * add `AlephaEmailBrevo` from `alepha/email/brevo`. * * @module alepha.email */ const AlephaEmail = $module({ name: "alepha.email", primitives: [$email], services: [EmailProvider, MemoryEmailProvider], register: (alepha) => { alepha.with({ optional: true, provide: EmailProvider, use: MemoryEmailProvider }); } }); //#endregion export { $email, AlephaEmail, EmailError, EmailPrimitive, EmailProvider, MemoryEmailProvider }; //# sourceMappingURL=index.workerd.js.map