Compare commits

..

5 Commits

Author SHA1 Message Date
Bas
768b70a003 Merge pull request #27 from xtr-dev/dev
Align `sendEmail` and `sendEmailTask` with updated `BaseEmail` typing
2025-09-13 22:49:05 +02:00
e91ab7e54e Bump package version to 0.1.11 in package.json. 2025-09-13 22:48:55 +02:00
06f9c2cb5b Align sendEmail and sendEmailTask with updated BaseEmail typing
- Refactor `sendEmail` to return extended type with `id` for better type inference.
- Update `sendEmailTask` to use `BaseEmail` instead of `Email`.
- Add `outputSchema` in `sendEmailTask` for consistent output structure.
2025-09-13 22:46:30 +02:00
Bas
21b22a033a Merge pull request #26 from xtr-dev/dev
Refactor `sendEmail` to improve type safety and align with `BaseEmail…
2025-09-13 22:41:28 +02:00
6ad90874cf Refactor sendEmail to improve type safety and align with BaseEmail interface
- Replace `Email` with `BaseEmail` for stricter type validation.
- Update `SendEmailOptions` and `sendEmail` typing for improved extensibility.
2025-09-13 22:39:28 +02:00
3 changed files with 16 additions and 14 deletions

View File

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

View File

@@ -1,5 +1,6 @@
import { sendEmail } from '../sendEmail.js'
import { Email } from '../payload-types.js'
import {BaseEmail} from "../types/index.js"
export interface SendEmailTaskInput {
// Template mode fields
@@ -116,6 +117,12 @@ export const sendEmailJob = {
}
}
],
outputSchema: [
{
name: 'id',
type: 'text' as const
}
],
handler: async ({ input, payload }: any) => {
// Cast input to our expected type
const taskInput = input as SendEmailTaskInput
@@ -154,18 +161,12 @@ export const sendEmailJob = {
})
// Use the sendEmail helper to create the email
const email = await sendEmail<Email>(payload, sendEmailOptions)
const email = await sendEmail<BaseEmail>(payload, sendEmailOptions)
return {
output: {
success: true,
emailId: email.id,
message: `Email queued successfully with ID: ${email.id}`,
mode: taskInput.templateSlug ? 'template' : 'direct',
templateSlug: taskInput.templateSlug || null,
subject: email.subject,
recipients: Array.isArray(email.to) ? email.to.length : 1,
scheduledAt: email.scheduledAt || null
id: email.id,
}
}
@@ -176,4 +177,4 @@ export const sendEmailJob = {
}
}
export default sendEmailJob
export default sendEmailJob

View File

@@ -1,9 +1,10 @@
import { Payload } from 'payload'
import { getMailing, renderTemplate, parseAndValidateEmails } from './utils/helpers.js'
import {Email} from "./payload-types.js"
import {BaseEmail} from "./types/index.js"
// Options for sending emails
export interface SendEmailOptions<T extends Email = Email> {
export interface SendEmailOptions<T extends BaseEmail = BaseEmail> {
// Template-based email
template?: {
slug: string
@@ -35,10 +36,10 @@ export interface SendEmailOptions<T extends Email = Email> {
* })
* ```
*/
export const sendEmail = async <T extends Email = Email>(
export const sendEmail = async <T extends BaseEmail = BaseEmail, ID = string | number>(
payload: Payload,
options: SendEmailOptions<T>
): Promise<T> => {
): Promise<T & {id: ID}> => {
const mailing = getMailing(payload)
const collectionSlug = options.collectionSlug || mailing.collections.emails || 'emails'
@@ -96,7 +97,7 @@ export const sendEmail = async <T extends Email = Email>(
data: emailData
})
return email as T
return email as T & {id: ID}
}
export default sendEmail