Compare commits

...

4 Commits

Author SHA1 Message Date
Bas
c78a8c2480 Merge pull request #23 from xtr-dev/dev
Fix TypeScript compatibility with PayloadCMS generated types
2025-09-13 21:10:09 +02:00
a27481c818 Bump package version to 0.1.7 in package.json. 2025-09-13 21:07:22 +02:00
b342f32d97 Simplify null checks in sendEmail validation logic 2025-09-13 21:06:54 +02:00
e1800f5a6e Fix TypeScript compatibility with PayloadCMS generated types
Resolves: TS2344: Type Email does not satisfy the constraint BaseEmailData
- Add null support to BaseEmailData interface for all optional fields
- Update parseAndValidateEmails to handle null values
- Update sendEmail validation to properly check for null values
- Maintain compatibility with PayloadCMS generated types that include null

Generated Email types like cc?: string[] | null | undefined now work correctly.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-13 21:03:21 +02:00
3 changed files with 13 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@xtr-dev/payload-mailing", "name": "@xtr-dev/payload-mailing",
"version": "0.1.6", "version": "0.1.7",
"description": "Template-based email system with scheduling and job processing for PayloadCMS", "description": "Template-based email system with scheduling and job processing for PayloadCMS",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -2,15 +2,16 @@ import { Payload } from 'payload'
import { getMailing, renderTemplate, parseAndValidateEmails } from './utils/helpers.js' import { getMailing, renderTemplate, parseAndValidateEmails } from './utils/helpers.js'
// Base type for email data that all emails must have // Base type for email data that all emails must have
// Compatible with PayloadCMS generated types that include null
export interface BaseEmailData { export interface BaseEmailData {
to: string | string[] to: string | string[]
cc?: string | string[] cc?: string | string[] | null
bcc?: string | string[] bcc?: string | string[] | null
subject?: string subject?: string | null
html?: string html?: string | null
text?: string text?: string | null
scheduledAt?: string | Date scheduledAt?: string | Date | null
priority?: number priority?: number | null
[key: string]: any [key: string]: any
} }
@@ -82,7 +83,7 @@ export const sendEmail = async <T extends BaseEmailData = BaseEmailData>(
throw new Error('Fields "subject" and "html" are required when not using a template') throw new Error('Fields "subject" and "html" are required when not using a template')
} }
// Process email addresses using shared validation // Process email addresses using shared validation (handle null values)
if (emailData.to) { if (emailData.to) {
emailData.to = parseAndValidateEmails(emailData.to as string | string[]) emailData.to = parseAndValidateEmails(emailData.to as string | string[])
} }

View File

@@ -5,8 +5,8 @@ import { TemplateVariables } from '../types/index.js'
* Parse and validate email addresses * Parse and validate email addresses
* @internal * @internal
*/ */
export const parseAndValidateEmails = (emails: string | string[] | undefined): string[] | undefined => { export const parseAndValidateEmails = (emails: string | string[] | null | undefined): string[] | undefined => {
if (!emails) return undefined if (!emails || emails === null) return undefined
let emailList: string[] let emailList: string[]
if (Array.isArray(emails)) { if (Array.isArray(emails)) {