UNPKG

n8n-nodes-placid

Version:

n8n node to interact with Placid API for creative generation

85 lines 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = execute; const n8n_workflow_1 = require("n8n-workflow"); const get_operation_1 = require("./get.operation"); const layerUtils_1 = require("../../utils/layerUtils"); const config_1 = require("../../utils/config"); async function execute(i) { 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 n8n_workflow_1.NodeOperationError(this.getNode(), 'Pages JSON must be an array'); } body.pages = pages; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON in pages configuration: ${error.message}`); } } else { const templateIdParam = this.getNodeParameter('template_id', i); const templateId = typeof templateIdParam === 'string' ? templateIdParam : templateIdParam.value; if (!templateId) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Template ID is required'); } const pageData = { template_uuid: templateId, layers: {}, }; const layers = this.getNodeParameter('layers.layerItems', i, []); pageData.layers = await (0, layerUtils_1.processUnifiedLayers)(layers, this, i); body.pages = [pageData]; } const createOptions = { method: 'POST', url: config_1.PlacidConfig.getRestUrl(config_1.PlacidConfig.ENDPOINTS.PDFS), headers: { 'Accept': config_1.PlacidConfig.HTTP.HEADERS.ACCEPT, 'Content-Type': config_1.PlacidConfig.HTTP.HEADERS.CONTENT_TYPE, 'x-placid-integration': config_1.PlacidConfig.HTTP.HEADERS.PLACID_INTEGRATION, }, body, }; const createResponse = await this.helpers.httpRequestWithAuthentication.call(this, 'placidApi', createOptions); if (createResponse.id) { const pdfId = createResponse.id; const pollingConfig = config_1.PlacidConfig.getPollingConfig('PDF'); const maxAttempts = pollingConfig.MAX_ATTEMPTS; const pollInterval = pollingConfig.INTERVAL_MS; for (let attempt = 0; attempt < maxAttempts; attempt++) { if (attempt > 0) { await (0, n8n_workflow_1.sleep)(pollInterval); } const pollResponse = await (0, get_operation_1.getPdfById)(this, pdfId); if (pollResponse.status === 'finished') { return { json: pollResponse, pairedItem: { item: i }, }; } else if (pollResponse.status === 'error') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `PDF generation failed: ${pollResponse.error || 'Unknown error'}`); } } throw new n8n_workflow_1.NodeOperationError(this.getNode(), `${pollingConfig.TIMEOUT_MESSAGE} after ${maxAttempts} attempts. Last status: ${createResponse.status}`); } return { json: createResponse, pairedItem: { item: i }, }; } //# sourceMappingURL=create.operation.js.map