n8n-nodes-placid
Version:
n8n node to interact with Placid API for creative generation
74 lines • 3.29 kB
JavaScript
;
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 templateIdParam = this.getNodeParameter('template_id', i);
const configurationMode = this.getNodeParameter('configurationMode', i);
const templateId = typeof templateIdParam === 'string' ? templateIdParam : templateIdParam.value;
const body = {
template_uuid: templateId,
layers: {},
};
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 layersJson = this.getNodeParameter('layersJson', i);
try {
body.layers = JSON.parse(layersJson);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON in layers configuration: ${error.message}`);
}
}
else {
const layers = this.getNodeParameter('layers.layerItems', i, []);
body.layers = await (0, layerUtils_1.processUnifiedLayers)(layers, this, i);
}
const createOptions = {
method: 'POST',
url: config_1.PlacidConfig.getRestUrl(config_1.PlacidConfig.ENDPOINTS.IMAGES),
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 imageId = createResponse.id;
const pollingConfig = config_1.PlacidConfig.getPollingConfig('IMAGE');
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.getImageById)(this, imageId);
if (pollResponse.status === 'finished') {
return {
json: pollResponse,
pairedItem: { item: i },
};
}
else if (pollResponse.status === 'error') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Image 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