n8n-nodes-placid
Version:
n8n node to interact with Placid API for creative generation
68 lines • 2.7 kB
JavaScript
;
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) {
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 Error(`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: 'https://api.placid.app/api/rest/images',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body,
};
const createResponse = await this.helpers.httpRequestWithAuthentication.call(this, 'placidApi', createOptions);
if (createResponse.id) {
const imageId = 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.getImageById)(this, imageId);
if (pollResponse.status === 'finished') {
return {
json: pollResponse,
};
}
else if (pollResponse.status === 'error') {
throw new Error(`Image generation failed: ${pollResponse.error || 'Unknown error'}`);
}
}
throw new Error(`Image generation timed out after ${maxAttempts} attempts. Last status: ${createResponse.status}`);
}
return {
json: createResponse,
};
}
//# sourceMappingURL=create.operation.js.map