UNPKG

n8n-nodes-placid

Version:

n8n node to interact with Placid API for creative generation

528 lines 20.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createLayerConfigurationFields = createLayerConfigurationFields; exports.uploadBinaryFile = uploadBinaryFile; exports.processUnifiedLayers = processUnifiedLayers; exports.processLegacyLayers = processLegacyLayers; exports.createNestedLayerFields = createNestedLayerFields; const layerProperties_1 = require("./layerProperties"); const n8n_workflow_1 = require("n8n-workflow"); function createLayerConfigurationFields(resourceType, operationType, configModeField, collectionName, itemName, displayName) { const allLayerTypes = (0, layerProperties_1.getAllLayerTypes)(); const propertyFields = []; allLayerTypes.forEach(layerType => { var _a; const properties = (0, layerProperties_1.getAllPropertiesForLayerType)(layerType.value, resourceType); if (properties.length > 0) { propertyFields.push({ displayName: 'Property', name: 'property', type: 'options', options: properties.map(prop => ({ name: prop.name, value: prop.value, description: prop.description, })), default: ((_a = properties[0]) === null || _a === void 0 ? void 0 : _a.value) || '', description: `Choose what property to set for this ${layerType.name.toLowerCase()} layer`, displayOptions: { show: { layerType: [layerType.value], }, }, }); } }); const valueFields = []; const allProperties = new Map(); allLayerTypes.forEach(layerType => { const properties = (0, layerProperties_1.getAllPropertiesForLayerType)(layerType.value, resourceType); properties.forEach(prop => { if (!allProperties.has(prop.value)) { allProperties.set(prop.value, { ...prop, layerTypes: [layerType.value] }); } else { allProperties.get(prop.value).layerTypes.push(layerType.value); } }); }); allProperties.forEach((propInfo, propValue) => { var _a; if (propValue === 'custom') { return; } let fieldType = propInfo.fieldType || 'string'; const placeholder = propInfo.placeholder || ''; const description = propInfo.description; if (propValue === 'imageArray') { fieldType = 'string'; } if (fieldType === 'binary') { fieldType = 'string'; } const fieldConfig = { displayName: propInfo.name, name: `${propValue}Value`, type: fieldType, default: '', description, displayOptions: { show: { property: [propValue], }, }, }; if (fieldType === 'options' && propInfo.options) { fieldConfig.options = propInfo.options; fieldConfig.default = ((_a = propInfo.options[0]) === null || _a === void 0 ? void 0 : _a.value) || ''; } else { fieldConfig.placeholder = placeholder; if (propValue === 'imageArray') { fieldConfig.typeOptions = { rows: 4, }; } if (propInfo.fieldType === 'binary') { fieldConfig.description = `${description} - Enter the field name containing the binary file data (e.g., "data")`; fieldConfig.hint = 'The field name from the input data that contains the binary file data'; } } valueFields.push(fieldConfig); }); valueFields.push({ displayName: 'Custom Property Name', name: 'customPropertyName', type: 'string', default: '', placeholder: 'e.g., lineHeight, letterSpacing, transform', description: 'The name of the custom property to set', displayOptions: { show: { property: ['custom'], }, }, }, { displayName: 'Custom Property Value', name: 'customPropertyValue', type: 'string', default: '', placeholder: 'e.g., 1.5, 2px, scale(1.2)', description: 'The value for the custom property', displayOptions: { show: { property: ['custom'], }, }, }); return [ { displayName: collectionName, name: collectionName.toLowerCase(), type: 'fixedCollection', default: { [itemName]: [] }, typeOptions: { multipleValues: true, }, displayOptions: { show: { resource: [resourceType], operation: [operationType], [configModeField]: ['simple'], }, }, options: [ { name: itemName, displayName: displayName, values: [ { displayName: 'Layer Name', name: 'layerId', type: 'string', description: 'Enter the name of the layer to modify (e.g., \'title\', \'subtitle\', \'logo\')', placeholder: 'e.g., title', default: '', required: true, }, { displayName: 'Layer Type', name: 'layerType', type: 'options', options: allLayerTypes, default: 'text', description: 'Select the type of layer you want to configure', }, ...propertyFields, ...valueFields, ], }, ], }, ]; } async function uploadBinaryFile(executeFunctions, index, fieldName) { var _a, _b; const items = executeFunctions.getInputData(); const item = items[index]; if (!item.binary || !item.binary[fieldName]) { throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `No binary data found for field "${fieldName}"`); } const binaryData = item.binary[fieldName]; const fileBuffer = await executeFunctions.helpers.getBinaryDataBuffer(index, fieldName); let fileName = binaryData.fileName; if (!fileName) { const extension = ((_a = binaryData.mimeType) === null || _a === void 0 ? void 0 : _a.split('/')[1]) || 'bin'; fileName = `upload.${extension}`; } const formData = { file: { value: fileBuffer, options: { filename: fileName, contentType: binaryData.mimeType || 'application/octet-stream', }, }, }; try { const response = await executeFunctions.helpers.requestWithAuthentication.call(executeFunctions, 'placidApi', { method: 'POST', url: 'https://api.placid.app/api/rest/media', formData, json: false, }); let responseData; try { responseData = typeof response === 'string' ? JSON.parse(response) : response; } catch (error) { throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Failed to parse upload response: ${error.message}`); } if (!responseData.media || !Array.isArray(responseData.media) || responseData.media.length === 0) { throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), 'Invalid response format from Placid media upload API'); } return responseData.media[0].file_id; } catch (error) { if (error.response) { const statusCode = error.response.status; const errorMessage = ((_b = error.response.data) === null || _b === void 0 ? void 0 : _b.message) || error.response.data || error.message; throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Media upload failed (${statusCode}): ${errorMessage}`); } throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Media upload failed: ${error.message}`); } } async function processUnifiedLayers(layers, executeFunctions, index) { const processedLayers = {}; for (const layer of layers) { const layerName = layer.layerId; if (!layerName) continue; if (!processedLayers[layerName]) { processedLayers[layerName] = {}; } if (layer.property === 'custom' && layer.customPropertyName && layer.customPropertyValue) { processedLayers[layerName][layer.customPropertyName] = layer.customPropertyValue; continue; } const propertyValue = layer.property; let valueToSet = null; switch (propertyValue) { case 'text': valueToSet = layer.textValue; break; case 'color': case 'text_color': case 'alt_text_color': case 'background_color': case 'border_color': valueToSet = layer.colorValue; break; case 'font': case 'alt_font': case 'fontFamily': valueToSet = layer.fontFamilyValue; break; case 'fontSize': valueToSet = layer.fontSizeValue; break; case 'image': valueToSet = layer.imageArrayValue && layer.imageArrayValue.length > 0 ? layer.imageArrayValue : layer.imageValue; break; case 'imageBinary': if (layer.imageBinaryValue && executeFunctions && index !== undefined) { try { valueToSet = await uploadBinaryFile(executeFunctions, index, layer.imageBinaryValue); if (valueToSet) { processedLayers[layerName]['image'] = valueToSet; } continue; } catch (error) { throw error; } } break; case 'imageArray': if (layer.imageArrayValue) { if (Array.isArray(layer.imageArrayValue)) { valueToSet = layer.imageArrayValue.length > 0 ? layer.imageArrayValue : null; } else if (typeof layer.imageArrayValue === 'string') { const imageUrls = layer.imageArrayValue.split('\n') .map(url => url.trim()) .filter(url => url.length > 0); valueToSet = imageUrls.length > 0 ? imageUrls : null; } } if (valueToSet !== null && valueToSet !== undefined && valueToSet !== '') { processedLayers[layerName]['image'] = valueToSet; } continue; case 'video': valueToSet = layer.videoValue; break; case 'videoBinary': if (layer.videoBinaryValue && executeFunctions && index !== undefined) { try { valueToSet = await uploadBinaryFile(executeFunctions, index, layer.videoBinaryValue); if (valueToSet) { processedLayers[layerName]['video'] = valueToSet; } continue; } catch (error) { throw error; } } break; case 'opacity': valueToSet = layer.opacityValue; break; case 'rotation': valueToSet = layer.rotationValue; break; case 'border_radius': case 'borderRadius': valueToSet = layer.borderRadiusValue; break; case 'border_width': valueToSet = layer.border_widthValue; break; case 'url': valueToSet = layer.urlValue; break; case 'svg': valueToSet = layer.svgValue; break; case 'image_viewport': valueToSet = layer.image_viewportValue; break; case 'value': valueToSet = layer.valueValue; break; case 'srt': valueToSet = layer.srtValue; break; default: const fieldName = `${propertyValue}Value`; valueToSet = layer[fieldName]; break; } if (valueToSet !== null && valueToSet !== undefined && valueToSet !== '') { processedLayers[layerName][propertyValue] = valueToSet; } } return processedLayers; } function processLegacyLayers(textLayers = [], pictureLayers = []) { const processedLayers = {}; for (const layer of textLayers) { const layerName = layer.layerId; if (layer.contentType === 'text' && layer.textValue) { processedLayers[layerName] = { text: layer.textValue, }; } else if (layer.contentType === 'custom' && layer.customPropertyName && layer.customPropertyValue) { processedLayers[layerName] = { [layer.customPropertyName]: layer.customPropertyValue, }; } } for (const layer of pictureLayers) { const layerName = layer.layerId; if (layer.contentType === 'image' && layer.imageValue) { processedLayers[layerName] = { image: layer.imageValue, }; } else if (layer.contentType === 'video' && layer.videoValue) { processedLayers[layerName] = { video: layer.videoValue, }; } else if (layer.contentType === 'custom' && layer.customPropertyName && layer.customPropertyValue) { processedLayers[layerName] = { [layer.customPropertyName]: layer.customPropertyValue, }; } } return processedLayers; } function createNestedLayerFields(resourceType) { const allLayerTypes = (0, layerProperties_1.getAllLayerTypes)(); const propertyFields = []; allLayerTypes.forEach(layerType => { var _a; const properties = (0, layerProperties_1.getAllPropertiesForLayerType)(layerType.value, resourceType); if (properties.length > 0) { propertyFields.push({ displayName: 'Property', name: 'property', type: 'options', options: properties.map(prop => ({ name: prop.name, value: prop.value, description: prop.description, })), default: ((_a = properties[0]) === null || _a === void 0 ? void 0 : _a.value) || '', description: `Choose what property to set for this ${layerType.name.toLowerCase()} layer`, displayOptions: { show: { layerType: [layerType.value], }, }, }); } }); const valueFields = []; const allProperties = new Map(); allLayerTypes.forEach(layerType => { const properties = (0, layerProperties_1.getAllPropertiesForLayerType)(layerType.value, resourceType); properties.forEach(prop => { if (!allProperties.has(prop.value)) { allProperties.set(prop.value, { ...prop, layerTypes: [layerType.value] }); } else { allProperties.get(prop.value).layerTypes.push(layerType.value); } }); }); allProperties.forEach((propInfo, propValue) => { var _a; if (propValue === 'custom') { return; } let fieldType = propInfo.fieldType || 'string'; const placeholder = propInfo.placeholder || ''; const description = propInfo.description; if (propValue === 'imageArray') { fieldType = 'string'; } if (fieldType === 'binary') { fieldType = 'string'; } const fieldConfig = { displayName: propInfo.name, name: `${propValue}Value`, type: fieldType, default: '', description, displayOptions: { show: { property: [propValue], }, }, }; if (fieldType === 'options' && propInfo.options) { fieldConfig.options = propInfo.options; fieldConfig.default = ((_a = propInfo.options[0]) === null || _a === void 0 ? void 0 : _a.value) || ''; } else { fieldConfig.placeholder = placeholder; if (propValue === 'imageArray') { fieldConfig.typeOptions = { rows: 4, }; } if (propInfo.fieldType === 'binary') { fieldConfig.description = `${description} - Enter the field name containing the binary file data (e.g., "data")`; fieldConfig.hint = 'The field name from the input data that contains the binary file data'; } } valueFields.push(fieldConfig); }); valueFields.push({ displayName: 'Custom Property Name', name: 'customPropertyName', type: 'string', default: '', placeholder: 'e.g., lineHeight, letterSpacing, transform', description: 'The name of the custom property to set', displayOptions: { show: { property: ['custom'], }, }, }, { displayName: 'Custom Property Value', name: 'customPropertyValue', type: 'string', default: '', placeholder: 'e.g., 1.5, 2px, scale(1.2)', description: 'The value for the custom property', displayOptions: { show: { property: ['custom'], }, }, }); return [ { displayName: 'Layer Configuration', name: 'layerData', type: 'fixedCollection', default: { layerItems: [] }, typeOptions: { multipleValues: true, }, options: [ { name: 'layerItems', displayName: 'Add Layer', values: [ { displayName: 'Layer Name', name: 'layerId', type: 'string', description: 'Enter the name of the layer to modify (e.g., \'title\', \'subtitle\', \'logo\')', placeholder: 'e.g., title', default: '', required: true, }, { displayName: 'Layer Type', name: 'layerType', type: 'options', options: allLayerTypes, default: 'text', description: 'Select the type of layer you want to configure', }, ...propertyFields, ...valueFields, ], }, ], }, ]; } //# sourceMappingURL=layerUtils.js.map