mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-10 17:03:22 +00:00
WIP: Refactor triggers to TriggerConfig pattern
- Convert webhook, global, and cron triggers to use TriggerConfig pattern like collectionTrigger - Simplify trigger slug names (remove '-trigger' suffix) - Update validation to use new slug names - Add perfectionist/sort-exports rule disable - Note: Workflow.ts integration still needs fixes for type compatibility
This commit is contained in:
@@ -30,6 +30,7 @@ export default [
|
|||||||
'no-console': 'off',
|
'no-console': 'off',
|
||||||
'perfectionist/sort-object-types': 'off',
|
'perfectionist/sort-object-types': 'off',
|
||||||
'perfectionist/sort-objects': 'off',
|
'perfectionist/sort-objects': 'off',
|
||||||
|
'perfectionist/sort-exports': 'off',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
import type {CollectionConfig, Field} from 'payload'
|
import type {CollectionConfig, Field} from 'payload'
|
||||||
|
|
||||||
import type {WorkflowsPluginConfig} from "../plugin/config-types.js"
|
import type {WorkflowsPluginConfig} from "../plugin/config-types.js"
|
||||||
import {
|
|
||||||
getCollectionTriggerFields,
|
|
||||||
getCronTriggerFields,
|
|
||||||
getGlobalTriggerFields,
|
|
||||||
getWebhookTriggerFields
|
|
||||||
} from '../triggers/index.js'
|
|
||||||
|
|
||||||
export const createWorkflowCollection: <T extends string>(options: WorkflowsPluginConfig<T>) => CollectionConfig = ({
|
import {
|
||||||
collectionTriggers,
|
collectionTrigger,
|
||||||
steps,
|
cronTrigger,
|
||||||
triggers
|
globalTrigger,
|
||||||
}) => ({
|
webhookTrigger
|
||||||
|
} from '../triggers/index.js'
|
||||||
|
import {trigger} from '../triggers/helpers.js'
|
||||||
|
|
||||||
|
export const createWorkflowCollection: <T extends string>(options: WorkflowsPluginConfig<T>) => CollectionConfig = (options) => {
|
||||||
|
const {steps, collectionTriggers} = options
|
||||||
|
const triggers = (options.triggers || []).map(t => t(options))
|
||||||
|
return {
|
||||||
slug: 'workflows',
|
slug: 'workflows',
|
||||||
access: {
|
access: {
|
||||||
create: () => true,
|
create: () => true,
|
||||||
@@ -60,10 +61,6 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
|
|||||||
name: 'type',
|
name: 'type',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
options: [
|
options: [
|
||||||
'collection-trigger',
|
|
||||||
'webhook-trigger',
|
|
||||||
'global-trigger',
|
|
||||||
'cron-trigger',
|
|
||||||
...(triggers || []).map(t => t.slug)
|
...(triggers || []).map(t => t.slug)
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -76,10 +73,10 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
|
|||||||
defaultValue: {}
|
defaultValue: {}
|
||||||
},
|
},
|
||||||
// Virtual fields for built-in triggers
|
// Virtual fields for built-in triggers
|
||||||
...getCollectionTriggerFields(collectionTriggers),
|
...trigger({slug: 'collection', fields: collectionTrigger(options).fields}).fields,
|
||||||
...getWebhookTriggerFields(),
|
...trigger({slug: 'webhook', fields: webhookTrigger().fields}).fields,
|
||||||
...getGlobalTriggerFields(),
|
...trigger({slug: 'global', fields: globalTrigger().fields}).fields,
|
||||||
...getCronTriggerFields(),
|
...trigger({slug: 'cron', fields: cronTrigger().fields}).fields,
|
||||||
{
|
{
|
||||||
name: 'condition',
|
name: 'condition',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
@@ -91,7 +88,7 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
|
|||||||
// Virtual fields for custom triggers
|
// Virtual fields for custom triggers
|
||||||
// Note: Custom trigger fields from trigger-helpers already have unique names
|
// Note: Custom trigger fields from trigger-helpers already have unique names
|
||||||
// We just need to pass them through without modification
|
// We just need to pass them through without modification
|
||||||
...(triggers || []).flatMap(t => (t.inputs || []))
|
...(triggers || []).flatMap(t => (t.fields || []))
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -177,4 +174,5 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
|
|||||||
},
|
},
|
||||||
maxPerDoc: 10,
|
maxPerDoc: 10,
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import type {Field, TaskConfig} from "payload"
|
import type {TaskConfig} from "payload"
|
||||||
|
|
||||||
|
import type {Trigger} from "../triggers/types.js"
|
||||||
|
|
||||||
export type CollectionTriggerConfigCrud = {
|
export type CollectionTriggerConfigCrud = {
|
||||||
create?: true
|
create?: true
|
||||||
@@ -9,10 +11,7 @@ export type CollectionTriggerConfigCrud = {
|
|||||||
|
|
||||||
export type CollectionTriggerConfig = CollectionTriggerConfigCrud | true
|
export type CollectionTriggerConfig = CollectionTriggerConfigCrud | true
|
||||||
|
|
||||||
export type CustomTriggerConfig = {
|
export type TriggerConfig = <T extends string>(config: WorkflowsPluginConfig<T>) => Trigger
|
||||||
inputs?: Field[]
|
|
||||||
slug: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
export type WorkflowsPluginConfig<TSlug extends string> = {
|
export type WorkflowsPluginConfig<TSlug extends string> = {
|
||||||
collectionTriggers: {
|
collectionTriggers: {
|
||||||
@@ -20,6 +19,6 @@ export type WorkflowsPluginConfig<TSlug extends string> = {
|
|||||||
}
|
}
|
||||||
enabled?: boolean
|
enabled?: boolean
|
||||||
steps: TaskConfig<string>[],
|
steps: TaskConfig<string>[],
|
||||||
triggers?: CustomTriggerConfig[]
|
triggers?: TriggerConfig[]
|
||||||
webhookPrefix?: string
|
webhookPrefix?: string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,14 @@
|
|||||||
import type {Field} from 'payload'
|
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||||
|
|
||||||
import type { WorkflowsPluginConfig } from '../plugin/config-types.js'
|
export const collectionTrigger: TriggerConfig = ({collectionTriggers}) => ({
|
||||||
|
slug: 'collection',
|
||||||
import {triggerField} from "./helpers.js"
|
fields: [
|
||||||
|
{
|
||||||
export function getCollectionTriggerFields<T extends string>(
|
|
||||||
collectionTriggers: WorkflowsPluginConfig<T>['collectionTriggers']
|
|
||||||
): Field[] {
|
|
||||||
return [
|
|
||||||
triggerField({
|
|
||||||
name: 'collectionSlug',
|
name: 'collectionSlug',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
options: Object.keys(collectionTriggers || {}),
|
options: Object.keys(collectionTriggers || {}),
|
||||||
}),
|
},
|
||||||
triggerField({
|
{
|
||||||
name: 'operation',
|
name: 'operation',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
options: [
|
options: [
|
||||||
@@ -22,6 +17,6 @@ export function getCollectionTriggerFields<T extends string>(
|
|||||||
'read',
|
'read',
|
||||||
'update',
|
'update',
|
||||||
],
|
],
|
||||||
})
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,25 +1,23 @@
|
|||||||
import type {Field} from 'payload'
|
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||||
|
|
||||||
import {triggerField} from "./helpers.js"
|
export const cronTrigger: TriggerConfig = () => ({
|
||||||
|
slug: 'cron',
|
||||||
export function getCronTriggerFields(): Field[] {
|
fields: [
|
||||||
return [
|
{
|
||||||
triggerField({
|
|
||||||
name: 'cronExpression',
|
name: 'cronExpression',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
condition: (_, siblingData) => siblingData?.type === 'cron-trigger',
|
|
||||||
description: 'Cron expression for scheduled execution (e.g., "0 0 * * *" for daily at midnight)',
|
description: 'Cron expression for scheduled execution (e.g., "0 0 * * *" for daily at midnight)',
|
||||||
placeholder: '0 0 * * *'
|
placeholder: '0 0 * * *'
|
||||||
},
|
},
|
||||||
validate: (value: any, {siblingData}: any) => {
|
validate: (value: any, {siblingData}: any) => {
|
||||||
const cronValue = value || siblingData?.parameters?.cronExpression
|
const cronValue = value || siblingData?.parameters?.cronExpression
|
||||||
if (siblingData?.type === 'cron-trigger' && !cronValue) {
|
if (siblingData?.type === 'cron' && !cronValue) {
|
||||||
return 'Cron expression is required for cron triggers'
|
return 'Cron expression is required for cron triggers'
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate cron expression format if provided
|
// Validate cron expression format if provided
|
||||||
if (siblingData?.type === 'cron-trigger' && cronValue) {
|
if (siblingData?.type === 'cron' && cronValue) {
|
||||||
// Basic format validation - should be 5 parts separated by spaces
|
// Basic format validation - should be 5 parts separated by spaces
|
||||||
const cronParts = cronValue.trim().split(/\s+/)
|
const cronParts = cronValue.trim().split(/\s+/)
|
||||||
if (cronParts.length !== 5) {
|
if (cronParts.length !== 5) {
|
||||||
@@ -32,19 +30,18 @@ export function getCronTriggerFields(): Field[] {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
}),
|
},
|
||||||
triggerField({
|
{
|
||||||
name: 'timezone',
|
name: 'timezone',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
condition: (_, siblingData) => siblingData?.type === 'cron-trigger',
|
|
||||||
description: 'Timezone for cron execution (e.g., "America/New_York", "Europe/London"). Defaults to UTC.',
|
description: 'Timezone for cron execution (e.g., "America/New_York", "Europe/London"). Defaults to UTC.',
|
||||||
placeholder: 'UTC'
|
placeholder: 'UTC'
|
||||||
},
|
},
|
||||||
defaultValue: 'UTC',
|
defaultValue: 'UTC',
|
||||||
validate: (value: any, {siblingData}: any) => {
|
validate: (value: any, {siblingData}: any) => {
|
||||||
const tzValue = value || siblingData?.parameters?.timezone
|
const tzValue = value || siblingData?.parameters?.timezone
|
||||||
if (siblingData?.type === 'cron-trigger' && tzValue) {
|
if (siblingData?.type === 'cron' && tzValue) {
|
||||||
try {
|
try {
|
||||||
// Test if timezone is valid by trying to create a date with it
|
// Test if timezone is valid by trying to create a date with it
|
||||||
new Intl.DateTimeFormat('en', {timeZone: tzValue})
|
new Intl.DateTimeFormat('en', {timeZone: tzValue})
|
||||||
@@ -55,6 +52,6 @@ export function getCronTriggerFields(): Field[] {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
})
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
@@ -1,28 +1,25 @@
|
|||||||
import type {Field} from 'payload'
|
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||||
|
|
||||||
import {triggerField} from "./helpers.js"
|
export const globalTrigger: TriggerConfig = () => ({
|
||||||
|
slug: 'global',
|
||||||
export function getGlobalTriggerFields(): Field[] {
|
fields: [
|
||||||
return [
|
{
|
||||||
triggerField({
|
|
||||||
name: 'global',
|
name: 'global',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
admin: {
|
admin: {
|
||||||
condition: (_, siblingData) => siblingData?.type === 'global-trigger',
|
|
||||||
description: 'Global that triggers the workflow',
|
description: 'Global that triggers the workflow',
|
||||||
},
|
},
|
||||||
options: [], // Will be populated dynamically based on available globals
|
options: [], // Will be populated dynamically based on available globals
|
||||||
}),
|
},
|
||||||
triggerField({
|
{
|
||||||
name: 'globalOperation',
|
name: 'globalOperation',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
admin: {
|
admin: {
|
||||||
condition: (_, siblingData) => siblingData?.type === 'global-trigger',
|
|
||||||
description: 'Global operation that triggers the workflow',
|
description: 'Global operation that triggers the workflow',
|
||||||
},
|
},
|
||||||
options: [
|
options: [
|
||||||
'update'
|
'update'
|
||||||
],
|
],
|
||||||
})
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
import type {Field, TextField, SelectField} from "payload"
|
import type {Field} from "payload"
|
||||||
|
|
||||||
import type {Trigger} from "./types.js"
|
import type {Trigger} from "./types.js"
|
||||||
|
|
||||||
type FieldWithName = TextField | SelectField | (Field & { name: string })
|
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
slug: string,
|
slug: string,
|
||||||
fields?: FieldWithName[]
|
fields?: ({name: string} & Field)[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const trigger = ({
|
export const trigger = ({
|
||||||
@@ -15,18 +13,18 @@ export const trigger = ({
|
|||||||
}: Options): Trigger => {
|
}: Options): Trigger => {
|
||||||
return {
|
return {
|
||||||
slug,
|
slug,
|
||||||
fields: (fields || []).map(triggerField) as Field[]
|
fields: (fields || []).map(f => triggerField(slug, f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const triggerField = (field: FieldWithName): Field => ({
|
export const triggerField = (slug: string, field: {name: string} & Field): Field => ({
|
||||||
...field,
|
...field,
|
||||||
name: '__trigger_' + field.name,
|
name: '__trigger_' + field.name,
|
||||||
admin: {
|
admin: {
|
||||||
...(field.admin as any || {}),
|
...(field.admin as unknown || {}),
|
||||||
condition: (_, siblingData, __) => {
|
condition: (_, siblingData, __) => {
|
||||||
const previous = field.admin?.condition?.call(null, _, siblingData, __)
|
const previous = field.admin?.condition?.call(null, _, siblingData, __)
|
||||||
return previous !== false // Preserve existing condition if it exists
|
return previous || (siblingData?.type === slug)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
hooks: {
|
hooks: {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export { getCollectionTriggerFields } from './collection-trigger.js'
|
export { collectionTrigger } from './collection-trigger.js'
|
||||||
export { getCronTriggerFields } from './cron-trigger.js'
|
export { cronTrigger } from './cron-trigger.js'
|
||||||
export { getGlobalTriggerFields } from './global-trigger.js'
|
export { globalTrigger } from './global-trigger.js'
|
||||||
export { getWebhookTriggerFields } from './webhook-trigger.js'
|
export { webhookTrigger } from './webhook-trigger.js'
|
||||||
|
|||||||
@@ -1,22 +1,20 @@
|
|||||||
import type {Field} from 'payload'
|
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||||
|
|
||||||
import {triggerField} from "./helpers.js"
|
export const webhookTrigger: TriggerConfig = () => ({
|
||||||
|
slug: 'webhook',
|
||||||
export function getWebhookTriggerFields(): Field[] {
|
fields: [
|
||||||
return [
|
{
|
||||||
triggerField({
|
|
||||||
name: 'webhookPath',
|
name: 'webhookPath',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
admin: {
|
admin: {
|
||||||
condition: (_, siblingData) => siblingData?.type === 'webhook-trigger',
|
|
||||||
description: 'URL path for the webhook (e.g., "my-webhook"). Full URL will be /api/workflows-webhook/my-webhook',
|
description: 'URL path for the webhook (e.g., "my-webhook"). Full URL will be /api/workflows-webhook/my-webhook',
|
||||||
},
|
},
|
||||||
validate: (value: any, {siblingData}: any) => {
|
validate: (value: any, {siblingData}: any) => {
|
||||||
if (siblingData?.type === 'webhook-trigger' && !value && !siblingData?.parameters?.webhookPath) {
|
if (siblingData?.type === 'webhook' && !value && !siblingData?.parameters?.webhookPath) {
|
||||||
return 'Webhook path is required for webhook triggers'
|
return 'Webhook path is required for webhook triggers'
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
})
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user