mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f04275d39 | ||
| 20afe30e88 | |||
| 02b3fecadf | |||
|
|
ea87f14308 | ||
| 6886027727 | |||
| 965569be06 | |||
|
|
ff788c1ecf | ||
| c12438aaa2 | |||
| 4dcbc1446a | |||
|
|
72f3d7f66d | ||
| ecc0b0a73e | |||
| a959673fc1 | |||
| 8809db6aff |
@@ -38,50 +38,57 @@ const customEmail = await sendEmail<MyEmail>(payload, {
|
||||
})
|
||||
```
|
||||
|
||||
## ID Type Compatibility
|
||||
## Compatibility
|
||||
|
||||
The plugin works with both:
|
||||
The plugin works with:
|
||||
- **String IDs**: `id: string`
|
||||
- **Number IDs**: `id: number`
|
||||
- **Nullable fields**: Fields can be `null`, `undefined`, or have values
|
||||
- **Date fields**: Timestamp fields support both `Date` objects and `string` (ISO) formats
|
||||
- **JSON variables**: Variables field supports any JSON-compatible value type
|
||||
- **Generated types**: Works with `payload generate:types` output
|
||||
|
||||
Your Payload configuration determines which type is used. The plugin automatically adapts to your setup.
|
||||
Your Payload configuration determines which types are used. The plugin automatically adapts to your setup.
|
||||
|
||||
## Type Definitions
|
||||
|
||||
The base interfaces provided by the plugin:
|
||||
|
||||
```typescript
|
||||
// JSON value type that matches Payload's JSON field type
|
||||
type JSONValue = string | number | boolean | { [k: string]: unknown } | unknown[] | null | undefined
|
||||
|
||||
interface BaseEmailDocument {
|
||||
id: string | number
|
||||
template?: any
|
||||
to: string[]
|
||||
cc?: string[]
|
||||
bcc?: string[]
|
||||
from?: string
|
||||
replyTo?: string
|
||||
cc?: string[] | null
|
||||
bcc?: string[] | null
|
||||
from?: string | null
|
||||
replyTo?: string | null
|
||||
subject: string
|
||||
html: string
|
||||
text?: string
|
||||
variables?: Record<string, any>
|
||||
scheduledAt?: string
|
||||
sentAt?: string
|
||||
status?: 'pending' | 'processing' | 'sent' | 'failed'
|
||||
attempts?: number
|
||||
lastAttemptAt?: string
|
||||
error?: string
|
||||
priority?: number
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
text?: string | null
|
||||
variables?: JSONValue // Supports any JSON-compatible value
|
||||
scheduledAt?: string | Date | null
|
||||
sentAt?: string | Date | null
|
||||
status?: 'pending' | 'processing' | 'sent' | 'failed' | null
|
||||
attempts?: number | null
|
||||
lastAttemptAt?: string | Date | null
|
||||
error?: string | null
|
||||
priority?: number | null
|
||||
createdAt?: string | Date | null
|
||||
updatedAt?: string | Date | null
|
||||
}
|
||||
|
||||
interface BaseEmailTemplateDocument {
|
||||
id: string | number
|
||||
name: string
|
||||
slug: string
|
||||
subject?: string
|
||||
subject?: string | null
|
||||
content?: any
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
createdAt?: string | Date | null
|
||||
updatedAt?: string | Date | null
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-mailing",
|
||||
"version": "0.1.13",
|
||||
"version": "0.1.17",
|
||||
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@@ -15,7 +15,7 @@ export interface SendEmailTaskInput {
|
||||
to: string | string[]
|
||||
cc?: string | string[]
|
||||
bcc?: string | string[]
|
||||
scheduledAt?: string // ISO date string
|
||||
scheduledAt?: string | Date // ISO date string or Date object
|
||||
priority?: number
|
||||
|
||||
// Allow any additional fields that users might have in their email collection
|
||||
|
||||
@@ -83,12 +83,39 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
if (emailData.to) {
|
||||
emailData.to = parseAndValidateEmails(emailData.to as string | string[])
|
||||
}
|
||||
if (emailData.cc) {
|
||||
if (emailData.cc && emailData.cc !== null) {
|
||||
emailData.cc = parseAndValidateEmails(emailData.cc as string | string[])
|
||||
}
|
||||
if (emailData.bcc) {
|
||||
if (emailData.bcc && emailData.bcc !== null) {
|
||||
emailData.bcc = parseAndValidateEmails(emailData.bcc as string | string[])
|
||||
}
|
||||
if (emailData.replyTo && emailData.replyTo !== null) {
|
||||
const validated = parseAndValidateEmails(emailData.replyTo as string | string[])
|
||||
// replyTo should be a single email, so take the first one if array
|
||||
emailData.replyTo = validated && validated.length > 0 ? validated[0] : undefined
|
||||
}
|
||||
if (emailData.from && emailData.from !== null) {
|
||||
const validated = parseAndValidateEmails(emailData.from as string | string[])
|
||||
// from should be a single email, so take the first one if array
|
||||
emailData.from = validated && validated.length > 0 ? validated[0] : undefined
|
||||
}
|
||||
|
||||
// Normalize Date objects to ISO strings for consistent database storage
|
||||
if (emailData.scheduledAt instanceof Date) {
|
||||
emailData.scheduledAt = emailData.scheduledAt.toISOString()
|
||||
}
|
||||
if (emailData.sentAt instanceof Date) {
|
||||
emailData.sentAt = emailData.sentAt.toISOString()
|
||||
}
|
||||
if (emailData.lastAttemptAt instanceof Date) {
|
||||
emailData.lastAttemptAt = emailData.lastAttemptAt.toISOString()
|
||||
}
|
||||
if (emailData.createdAt instanceof Date) {
|
||||
emailData.createdAt = emailData.createdAt.toISOString()
|
||||
}
|
||||
if (emailData.updatedAt instanceof Date) {
|
||||
emailData.updatedAt = emailData.updatedAt.toISOString()
|
||||
}
|
||||
|
||||
// Create the email in the collection with proper typing
|
||||
const email = await payload.create({
|
||||
|
||||
@@ -2,38 +2,41 @@ import { Payload } from 'payload'
|
||||
import type { CollectionConfig, RichTextField } from 'payload'
|
||||
import { Transporter } from 'nodemailer'
|
||||
|
||||
// Generic base interfaces that work with any ID type
|
||||
// JSON value type that matches Payload's JSON field type
|
||||
export type JSONValue = string | number | boolean | { [k: string]: unknown } | unknown[] | null | undefined
|
||||
|
||||
// Generic base interfaces that work with any ID type and null values
|
||||
export interface BaseEmailDocument {
|
||||
id: string | number
|
||||
template?: any
|
||||
to: string[]
|
||||
cc?: string[]
|
||||
bcc?: string[]
|
||||
from?: string
|
||||
replyTo?: string
|
||||
cc?: string[] | null
|
||||
bcc?: string[] | null
|
||||
from?: string | null
|
||||
replyTo?: string | null
|
||||
subject: string
|
||||
html: string
|
||||
text?: string
|
||||
variables?: Record<string, any>
|
||||
scheduledAt?: string
|
||||
sentAt?: string
|
||||
status?: 'pending' | 'processing' | 'sent' | 'failed'
|
||||
attempts?: number
|
||||
lastAttemptAt?: string
|
||||
error?: string
|
||||
priority?: number
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
text?: string | null
|
||||
variables?: JSONValue
|
||||
scheduledAt?: string | Date | null
|
||||
sentAt?: string | Date | null
|
||||
status?: 'pending' | 'processing' | 'sent' | 'failed' | null
|
||||
attempts?: number | null
|
||||
lastAttemptAt?: string | Date | null
|
||||
error?: string | null
|
||||
priority?: number | null
|
||||
createdAt?: string | Date | null
|
||||
updatedAt?: string | Date | null
|
||||
}
|
||||
|
||||
export interface BaseEmailTemplateDocument {
|
||||
id: string | number
|
||||
name: string
|
||||
slug: string
|
||||
subject?: string
|
||||
subject?: string | null
|
||||
content?: any
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
createdAt?: string | Date | null
|
||||
updatedAt?: string | Date | null
|
||||
}
|
||||
|
||||
export type BaseEmail<TEmail extends BaseEmailDocument = BaseEmailDocument, TEmailTemplate extends BaseEmailTemplateDocument = BaseEmailTemplateDocument> = Omit<TEmail, 'id' | 'template'> & {template: Omit<TEmailTemplate, 'id'> | TEmailTemplate['id'] | undefined | null}
|
||||
@@ -75,23 +78,23 @@ export interface MailingTransportConfig {
|
||||
|
||||
export interface QueuedEmail {
|
||||
id: string
|
||||
template?: string
|
||||
template?: string | null
|
||||
to: string[]
|
||||
cc?: string[]
|
||||
bcc?: string[]
|
||||
from?: string
|
||||
replyTo?: string
|
||||
cc?: string[] | null
|
||||
bcc?: string[] | null
|
||||
from?: string | null
|
||||
replyTo?: string | null
|
||||
subject: string
|
||||
html: string
|
||||
text?: string
|
||||
variables?: Record<string, any>
|
||||
scheduledAt?: string
|
||||
sentAt?: string
|
||||
text?: string | null
|
||||
variables?: JSONValue
|
||||
scheduledAt?: string | Date | null
|
||||
sentAt?: string | Date | null
|
||||
status: 'pending' | 'processing' | 'sent' | 'failed'
|
||||
attempts: number
|
||||
lastAttemptAt?: string
|
||||
error?: string
|
||||
priority?: number
|
||||
lastAttemptAt?: string | Date | null
|
||||
error?: string | null
|
||||
priority?: number | null
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user