UNPKG

n8n-nodes-placid

Version:

n8n node to interact with Placid API for creative generation

85 lines 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = execute; const get_operation_1 = require("./get.operation"); const layerUtils_1 = require("../../utils/layerUtils"); async function execute(i) { var _a; const configurationMode = this.getNodeParameter('configurationMode', i); const body = { pages: [], }; const additionalFields = this.getNodeParameter('additionalFields', i, {}); if (additionalFields.webhook_success) { body.webhook_success = additionalFields.webhook_success; } if (additionalFields.passthrough) { body.passthrough = additionalFields.passthrough; } if (configurationMode === 'advanced') { const pagesJson = this.getNodeParameter('pagesJson', i); try { const pages = JSON.parse(pagesJson); if (!Array.isArray(pages)) { throw new Error('Pages JSON must be an array'); } body.pages = pages; } catch (error) { throw new Error(`Invalid JSON in pages configuration: ${error.message}`); } } else { const pages = this.getNodeParameter('pages.pageItems', i, []); if (!pages || pages.length === 0) { throw new Error('At least one page is required for PDF generation'); } for (const page of pages) { const templateId = typeof page.template_id === 'string' ? page.template_id : page.template_id.value; if (!templateId) { throw new Error('Template ID is required for each page'); } const pageData = { template_uuid: templateId, layers: {}, }; const layers = ((_a = page.layerData) === null || _a === void 0 ? void 0 : _a.layerItems) || []; pageData.layers = await (0, layerUtils_1.processUnifiedLayers)(layers, this, i); body.pages.push(pageData); } } const createOptions = { method: 'POST', url: 'https://api.placid.app/api/rest/pdfs', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body, }; const createResponse = await this.helpers.httpRequestWithAuthentication.call(this, 'placidApi', createOptions); if (createResponse.id) { const pdfId = createResponse.id; const maxAttempts = 30; const pollInterval = 2000; for (let attempt = 0; attempt < maxAttempts; attempt++) { if (attempt > 0) { await new Promise(resolve => setTimeout(resolve, pollInterval)); } const pollResponse = await (0, get_operation_1.getPdfById)(this, pdfId); if (pollResponse.status === 'finished') { return { json: pollResponse, }; } else if (pollResponse.status === 'error') { throw new Error(`PDF generation failed: ${pollResponse.error || 'Unknown error'}`); } } throw new Error(`PDF generation timed out after ${maxAttempts} attempts. Last status: ${createResponse.status}`); } return { json: createResponse, }; } //# sourceMappingURL=create.operation.js.map