mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-12 01:33:23 +00:00
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
This commit is contained in:
@@ -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'
|
||||
}
|
||||
}
|
||||
})
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user