10 Commits

Author SHA1 Message Date
c46b58f43e 0.0.31 2025-09-09 11:11:40 +02:00
398a2d160e HOTFIX: Fix duplicate collectionSlug field error
- Multiple step types (create-document, read-document, etc.) were defining collectionSlug fields
- These created duplicate field names at the same level in the Workflow collection
- Fixed by prefixing step field names with step slug (__step_{stepSlug}_{fieldName})
- Added virtual field hooks to store/retrieve data using original field names
- Resolves DuplicateFieldName error preventing PayloadCMS initialization

Fixes: #duplicate-field-name-issue
Closes: User bug report for @xtr-dev/payload-automation@0.0.30
2025-09-09 11:11:31 +02:00
96b36a3caa 0.0.30 2025-09-09 10:30:38 +02:00
71ecca8253 Fix component import paths to use package imports
- Change component paths from relative to @xtr-dev/payload-automation/client#Component
- Use proper PayloadCMS plugin import syntax for components
- Regenerate import map with correct package-based imports
- Resolves 'Module not found' errors in dev project
2025-09-09 10:30:29 +02:00
8eedaba9ed 0.0.29 2025-09-09 10:13:04 +02:00
2bc01f30f8 Fix TypeScript and ESLint errors, resolve component imports
- Fix TypeScript types in trigger-helpers with proper interfaces
- Remove all ESLint no-explicit-any warnings with better typing
- Fix component import paths from @/components/* to relative paths
- Regenerate import map with correct component references
- All compilation and linting errors resolved
2025-09-09 10:13:00 +02:00
3e9ff10076 0.0.28 2025-09-08 20:54:58 +02:00
e204d1241a Refactor trigger helpers to single simplified function
- Replace multiple helper functions with single createTriggerField function
- createTriggerField takes a standard PayloadCMS field and adds virtual storage hooks
- Simplify trigger presets to use the new createTrigger helper
- Update exports to match new simplified API
- Cleaner, more maintainable code with less boilerplate
2025-09-08 20:54:49 +02:00
0fb23cb425 0.0.27 2025-09-08 20:46:15 +02:00
45c5847f5a Fix duplicate field name issue in custom triggers
- Remove redundant field name prefixing in Workflow.ts
- Custom trigger fields from trigger-helpers already have unique names
- Simplify by passing through inputs without modification
- Fixes DuplicateFieldName error
2025-09-08 20:46:11 +02:00
11 changed files with 578 additions and 335 deletions

View File

@@ -1,5 +1,9 @@
import { WorkflowExecutionStatus as WorkflowExecutionStatus_6f365a93b6cb4b34ad564b391e21db6f } from '@xtr-dev/payload-automation/client'
import { StatusCell as StatusCell_6f365a93b6cb4b34ad564b391e21db6f } from '@xtr-dev/payload-automation/client'
import { ErrorDisplay as ErrorDisplay_6f365a93b6cb4b34ad564b391e21db6f } from '@xtr-dev/payload-automation/client'
export const importMap = {
"@xtr-dev/payload-automation/client#WorkflowExecutionStatus": WorkflowExecutionStatus_6f365a93b6cb4b34ad564b391e21db6f,
"@xtr-dev/payload-automation/client#StatusCell": StatusCell_6f365a93b6cb4b34ad564b391e21db6f,
"@xtr-dev/payload-automation/client#ErrorDisplay": ErrorDisplay_6f365a93b6cb4b34ad564b391e21db6f
}

View File

@@ -92,7 +92,7 @@ export interface Config {
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
};
db: {
defaultIDType: number;
defaultIDType: string;
};
globals: {};
globalsSelect: {};
@@ -136,7 +136,7 @@ export interface UserAuthOperations {
* via the `definition` "posts".
*/
export interface Post {
id: number;
id: string;
content?: string | null;
updatedAt: string;
createdAt: string;
@@ -146,7 +146,7 @@ export interface Post {
* via the `definition` "media".
*/
export interface Media {
id: number;
id: string;
updatedAt: string;
createdAt: string;
url?: string | null;
@@ -164,9 +164,9 @@ export interface Media {
* via the `definition` "auditLog".
*/
export interface AuditLog {
id: number;
post?: (number | null) | Post;
user?: (number | null) | User;
id: string;
post?: (string | null) | Post;
user?: (string | null) | User;
message?: string | null;
updatedAt: string;
createdAt: string;
@@ -176,7 +176,7 @@ export interface AuditLog {
* via the `definition` "users".
*/
export interface User {
id: number;
id: string;
updatedAt: string;
createdAt: string;
email: string;
@@ -202,7 +202,7 @@ export interface User {
* via the `definition` "workflows".
*/
export interface Workflow {
id: number;
id: string;
/**
* Human-readable name for the workflow
*/
@@ -214,36 +214,45 @@ export interface Workflow {
triggers?:
| {
type?: ('collection-trigger' | 'webhook-trigger' | 'global-trigger' | 'cron-trigger') | null;
parameters?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
/**
* Collection that triggers the workflow
*/
collectionSlug?: ('posts' | 'media') | null;
__builtin_collectionSlug?: ('posts' | 'media') | null;
/**
* Collection operation that triggers the workflow
*/
operation?: ('create' | 'delete' | 'read' | 'update') | null;
__builtin_operation?: ('create' | 'delete' | 'read' | 'update') | null;
/**
* URL path for the webhook (e.g., "my-webhook"). Full URL will be /api/workflows/webhook/my-webhook
* URL path for the webhook (e.g., "my-webhook"). Full URL will be /api/workflows-webhook/my-webhook
*/
webhookPath?: string | null;
__builtin_webhookPath?: string | null;
/**
* Global that triggers the workflow
*/
global?: string | null;
__builtin_global?: string | null;
/**
* Global operation that triggers the workflow
*/
globalOperation?: 'update' | null;
__builtin_globalOperation?: 'update' | null;
/**
* Cron expression for scheduled execution (e.g., "0 0 * * *" for daily at midnight)
*/
cronExpression?: string | null;
__builtin_cronExpression?: string | null;
/**
* Timezone for cron execution (e.g., "America/New_York", "Europe/London"). Defaults to UTC.
*/
timezone?: string | null;
__builtin_timezone?: string | null;
/**
* JSONPath expression that must evaluate to true for this trigger to execute the workflow (e.g., "$.doc.status == 'published'")
* JSONPath expression that must evaluate to true for this trigger to execute the workflow (e.g., "$.trigger.doc.status == 'published'")
*/
condition?: string | null;
id?: string | null;
@@ -253,7 +262,18 @@ export interface Workflow {
| {
step?: ('http-request-step' | 'create-document') | null;
name?: string | null;
input?:
/**
* The URL to make the HTTP request to
*/
url?: string | null;
/**
* HTTP method to use
*/
method?: ('GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH') | null;
/**
* HTTP headers as JSON object (e.g., {"Content-Type": "application/json"})
*/
headers?:
| {
[k: string]: unknown;
}
@@ -262,6 +282,80 @@ export interface Workflow {
| number
| boolean
| null;
/**
* Request body data. Use JSONPath to reference values (e.g., {"postId": "$.trigger.doc.id", "title": "$.trigger.doc.title"})
*/
body?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
/**
* Request timeout in milliseconds (default: 30000)
*/
timeout?: number | null;
authentication?: {
/**
* Authentication method
*/
type?: ('none' | 'bearer' | 'basic' | 'apikey') | null;
/**
* Bearer token value
*/
token?: string | null;
/**
* Basic auth username
*/
username?: string | null;
/**
* Basic auth password
*/
password?: string | null;
/**
* API key header name (e.g., "X-API-Key")
*/
headerName?: string | null;
/**
* API key value
*/
headerValue?: string | null;
};
/**
* Number of retry attempts on failure (max: 5)
*/
retries?: number | null;
/**
* Delay between retries in milliseconds
*/
retryDelay?: number | null;
/**
* The collection slug to create a document in
*/
collectionSlug?: string | null;
/**
* The document data to create. Use JSONPath to reference trigger data (e.g., {"title": "$.trigger.doc.title", "author": "$.trigger.doc.author"})
*/
data?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
/**
* Create as draft (if collection has drafts enabled)
*/
draft?: boolean | null;
/**
* Locale for the document (if localization is enabled)
*/
locale?: string | null;
/**
* Step names that must complete before this step can run
*/
@@ -282,11 +376,11 @@ export interface Workflow {
* via the `definition` "workflow-runs".
*/
export interface WorkflowRun {
id: number;
id: string;
/**
* Reference to the workflow that was executed
*/
workflow: number | Workflow;
workflow: string | Workflow;
/**
* Version of the workflow that was executed
*/
@@ -380,7 +474,7 @@ export interface WorkflowRun {
* via the `definition` "payload-jobs".
*/
export interface PayloadJob {
id: number;
id: string;
/**
* Input data provided to the job
*/
@@ -472,40 +566,40 @@ export interface PayloadJob {
* via the `definition` "payload-locked-documents".
*/
export interface PayloadLockedDocument {
id: number;
id: string;
document?:
| ({
relationTo: 'posts';
value: number | Post;
value: string | Post;
} | null)
| ({
relationTo: 'media';
value: number | Media;
value: string | Media;
} | null)
| ({
relationTo: 'auditLog';
value: number | AuditLog;
value: string | AuditLog;
} | null)
| ({
relationTo: 'workflows';
value: number | Workflow;
value: string | Workflow;
} | null)
| ({
relationTo: 'workflow-runs';
value: number | WorkflowRun;
value: string | WorkflowRun;
} | null)
| ({
relationTo: 'users';
value: number | User;
value: string | User;
} | null)
| ({
relationTo: 'payload-jobs';
value: number | PayloadJob;
value: string | PayloadJob;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
value: number | User;
value: string | User;
};
updatedAt: string;
createdAt: string;
@@ -515,10 +609,10 @@ export interface PayloadLockedDocument {
* via the `definition` "payload-preferences".
*/
export interface PayloadPreference {
id: number;
id: string;
user: {
relationTo: 'users';
value: number | User;
value: string | User;
};
key?: string | null;
value?:
@@ -538,7 +632,7 @@ export interface PayloadPreference {
* via the `definition` "payload-migrations".
*/
export interface PayloadMigration {
id: number;
id: string;
name?: string | null;
batch?: number | null;
updatedAt: string;
@@ -592,13 +686,14 @@ export interface WorkflowsSelect<T extends boolean = true> {
| T
| {
type?: T;
collectionSlug?: T;
operation?: T;
webhookPath?: T;
global?: T;
globalOperation?: T;
cronExpression?: T;
timezone?: T;
parameters?: T;
__builtin_collectionSlug?: T;
__builtin_operation?: T;
__builtin_webhookPath?: T;
__builtin_global?: T;
__builtin_globalOperation?: T;
__builtin_cronExpression?: T;
__builtin_timezone?: T;
condition?: T;
id?: T;
};
@@ -607,7 +702,27 @@ export interface WorkflowsSelect<T extends boolean = true> {
| {
step?: T;
name?: T;
input?: T;
url?: T;
method?: T;
headers?: T;
body?: T;
timeout?: T;
authentication?:
| T
| {
type?: T;
token?: T;
username?: T;
password?: T;
headerName?: T;
headerValue?: T;
};
retries?: T;
retryDelay?: T;
collectionSlug?: T;
data?: T;
draft?: T;
locale?: T;
dependencies?: T;
condition?: T;
id?: T;
@@ -736,10 +851,118 @@ export interface TaskWorkflowCronExecutor {
*/
export interface TaskHttpRequestStep {
input: {
url?: string | null;
/**
* The URL to make the HTTP request to
*/
url: string;
/**
* HTTP method to use
*/
method?: ('GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH') | null;
/**
* HTTP headers as JSON object (e.g., {"Content-Type": "application/json"})
*/
headers?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
/**
* Request body data. Use JSONPath to reference values (e.g., {"postId": "$.trigger.doc.id", "title": "$.trigger.doc.title"})
*/
body?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
/**
* Request timeout in milliseconds (default: 30000)
*/
timeout?: number | null;
authentication?: {
/**
* Authentication method
*/
type?: ('none' | 'bearer' | 'basic' | 'apikey') | null;
/**
* Bearer token value
*/
token?: string | null;
/**
* Basic auth username
*/
username?: string | null;
/**
* Basic auth password
*/
password?: string | null;
/**
* API key header name (e.g., "X-API-Key")
*/
headerName?: string | null;
/**
* API key value
*/
headerValue?: string | null;
};
/**
* Number of retry attempts on failure (max: 5)
*/
retries?: number | null;
/**
* Delay between retries in milliseconds
*/
retryDelay?: number | null;
};
output: {
response?: string | null;
/**
* HTTP status code
*/
status?: number | null;
/**
* HTTP status text
*/
statusText?: string | null;
/**
* Response headers
*/
headers?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
/**
* Response body
*/
body?: string | null;
/**
* Parsed response data (if JSON)
*/
data?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
/**
* Request duration in milliseconds
*/
duration?: number | null;
};
}
/**
@@ -753,7 +976,7 @@ export interface TaskCreateDocument {
*/
collectionSlug: string;
/**
* The document data to create
* The document data to create. Use JSONPath to reference trigger data (e.g., {"title": "$.trigger.doc.title", "author": "$.trigger.doc.author"})
*/
data:
| {

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@xtr-dev/payload-workflows",
"version": "0.0.26",
"version": "0.0.31",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@xtr-dev/payload-workflows",
"version": "0.0.26",
"version": "0.0.31",
"license": "MIT",
"dependencies": {
"jsonpath-plus": "^10.3.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@xtr-dev/payload-automation",
"version": "0.0.26",
"version": "0.0.31",
"description": "PayloadCMS Automation Plugin - Comprehensive workflow automation system with visual workflow building, execution tracking, and step types",
"license": "MIT",
"type": "module",

View File

@@ -41,7 +41,7 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
type: 'ui',
admin: {
components: {
Field: '@/components/WorkflowExecutionStatus'
Field: '@xtr-dev/payload-automation/client#WorkflowExecutionStatus'
},
condition: (data) => !!data?.id // Only show for existing workflows
}
@@ -296,34 +296,9 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
required: false
},
// Virtual fields for custom triggers
...(triggers || []).flatMap(t => (t.inputs || []).filter(f => 'name' in f && f.name).map(f => ({
...f,
// Prefix field name with trigger slug to avoid conflicts
name: `__${t.slug}_${(f as any).name}`,
admin: {
...(f.admin || {}),
condition: (...args) => args[1]?.type === t.slug && (
f.admin?.condition ?
f.admin.condition.call(this, ...args) :
true
),
},
hooks: {
afterRead: [
({ siblingData }) => {
return siblingData?.parameters?.[(f as any).name] || undefined
}
],
beforeChange: [
({ siblingData, value }) => {
if (!siblingData.parameters) {siblingData.parameters = {}}
siblingData.parameters[(f as any).name] = value
return undefined // Virtual field, don't store directly
}
]
},
virtual: true,
} as Field)))
// Note: Custom trigger fields from trigger-helpers already have unique names
// We just need to pass them through without modification
...(triggers || []).flatMap(t => (t.inputs || []))
]
},
{
@@ -344,17 +319,45 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
}
]
},
...(steps || []).flatMap(step => (step.inputSchema || []).map(field => ({
...field,
admin: {
...(field.admin || {}),
condition: (...args) => args[1]?.step === step.slug && (
field.admin?.condition ?
field.admin.condition.call(this, ...args) :
true
),
},
} as Field))),
...(steps || []).flatMap(step => (step.inputSchema || []).map(field => {
const originalName = (field as any).name;
const resultField: any = {
...field,
// Prefix field name with step slug to avoid conflicts
name: `__step_${step.slug}_${originalName}`,
admin: {
...(field.admin || {}),
condition: (...args: any[]) => args[1]?.step === step.slug && (
(field.admin as any)?.condition ?
(field.admin as any).condition.call(this, ...args) :
true
),
},
virtual: true,
};
// Add hooks to store/retrieve from the step's input data
resultField.hooks = {
...((field as any).hooks || {}),
afterRead: [
...(((field as any).hooks)?.afterRead || []),
({ siblingData }: any) => {
// Read from step input data using original field name
return siblingData?.[originalName] || (field as any).defaultValue;
}
],
beforeChange: [
...(((field as any).hooks)?.beforeChange || []),
({ siblingData, value }: any) => {
// Store in step data using original field name
siblingData[originalName] = value;
return undefined; // Don't store the prefixed field
}
]
};
return resultField as Field;
})),
{
name: 'dependencies',
type: 'text',

View File

@@ -40,7 +40,7 @@ export const WorkflowRunsCollection: CollectionConfig = {
admin: {
description: 'Current execution status',
components: {
Cell: '@/components/StatusCell'
Cell: '@xtr-dev/payload-automation/client#StatusCell'
}
},
defaultValue: 'pending',
@@ -141,7 +141,7 @@ export const WorkflowRunsCollection: CollectionConfig = {
description: 'Error message if workflow execution failed',
condition: (_, siblingData) => siblingData?.status === 'failed',
components: {
Field: '@/components/ErrorDisplay'
Field: '@xtr-dev/payload-automation/client#ErrorDisplay'
}
},
},

View File

@@ -166,14 +166,15 @@ export const ErrorDisplay: React.FC<ErrorDisplayProps> = ({
{/* Technical Details Toggle */}
<div>
<Button
onClick={() => setExpanded(!expanded)}
size="small"
buttonStyle="secondary"
style={{ marginBottom: expanded ? '12px' : '0' }}
>
{expanded ? 'Hide' : 'Show'} Technical Details
</Button>
<div style={{ marginBottom: expanded ? '12px' : '0' }}>
<Button
onClick={() => setExpanded(!expanded)}
size="small"
buttonStyle="secondary"
>
{expanded ? 'Hide' : 'Show'} Technical Details
</Button>
</div>
{expanded && (
<div style={{

View File

@@ -3,7 +3,7 @@
export { TriggerWorkflowButton } from '../components/TriggerWorkflowButton.js'
export { StatusCell } from '../components/StatusCell.js'
// export { ErrorDisplay } from '../components/ErrorDisplay.js' // Temporarily disabled
export { ErrorDisplay } from '../components/ErrorDisplay.js'
export { WorkflowExecutionStatus } from '../components/WorkflowExecutionStatus.js'
// Future client components can be added here:

View File

@@ -3,31 +3,29 @@
*
* @example
* ```typescript
* import { createTrigger, webhookTrigger } from '@xtr-dev/payload-automation/helpers'
* import { createTrigger, createTriggerField, webhookTrigger } from '@xtr-dev/payload-automation/helpers'
*
* // Simple trigger
* const myTrigger = createTrigger('my-trigger').parameters({
* apiKey: { type: 'text', required: true },
* timeout: { type: 'number', defaultValue: 30 }
* })
* // Simple trigger with array of fields
* const myTrigger = createTrigger('my-trigger', [
* { name: 'apiKey', type: 'text', required: true },
* { name: 'timeout', type: 'number', defaultValue: 30 }
* ])
*
* // Webhook trigger with presets
* // Single field with virtual storage
* const field = createTriggerField(
* { name: 'webhookUrl', type: 'text', required: true },
* 'my-trigger'
* )
*
* // Webhook trigger preset
* const orderWebhook = webhookTrigger('order-webhook')
* .parameter('orderTypes', {
* type: 'select',
* hasMany: true,
* options: ['regular', 'subscription']
* })
* .build()
* ```
*/
// Core helpers
export {
createTriggerParameter,
createTriggerParameters,
createTrigger,
createAdvancedTrigger
createTriggerField,
createTrigger
} from '../utils/trigger-helpers.js'
// Preset builders
@@ -37,11 +35,4 @@ export {
eventTrigger,
manualTrigger,
apiTrigger
} from '../utils/trigger-presets.js'
// Common parameter sets for extending
export {
webhookParameters,
cronParameters,
eventParameters
} from '../utils/trigger-presets.js'

View File

@@ -1,138 +1,158 @@
import type { Field } from 'payload'
import type { CustomTriggerConfig } from '../plugin/config-types.js'
// Types for better type safety
interface FieldWithName {
name: string
[key: string]: unknown
}
interface HookContext {
siblingData: Record<string, unknown>
value?: unknown
}
interface ValidationContext {
siblingData: Record<string, unknown>
}
/**
* Helper function to create a virtual trigger parameter field
* Handles the boilerplate for storing/reading from the parameters JSON field
* Creates a virtual field for a trigger parameter that stores its value in the parameters JSON field
*
* @param field - Standard PayloadCMS field configuration (must be a data field with a name)
* @param triggerSlug - The slug of the trigger this field belongs to
* @returns Modified field with virtual storage hooks and proper naming
*
* @example
* ```typescript
* const myTrigger: CustomTriggerConfig = {
* slug: 'my-trigger',
* inputs: [
* createTriggerField({
* name: 'webhookUrl',
* type: 'text',
* required: true,
* admin: {
* description: 'URL to call when triggered'
* }
* }, 'my-trigger')
* ]
* }
* ```
*/
export function createTriggerParameter(
name: string,
fieldConfig: any, // Use any to allow flexible field configurations
triggerSlug: string
): Field {
export function createTriggerField(field: FieldWithName, triggerSlug: string): Field {
const originalName = field.name
if (!originalName) {
throw new Error('Field must have a name property')
}
// Create a unique field name by prefixing with trigger slug
const uniqueFieldName = `__trigger_${triggerSlug}_${name}`
const uniqueFieldName = `__trigger_${triggerSlug}_${originalName}`
return {
...fieldConfig,
name: uniqueFieldName,
virtual: true,
const resultField: Record<string, unknown> = {
...field,
admin: {
...fieldConfig.admin,
condition: (_, siblingData) => siblingData?.type === triggerSlug && (
fieldConfig.admin?.condition ?
fieldConfig.admin.condition(_, siblingData) :
true
)
...(field.admin as Record<string, unknown> || {}),
condition: (data: unknown, siblingData: Record<string, unknown>) => {
// Only show this field when the trigger type matches
const triggerMatches = siblingData?.type === triggerSlug
// If the original field had a condition, combine it with our trigger condition
const originalCondition = (field.admin as Record<string, unknown>)?.condition
if (originalCondition && typeof originalCondition === 'function') {
return triggerMatches && (originalCondition as (data: unknown, siblingData: Record<string, unknown>) => boolean)(data, siblingData)
}
return triggerMatches
}
},
hooks: {
...fieldConfig.hooks,
...(field.hooks as Record<string, unknown[]> || {}),
afterRead: [
...(fieldConfig.hooks?.afterRead || []),
({ siblingData }) => siblingData?.parameters?.[name] || fieldConfig.defaultValue
...((field.hooks as Record<string, unknown[]>)?.afterRead || []),
({ siblingData }: HookContext) => {
// Read the value from the parameters JSON field
const parameters = siblingData?.parameters as Record<string, unknown>
return parameters?.[originalName] ?? (field as Record<string, unknown>).defaultValue
}
],
beforeChange: [
...(fieldConfig.hooks?.beforeChange || []),
({ value, siblingData }) => {
if (!siblingData.parameters) siblingData.parameters = {}
siblingData.parameters[name] = value
...((field.hooks as Record<string, unknown[]>)?.beforeChange || []),
({ siblingData, value }: HookContext) => {
// Store the value in the parameters JSON field
if (!siblingData.parameters) {
siblingData.parameters = {}
}
const parameters = siblingData.parameters as Record<string, unknown>
parameters[originalName] = value
return undefined // Virtual field, don't store directly
}
]
},
validate: fieldConfig.validate || fieldConfig.required ?
(value: any, args: any) => {
const paramValue = value ?? args.siblingData?.parameters?.[name]
// Check required
if (fieldConfig.required && args.siblingData?.type === triggerSlug && !paramValue) {
return `${fieldConfig.admin?.description || name} is required for ${triggerSlug}`
}
// Run original validation if present
return fieldConfig.validate?.(paramValue, args) ?? true
} :
undefined
} as Field
name: uniqueFieldName,
virtual: true,
}
// Only add validate if the field supports it (data fields)
const hasValidation = (field as Record<string, unknown>).validate || (field as Record<string, unknown>).required
if (hasValidation) {
resultField.validate = (value: unknown, args: ValidationContext) => {
const parameters = args.siblingData?.parameters as Record<string, unknown>
const paramValue = value ?? parameters?.[originalName]
// Check required validation
const isRequired = (field as Record<string, unknown>).required
if (isRequired && args.siblingData?.type === triggerSlug && !paramValue) {
const fieldLabel = (field as Record<string, unknown>).label as string
const adminDesc = ((field as Record<string, unknown>).admin as Record<string, unknown>)?.description as string
const label = fieldLabel || adminDesc || originalName
return `${label} is required for ${triggerSlug}`
}
// Run original validation if present
const originalValidate = (field as Record<string, unknown>).validate
if (originalValidate && typeof originalValidate === 'function') {
return (originalValidate as (value: unknown, args: ValidationContext) => boolean | string)(paramValue, args)
}
return true
}
}
return resultField as Field
}
/**
* Helper to create multiple trigger parameter fields at once
* Creates a custom trigger configuration with the provided fields
*
* @param slug - Unique identifier for the trigger
* @param fields - Array of PayloadCMS fields that will be shown as trigger parameters
* @returns Complete trigger configuration
*
* @example
* ```typescript
* const webhookTrigger = createTrigger('webhook', [
* {
* name: 'url',
* type: 'text',
* required: true,
* admin: {
* description: 'Webhook URL'
* }
* },
* {
* name: 'method',
* type: 'select',
* options: ['GET', 'POST', 'PUT', 'DELETE'],
* defaultValue: 'POST'
* }
* ])
* ```
*/
export function createTriggerParameters(
triggerSlug: string,
parameters: Record<string, any>
): Field[] {
return Object.entries(parameters).map(([name, fieldConfig]) =>
createTriggerParameter(name, fieldConfig, triggerSlug)
)
}
/**
* Main trigger builder function that creates a fluent API for defining triggers
*/
export function createTrigger<TSlug extends string>(slug: TSlug) {
export function createTrigger(slug: string, fields: FieldWithName[]): CustomTriggerConfig {
return {
/**
* Define parameters for this trigger using a clean object syntax
* @param paramConfig - Object where keys are parameter names and values are Field configs
* @returns Complete CustomTriggerConfig ready for use
*/
parameters(paramConfig: Record<string, any>): CustomTriggerConfig {
return {
slug,
inputs: Object.entries(paramConfig).map(([name, fieldConfig]) =>
createTriggerParameter(name, fieldConfig, slug)
)
}
}
}
}
/**
* Advanced trigger builder with chainable methods for more complex scenarios
*/
export function createAdvancedTrigger<TSlug extends string>(slug: TSlug) {
const builder = {
slug,
_parameters: {} as Record<string, any>,
/**
* Set all parameters at once
*/
parameters(paramConfig: Record<string, any>) {
this._parameters = paramConfig
return this
},
/**
* Add a single parameter
*/
parameter(name: string, fieldConfig: any) {
this._parameters[name] = fieldConfig
return this
},
/**
* Extend with existing parameter sets (useful for common patterns)
*/
extend(baseParameters: Record<string, any>) {
this._parameters = { ...baseParameters, ...this._parameters }
return this
},
/**
* Build the final trigger configuration
*/
build(): CustomTriggerConfig {
return {
slug: this.slug,
inputs: Object.entries(this._parameters).map(([name, fieldConfig]) =>
createTriggerParameter(name, fieldConfig, this.slug)
)
}
}
inputs: fields.map(field => createTriggerField(field, slug))
}
return builder
}

View File

@@ -1,88 +1,5 @@
import { createAdvancedTrigger } from './trigger-helpers.js'
/**
* Common parameter sets for reuse across different triggers
*/
export const webhookParameters: Record<string, any> = {
path: {
type: 'text',
required: true,
admin: {
description: 'URL path for the webhook endpoint (e.g., "my-webhook")'
},
validate: (value: any) => {
if (typeof value === 'string' && value.includes(' ')) {
return 'Webhook path cannot contain spaces'
}
return true
}
},
secret: {
type: 'text',
admin: {
description: 'Secret key for webhook signature validation (optional but recommended)'
}
},
headers: {
type: 'json',
admin: {
description: 'Expected HTTP headers for validation (JSON object)'
}
}
}
export const cronParameters: Record<string, any> = {
expression: {
type: 'text',
required: true,
admin: {
description: 'Cron expression for scheduling (e.g., "0 9 * * 1" for every Monday at 9 AM)',
placeholder: '0 9 * * 1'
}
},
timezone: {
type: 'text',
defaultValue: 'UTC',
admin: {
description: 'Timezone for cron execution (e.g., "America/New_York", "Europe/London")',
placeholder: 'UTC'
},
validate: (value: any) => {
if (value) {
try {
new Intl.DateTimeFormat('en', { timeZone: value as string })
return true
} catch {
return `Invalid timezone: ${value}. Please use a valid IANA timezone identifier`
}
}
return true
}
}
}
export const eventParameters: Record<string, any> = {
eventTypes: {
type: 'select',
hasMany: true,
options: [
{ label: 'User Created', value: 'user.created' },
{ label: 'User Updated', value: 'user.updated' },
{ label: 'Document Published', value: 'document.published' },
{ label: 'Payment Completed', value: 'payment.completed' }
],
admin: {
description: 'Event types that should trigger this workflow'
}
},
filters: {
type: 'json',
admin: {
description: 'JSON filters to apply to event data (e.g., {"status": "active"})'
}
}
}
import { createTrigger } from './trigger-helpers.js'
import type { CustomTriggerConfig } from '../plugin/config-types.js'
/**
* Preset trigger builders for common patterns
@@ -91,28 +8,109 @@ export const eventParameters: Record<string, any> = {
/**
* Create a webhook trigger with common webhook parameters pre-configured
*/
export function webhookTrigger<TSlug extends string>(slug: TSlug) {
return createAdvancedTrigger(slug).extend(webhookParameters)
export function webhookTrigger(slug: string): CustomTriggerConfig {
return createTrigger(slug, [
{
name: 'path',
type: 'text',
required: true,
admin: {
description: 'URL path for the webhook endpoint (e.g., "my-webhook")'
},
validate: (value: any) => {
if (typeof value === 'string' && value.includes(' ')) {
return 'Webhook path cannot contain spaces'
}
return true
}
},
{
name: 'secret',
type: 'text',
admin: {
description: 'Secret key for webhook signature validation (optional but recommended)'
}
},
{
name: 'headers',
type: 'json',
admin: {
description: 'Expected HTTP headers for validation (JSON object)'
}
}
])
}
/**
* Create a scheduled/cron trigger with timing parameters pre-configured
*/
export function cronTrigger<TSlug extends string>(slug: TSlug) {
return createAdvancedTrigger(slug).extend(cronParameters)
export function cronTrigger(slug: string): CustomTriggerConfig {
return createTrigger(slug, [
{
name: 'expression',
type: 'text',
required: true,
admin: {
description: 'Cron expression for scheduling (e.g., "0 9 * * 1" for every Monday at 9 AM)',
placeholder: '0 9 * * 1'
}
},
{
name: 'timezone',
type: 'text',
defaultValue: 'UTC',
admin: {
description: 'Timezone for cron execution (e.g., "America/New_York", "Europe/London")',
placeholder: 'UTC'
},
validate: (value: any) => {
if (value) {
try {
new Intl.DateTimeFormat('en', { timeZone: value as string })
return true
} catch {
return `Invalid timezone: ${value}. Please use a valid IANA timezone identifier`
}
}
return true
}
}
])
}
/**
* Create an event-driven trigger with event filtering parameters
*/
export function eventTrigger<TSlug extends string>(slug: TSlug) {
return createAdvancedTrigger(slug).extend(eventParameters)
export function eventTrigger(slug: string): CustomTriggerConfig {
return createTrigger(slug, [
{
name: 'eventTypes',
type: 'select',
hasMany: true,
options: [
{ label: 'User Created', value: 'user.created' },
{ label: 'User Updated', value: 'user.updated' },
{ label: 'Document Published', value: 'document.published' },
{ label: 'Payment Completed', value: 'payment.completed' }
],
admin: {
description: 'Event types that should trigger this workflow'
}
},
{
name: 'filters',
type: 'json',
admin: {
description: 'JSON filters to apply to event data (e.g., {"status": "active"})'
}
}
])
}
/**
* Create a simple manual trigger (no parameters needed)
*/
export function manualTrigger<TSlug extends string>(slug: TSlug) {
export function manualTrigger(slug: string): CustomTriggerConfig {
return {
slug,
inputs: []
@@ -122,16 +120,18 @@ export function manualTrigger<TSlug extends string>(slug: TSlug) {
/**
* Create an API trigger for external systems to call
*/
export function apiTrigger<TSlug extends string>(slug: TSlug) {
return createAdvancedTrigger(slug).extend({
endpoint: {
export function apiTrigger(slug: string): CustomTriggerConfig {
return createTrigger(slug, [
{
name: 'endpoint',
type: 'text',
required: true,
admin: {
description: 'API endpoint path (e.g., "/api/triggers/my-trigger")'
}
},
method: {
{
name: 'method',
type: 'select',
options: ['GET', 'POST', 'PUT', 'PATCH'],
defaultValue: 'POST',
@@ -139,7 +139,8 @@ export function apiTrigger<TSlug extends string>(slug: TSlug) {
description: 'HTTP method for the API endpoint'
}
},
authentication: {
{
name: 'authentication',
type: 'select',
options: [
{ label: 'None', value: 'none' },
@@ -152,5 +153,5 @@ export function apiTrigger<TSlug extends string>(slug: TSlug) {
description: 'Authentication method for the API endpoint'
}
}
})
])
}