UNPKG

n8n-nodes-fxcm

Version:
136 lines (135 loc) 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FxcmTrigger = void 0; const FxcmClient_1 = require("./FxcmClient"); class FxcmTrigger { constructor() { this.description = { displayName: 'FXCM Trigger', name: 'fxcmTrigger', icon: 'file:fxcm.svg', group: ['trigger'], version: 1, description: 'Handle FXCM events', defaults: { name: 'FXCM Trigger', }, inputs: [], outputs: ['main'], credentials: [ { name: 'fxcmApi', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Event', name: 'event', type: 'options', options: [ { name: 'Price Update', value: 'priceUpdate', description: 'Triggers when price changes for specified instrument', }, { name: 'Order Update', value: 'orderUpdate', description: 'Triggers when order status changes', }, { name: 'Position Update', value: 'positionUpdate', description: 'Triggers when position changes', }, ], default: 'priceUpdate', required: true, }, { displayName: 'Instrument', name: 'instrument', type: 'string', default: 'EUR/USD', description: 'The trading instrument to monitor', required: true, displayOptions: { show: { event: ['priceUpdate'], }, }, }, { displayName: 'Minimum Price Change', name: 'minPriceChange', type: 'number', default: 0.0001, description: 'Minimum price change to trigger an update', required: true, displayOptions: { show: { event: ['priceUpdate'], }, }, } ], }; } async trigger() { const credentials = await this.getCredentials('fxcmApi'); // Convert credentials to FxcmCredentials format const fxcmCredentials = { apiToken: credentials.apiToken, apiUrl: `https://${credentials.accountType === 'demo' ? 'demo-api' : 'api'}.fxcm.com`, accountType: credentials.accountType, tradingStationId: credentials.tradingStationId, }; const client = new FxcmClient_1.FxcmClient(fxcmCredentials); const event = this.getNodeParameter('event'); try { if (event === 'priceUpdate') { const instrument = this.getNodeParameter('instrument'); const minPriceChange = this.getNodeParameter('minPriceChange'); await client.subscribeToPrices(instrument, (data) => { if (Math.abs(data.priceChange) >= minPriceChange) { this.emit([this.helpers.returnJsonArray([data])]); } }); } // ... handle other events ... } catch (error) { if (error instanceof Error) { throw new Error(`FXCM Trigger error: ${error.message}`); } else { throw new Error('FXCM Trigger error: Unknown error'); } } return { closeFunction: async () => { client.disconnect(); } }; } // Fix webhook method to use proper types and methods async webhook() { const body = this.getBodyData(); return { webhookResponse: { statusCode: 200, body: body, }, }; } } exports.FxcmTrigger = FxcmTrigger;