mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-10 00:43:23 +00:00
Disable perfectionist ESLint rules
- Disable 'perfectionist/sort-object-types' and 'perfectionist/sort-objects' - Allow natural object property ordering without enforced sorting
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
/**
|
||||
* Trigger builder helpers for creating custom triggers with less boilerplate
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { createTrigger, createTriggerField, webhookTrigger } from '@xtr-dev/payload-automation/helpers'
|
||||
*
|
||||
* // Simple trigger with array of fields
|
||||
* const myTrigger = createTrigger('my-trigger', [
|
||||
* { name: 'apiKey', type: 'text', required: true },
|
||||
* { name: 'timeout', type: 'number', defaultValue: 30 }
|
||||
* ])
|
||||
*
|
||||
* // Single field with virtual storage
|
||||
* const field = createTriggerField(
|
||||
* { name: 'webhookUrl', type: 'text', required: true },
|
||||
* 'my-trigger'
|
||||
* )
|
||||
*
|
||||
* // Webhook trigger preset
|
||||
* const orderWebhook = webhookTrigger('order-webhook')
|
||||
* ```
|
||||
*/
|
||||
|
||||
// Core helpers
|
||||
export {
|
||||
createTriggerField,
|
||||
createTrigger
|
||||
} from '../utils/trigger-helpers.js'
|
||||
|
||||
// Preset builders
|
||||
export {
|
||||
webhookTrigger,
|
||||
cronTrigger,
|
||||
eventTrigger,
|
||||
manualTrigger,
|
||||
apiTrigger
|
||||
} from '../utils/trigger-presets.js'
|
||||
0
src/triggers/helpers.ts
Normal file
0
src/triggers/helpers.ts
Normal file
0
src/triggers/types.ts
Normal file
0
src/triggers/types.ts
Normal file
@@ -1,158 +0,0 @@
|
||||
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>
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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}_${originalName}`
|
||||
|
||||
const resultField: Record<string, unknown> = {
|
||||
...field,
|
||||
admin: {
|
||||
...(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: {
|
||||
...(field.hooks as Record<string, unknown[]> || {}),
|
||||
afterRead: [
|
||||
...((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: [
|
||||
...((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
|
||||
}
|
||||
]
|
||||
},
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 createTrigger(slug: string, fields: FieldWithName[]): CustomTriggerConfig {
|
||||
return {
|
||||
slug,
|
||||
inputs: fields.map(field => createTriggerField(field, slug))
|
||||
}
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
import { createTrigger } from './trigger-helpers.js'
|
||||
import type { CustomTriggerConfig } from '../plugin/config-types.js'
|
||||
|
||||
/**
|
||||
* Preset trigger builders for common patterns
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create a webhook trigger with common webhook parameters pre-configured
|
||||
*/
|
||||
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(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(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(slug: string): CustomTriggerConfig {
|
||||
return {
|
||||
slug,
|
||||
inputs: []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an API trigger for external systems to call
|
||||
*/
|
||||
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")'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'method',
|
||||
type: 'select',
|
||||
options: ['GET', 'POST', 'PUT', 'PATCH'],
|
||||
defaultValue: 'POST',
|
||||
admin: {
|
||||
description: 'HTTP method for the API endpoint'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'authentication',
|
||||
type: 'select',
|
||||
options: [
|
||||
{ label: 'None', value: 'none' },
|
||||
{ label: 'API Key', value: 'api-key' },
|
||||
{ label: 'Bearer Token', value: 'bearer' },
|
||||
{ label: 'Basic Auth', value: 'basic' }
|
||||
],
|
||||
defaultValue: 'api-key',
|
||||
admin: {
|
||||
description: 'Authentication method for the API endpoint'
|
||||
}
|
||||
}
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user