mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Refactor email handling for improved type safety and extensibility
- Replace `BaseEmailData` with `Email` type for stricter type validation - Update `sendEmail` API to support new typed structure and template integration - Migrate to `@xtr-dev/payload-mailing` for enhanced email sending capabilities - Remove unnecessary null checks and redundant code in email scheduling logic - Regenerate PayloadCMS types for aligning with revised schema changes - Update dev scripts and imports for seamless compatibility with the new email module
This commit is contained in:
@@ -1,22 +1,9 @@
|
||||
import { Payload } from 'payload'
|
||||
import { getMailing, renderTemplate, parseAndValidateEmails } from './utils/helpers.js'
|
||||
|
||||
// Base type for email data that all emails must have
|
||||
// Compatible with PayloadCMS generated types that include null
|
||||
export interface BaseEmailData {
|
||||
to: string | string[]
|
||||
cc?: string | string[] | null
|
||||
bcc?: string | string[] | null
|
||||
subject?: string | null
|
||||
html?: string | null
|
||||
text?: string | null
|
||||
scheduledAt?: string | Date | null
|
||||
priority?: number | null
|
||||
[key: string]: any
|
||||
}
|
||||
import {Email} from "./payload-types.js"
|
||||
|
||||
// Options for sending emails
|
||||
export interface SendEmailOptions<T extends BaseEmailData = BaseEmailData> {
|
||||
export interface SendEmailOptions<T extends Email = Email> {
|
||||
// Template-based email
|
||||
template?: {
|
||||
slug: string
|
||||
@@ -48,7 +35,7 @@ export interface SendEmailOptions<T extends BaseEmailData = BaseEmailData> {
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
export const sendEmail = async <T extends BaseEmailData = BaseEmailData>(
|
||||
export const sendEmail = async <T extends Email = Email>(
|
||||
payload: Payload,
|
||||
options: SendEmailOptions<T>
|
||||
): Promise<T> => {
|
||||
@@ -94,11 +81,6 @@ export const sendEmail = async <T extends BaseEmailData = BaseEmailData>(
|
||||
emailData.bcc = parseAndValidateEmails(emailData.bcc as string | string[])
|
||||
}
|
||||
|
||||
// Convert scheduledAt to ISO string if it's a Date
|
||||
if (emailData.scheduledAt instanceof Date) {
|
||||
emailData.scheduledAt = emailData.scheduledAt.toISOString()
|
||||
}
|
||||
|
||||
// Create the email in the collection
|
||||
const email = await payload.create({
|
||||
collection: collectionSlug as any,
|
||||
|
||||
Reference in New Issue
Block a user