n8n-nodes-piapi
Version:
Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows
152 lines • 6.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.PiAPITaskStatus = void 0;
const GenericFunctions_1 = require("../shared/GenericFunctions");
class PiAPITaskStatus {
constructor() {
this.description = {
displayName: 'PiAPI Task Status',
name: 'piAPITaskStatus',
icon: 'file:../piapi.svg',
group: ['transform'],
version: 1,
description: 'Check the status of a PiAPI task',
defaults: {
name: 'PiAPI Task Status',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'piAPIApi',
required: true,
},
],
properties: [
{
displayName: 'Task ID',
name: 'taskId',
type: 'string',
default: '',
required: true,
description: 'The ID of the task to check (use: {{ $json.data?.task_id || $json.task_id }} to infer from input data)',
placeholder: '{{ $json.data?.task_id || $json.task_id }}',
},
{
displayName: 'Return Only Media URL',
name: 'returnOnlyImageUrl',
type: 'boolean',
default: false,
description: 'Whether to return only the image/video URL instead of the full task data',
},
{
displayName: 'Return Binary Data',
name: 'returnBinaryData',
type: 'boolean',
default: false,
description: 'Whether to return the Image/Video as binary data',
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
returnBinaryData: [true],
},
},
description: 'Name of the binary property to which to write the data of the image',
},
],
};
}
async execute() {
var _a, _b, _c, _d, _e, _f, _g;
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
let taskId = this.getNodeParameter('taskId', i, '');
if (!taskId || taskId.trim() === '') {
const inputData = items[i].json;
taskId = ((_a = inputData === null || inputData === void 0 ? void 0 : inputData.data) === null || _a === void 0 ? void 0 : _a.task_id) || (inputData === null || inputData === void 0 ? void 0 : inputData.task_id) || '';
if (!taskId || taskId.trim() === '') {
throw new Error('Task ID is required and cannot be found in input data');
}
}
const returnOnlyImageUrl = this.getNodeParameter('returnOnlyImageUrl', i, false);
const returnBinaryData = this.getNodeParameter('returnBinaryData', i, false);
const binaryPropertyName = returnBinaryData
? this.getNodeParameter('binaryPropertyName', i)
: '';
try {
const encodedTaskId = encodeURIComponent(taskId.trim());
const response = await GenericFunctions_1.piApiRequest.call(this, 'GET', `/api/v1/task/${encodedTaskId}`);
if (!response.data || response.code !== 200) {
throw new Error(`Failed to retrieve task: ${response.message || 'Unknown error'}`);
}
const mediaUrl = ((_c = (_b = response.data) === null || _b === void 0 ? void 0 : _b.output) === null || _c === void 0 ? void 0 : _c.image_url) || ((_e = (_d = response.data) === null || _d === void 0 ? void 0 : _d.output) === null || _e === void 0 ? void 0 : _e.video_url);
if (returnOnlyImageUrl && mediaUrl) {
returnData.push({
json: {
code: 200,
data: {
...response.data,
mediaUrl,
type: ((_g = (_f = response.data) === null || _f === void 0 ? void 0 : _f.output) === null || _g === void 0 ? void 0 : _g.image_url) ? 'image' : 'video',
},
message: "success"
}
});
}
else if (returnBinaryData && mediaUrl) {
const imageUrl = mediaUrl;
const imageData = await this.helpers.request({
method: 'GET',
url: imageUrl,
encoding: null,
resolveWithFullResponse: true,
});
const newItem = {
json: {
code: 200,
data: response.data,
message: "success"
},
binary: {},
};
if (newItem.binary) {
newItem.binary[binaryPropertyName] = await this.helpers.prepareBinaryData(Buffer.from(imageData.body), imageUrl.split('/').pop() || 'image.png', imageData.headers['content-type']);
}
returnData.push(newItem);
}
else {
returnData.push({
json: {
code: 200,
data: response.data,
message: "success"
}
});
}
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
code: 400,
data: null,
message: error.message,
},
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.PiAPITaskStatus = PiAPITaskStatus;
//# sourceMappingURL=PiAPITaskStatus.node.js.map
;