UNPKG

@daouy/n8n-nodes-carbone

Version:

n8n nodes for Carbone integration

353 lines 14.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CarboneLifecycle = void 0; const n8n_workflow_1 = require("n8n-workflow"); class CarboneLifecycle { constructor() { this.description = { displayName: 'Carbone Lifecycle', name: 'ComposableReport', icon: 'file:carbone.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Interact with Carbone API through lifecycle stages', defaults: { name: 'Carbone Lifecycle', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'carboneApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Create Template', value: 'createTemplate', description: 'Upload and register a template', action: 'Create a template', }, { name: 'Generate Report', value: 'generateReport', description: 'Generate a report from template and data', action: 'Generate a report', }, { name: 'Get Report Status', value: 'getReportStatus', description: 'Check the status of a report generation', action: 'Get report status', }, { name: 'Download Report', value: 'downloadReport', description: 'Download a generated report', action: 'Download a report', }, ], default: 'createTemplate', }, { displayName: 'Template Source', name: 'templateSource', type: 'options', options: [ { name: 'Existing Template', value: 'existing', }, { name: 'Upload New Template', value: 'upload', }, ], default: 'existing', displayOptions: { show: { operation: ['createTemplate'], }, }, description: 'Choose whether to use an existing template or upload a new one', }, { displayName: 'Existing Template', name: 'existingTemplate', type: 'resourceLocator', default: { mode: 'list', value: '' }, required: true, modes: [ { displayName: 'From List', name: 'list', type: 'list', placeholder: 'Select a template', typeOptions: { searchListMethod: 'listTemplates', searchable: true, }, }, ], displayOptions: { show: { operation: ['createTemplate'], templateSource: ['existing'], }, }, description: 'Choose a template from the list of existing templates', }, { displayName: 'Template File', name: 'templateFile', type: 'string', default: '', required: true, displayOptions: { show: { operation: ['createTemplate'], templateSource: ['upload'], }, }, description: 'Path to the template file to upload (DOCX, XLSX, PPTX, ODT, etc.)', }, { displayName: 'Template Name', name: 'templateName', type: 'string', default: '', required: true, displayOptions: { show: { operation: ['createTemplate'], }, }, description: 'Name for the template', }, { displayName: 'Template ID', name: 'templateId', type: 'string', default: '', required: true, displayOptions: { show: { operation: ['generateReport'], }, }, description: 'ID of the template to use', }, { displayName: 'Data', name: 'data', type: 'json', default: '{}', required: true, displayOptions: { show: { operation: ['generateReport'], }, }, description: 'Data to use for report generation (JSON format)', }, { displayName: 'Output Format', name: 'outputFormat', type: 'options', displayOptions: { show: { operation: ['generateReport'], }, }, options: [ { name: 'PDF', value: 'pdf', }, { name: 'DOCX', value: 'docx', }, { name: 'XLSX', value: 'xlsx', }, { name: 'PPTX', value: 'pptx', }, { name: 'HTML', value: 'html', }, ], default: 'pdf', description: 'Format of the output file', }, { displayName: 'Report ID', name: 'reportId', type: 'string', default: '', required: true, displayOptions: { show: { operation: ['getReportStatus', 'downloadReport'], }, }, description: 'ID of the report to check', }, ], }; } async listTemplates() { const credentials = await this.getCredentials('carboneApi'); const apiUrl = credentials.apiUrl; const apiKey = credentials.apiKey; const options = { method: 'GET', url: `${apiUrl}/templates`, headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', }, json: true, }; try { const response = await this.helpers.request(options); if (Array.isArray(response)) { return response.map(template => ({ name: template.name || `Template ${template.id}`, value: template.id, })); } return []; } catch (error) { console.error('Error fetching templates:', error); return []; } } async execute() { const items = this.getInputData(); const returnData = []; const credentials = await this.getCredentials('carboneApi'); const apiUrl = credentials.apiUrl; const apiKey = credentials.apiKey; for (let i = 0; i < items.length; i++) { try { const operation = this.getNodeParameter('operation', i); let responseData; const headers = { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', }; if (operation === 'createTemplate') { const templateName = this.getNodeParameter('templateName', i); const templateSource = this.getNodeParameter('templateSource', i); let options; if (templateSource === 'existing') { const existingTemplate = this.getNodeParameter('existingTemplate', i); const templateId = existingTemplate.value; options = { method: 'POST', url: `${apiUrl}/template/use`, headers, body: { templateId, templateName, }, json: true, }; } else { const templateFile = this.getNodeParameter('templateFile', i); console.log(`Preparing to upload template from: ${templateFile}`); options = { method: 'POST', url: `${apiUrl}/template`, headers, body: { templateName, templateFile: 'BASE64_ENCODED_FILE_CONTENT', }, json: true, }; } responseData = await this.helpers.request(options); } else if (operation === 'generateReport') { const templateId = this.getNodeParameter('templateId', i); const data = this.getNodeParameter('data', i); const outputFormat = this.getNodeParameter('outputFormat', i); const options = { method: 'POST', url: `${apiUrl}/render/${templateId}`, headers, body: { data, outputFormat, }, json: true, }; responseData = await this.helpers.request(options); } else if (operation === 'getReportStatus') { const reportId = this.getNodeParameter('reportId', i); const options = { method: 'GET', url: `${apiUrl}/report/${reportId}/status`, headers, json: true, }; responseData = await this.helpers.request(options); } else if (operation === 'downloadReport') { const reportId = this.getNodeParameter('reportId', i); const options = { method: 'GET', url: `${apiUrl}/report/${reportId}/download`, headers, encoding: null, }; const binaryData = await this.helpers.request(options); const binaryProperty = 'data'; const fileName = `report-${reportId}.pdf`; const newItem = { json: { reportId, success: true, }, binary: {}, }; if (newItem.binary) { newItem.binary[binaryProperty] = await this.helpers.prepareBinaryData(Buffer.from(binaryData), fileName); } returnData.push(newItem); continue; } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ ...items[i].json, ...responseData }), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionData); continue; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i }); } } return [returnData]; } } exports.CarboneLifecycle = CarboneLifecycle; //# sourceMappingURL=CarboneLifecycle.node.js.map