Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.4 KB
Raw Blame
import { FlowActionType, flowStructureUtil, FlowVersion, PropertyExecutionType } from '@activepieces/shared'
import semver from 'semver'
import { Migration } from '.'

const HTTP_PIECE_NAME = '@activepieces/piece-http'
const WEBHOOK_PIECE_NAME = '@activepieces/piece-webhook'
const HTTP_RETURN_RESPONSE_ACTION = 'return_response'
const WEBHOOK_RETURN_RESPONSE_ACTION = 'return_response'

export const migrateHttpToWebhookV5: Migration = {
    targetSchemaVersion: '5',
    migrate: async (flowVersion: FlowVersion): Promise<FlowVersion> => {
        const newVersion = flowStructureUtil.transferFlow(flowVersion, (step) => {
            if (
                step.type === FlowActionType.PIECE &&
                step.settings.pieceName === HTTP_PIECE_NAME &&
                step.settings.actionName === HTTP_RETURN_RESPONSE_ACTION
            ) {
                const httpInput = step.settings.input || {}
                const fields: Record<string, unknown> = {}
                const pieceVersionWithoutTildaOrPlus = step.settings.pieceVersion.replace('~', '').replace('^', '')
                // Check the scehma for each action in the http piece
                const isGreaterThanOrEqual050 = semver.gte(pieceVersionWithoutTildaOrPlus, '0.5.0')

                if (httpInput['body'] && typeof httpInput['body'] === 'object' && 'data' in httpInput['body'] && isGreaterThanOrEqual050) {
                    fields['body'] = (httpInput['body'] as Record<string, unknown>)['data']
                }

                if (httpInput['body'] && typeof httpInput['body'] === 'object' && !isGreaterThanOrEqual050) {
                    fields['body'] = (httpInput['body'] as Record<string, unknown>)
                }
           


                if (httpInput['status'] !== undefined) {
                    fields['status'] = httpInput['status']
                }
                if (httpInput['headers']) {
                    fields['headers'] = httpInput['headers']
                }
                
                const webhookInput = {
                    respond: 'stop',
                    responseType: httpInput['body_type'] || 'json',
                    fields,
                }
                
                return {
                    ...step,
                    settings: {
                        ...step.settings,
                        pieceName: WEBHOOK_PIECE_NAME,
                        pieceVersion: '0.1.20',
                        actionName: WEBHOOK_RETURN_RESPONSE_ACTION,
                        input: webhookInput,
                        propertySettings: {
                            ...step.settings.propertySettings,
                            'respond': {
                                type: PropertyExecutionType.MANUAL,
                                schema: undefined,
                            },
                            'responseType': {
                                type: PropertyExecutionType.MANUAL,
                                schema: undefined,
                            },
                            'fields': {
                                type: PropertyExecutionType.MANUAL,
                                schema: undefined,
                            },
                        },
                    },
                }
            }
            return step
        })
        
        return {
            ...newVersion,
            schemaVersion: '6',
        }
    },
}