n8n-nodes-cloudconvert
Version:
A Node to send file conversion jobs to cloudconvert.com
417 lines • 17.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloudConvert = void 0;
const GenericFunctions_1 = require("./GenericFunctions");
class CloudConvert {
constructor() {
this.description = {
displayName: 'CloudConvert',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
documentationUrl: 'https://github.com/one-acre-fund/n8n-nodes-cloudconvert',
name: 'cloudConvert',
icon: 'file:cloudconvert-logo.png',
group: ['transform'],
version: 1,
description: 'A node to execute file conversion jobs on https://cloudconvert.com',
defaults: {
name: 'CloudConvert',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'cloudConvertCredentialsApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Job',
value: 'job',
description: 'Conversion Jobs',
},
{
name: 'Webhook',
value: 'webhook',
},
],
default: 'job',
required: true,
},
{
displayName: 'Create',
name: 'operation',
type: 'options',
required: true,
noDataExpression: true,
displayOptions: {
show: {
resource: ['job'],
},
},
default: 'create',
options: [
{
name: 'Create',
value: 'create',
action: 'Create job',
},
{
name: 'Get All',
value: 'list',
action: 'Get all jobs',
},
{
name: 'Get One',
value: 'get',
action: 'Get job',
},
],
},
{
displayName: 'Create',
name: 'operation',
type: 'options',
required: true,
noDataExpression: true,
displayOptions: {
show: {
resource: ['webhook'],
},
},
default: 'list',
options: [
{
name: 'Get All',
value: 'list',
action: 'Get all webhooks',
},
{
name: 'Delete',
value: 'delete',
action: 'Delete webhook',
},
],
},
{
displayName: 'You can use the CloudConvert <a target="_blank" href="https://cloudconvert.com/api/v2/jobs/builder">job builder UI</a> to construct your job definition and paste it below.',
name: 'notice',
type: 'notice',
default: '',
displayOptions: {
show: {
resource: ['job'],
operation: ['create'],
},
},
},
{
displayName: 'Job Definition (JSON)',
name: 'definition',
type: 'string',
default: '',
description: 'The full JSON definition of the conversion job',
typeOptions: {
rows: 10,
},
required: true,
displayOptions: {
show: {
resource: ['job'],
operation: ['create'],
},
},
},
{
displayName: 'ID',
name: 'jobId',
type: 'string',
default: '',
description: 'Job ID',
required: true,
displayOptions: {
show: {
resource: ['job'],
operation: ['get'],
},
},
},
{
displayName: 'Tag',
name: 'tag',
type: 'string',
default: '',
description: 'Custom tag to add to the job',
displayOptions: {
show: {
resource: ['job'],
operation: ['create'],
},
},
},
{
displayName: 'Synchronous Call?',
name: 'sync',
type: 'boolean',
default: false,
description: 'Whether to send the request synchronously (waiting for job completion)?',
displayOptions: {
show: {
resource: ['job'],
operation: ['create'],
},
},
},
{
displayName: '<strong>WARNING</strong>, synchronous calls are not recommended for heavy jobs, please consider using an asynchronous call using webhooks to be notified of job completions',
name: 'notice_async',
type: 'notice',
default: '',
displayOptions: {
show: {
sync: [true],
},
},
},
{
displayName: 'Download?',
name: 'download',
type: 'boolean',
default: false,
description: 'Whether to download result files from a sync request and add them as binary attachments?',
displayOptions: {
show: {
resource: ['job'],
operation: ['create'],
sync: [true],
},
},
},
{
displayName: '<strong>NOTE</strong>: only <strong><code>export/url</code></strong> export tasks will be retrieved. The property name of the binary attachment will be the name of the export task in the job definition (e.g. <code>export-1</code>).',
name: 'notice_async',
type: 'notice',
default: '',
displayOptions: {
show: {
download: [true],
},
},
},
{
displayName: 'Upload Input Binaries?',
name: 'upload',
type: 'boolean',
default: false,
description: 'Whether to upload any binary attachment from input items?',
displayOptions: {
show: {
resource: ['job'],
operation: ['create'],
},
},
},
{
displayName: '<strong>NOTE</strong>: If an input item has any binary attachment, it will be imported into CloudConvert by automatically adding a corresponding <strong><code>import/base64</code></strong> task to the job. The task name will be <strong><code>autoimport-propertyName</code></strong> where <strong><code>propertyName</code></strong> is the name of the binary property. Your job may use this task as an input to any conversion or export task by referencing that name.',
name: 'notice_upload',
type: 'notice',
default: '',
displayOptions: {
show: {
upload: [true],
},
},
},
{
displayName: 'Webhook URL',
name: 'webhook_url',
type: 'string',
default: '',
description: 'Webhook URL to invoke on job completion (in addition to any account-wide webhooks)',
displayOptions: {
show: {
resource: ['job'],
operation: ['create'],
sync: [false],
},
},
},
{
displayName: 'List Options',
name: 'list_options',
type: 'collection',
default: {},
description: 'Options for job list',
displayOptions: {
show: {
resource: ['job'],
operation: ['list'],
},
},
options: [
{
displayName: 'Status Filter',
name: 'status',
type: 'options',
options: [
{
name: 'All',
value: '',
},
{
name: 'Error',
value: 'error',
},
{
name: 'Finished',
value: 'finished',
},
{
name: 'Processing',
value: 'processing',
},
],
default: '',
},
{
displayName: 'Tag Filter',
name: 'tag',
type: 'string',
default: '',
},
],
},
{
displayName: 'Webhook ID',
name: 'webhook_id',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['webhook'],
operation: ['delete'],
},
},
},
{
displayName: 'Webhook URL',
name: 'webhook_url',
type: 'string',
default: '',
description: 'The result will be filtered to include only webhooks with a specific URL',
displayOptions: {
show: {
resource: ['webhook'],
operation: ['list'],
},
},
},
],
};
}
async execute() {
const items = this.getInputData();
const binaryItems = [];
let returnData = [];
let responseData;
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
for (let i = 0; i < items.length; i++) {
if (resource === 'job') {
if (operation === 'list') {
const listOptions = this.getNodeParameter('list_options', i);
responseData = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
url: `/v2/jobs`,
qs: {
...(listOptions.tag && { 'filter[tag]': listOptions.tag }),
...(listOptions.status && { 'filter[status]': listOptions.status }),
},
});
returnData = returnData.concat(responseData || []);
}
if (operation === 'get') {
const jobId = this.getNodeParameter('jobId', i, null);
responseData = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
url: `/v2/jobs/${jobId}`,
});
returnData = returnData.concat(responseData);
}
if (operation === 'delete') {
const jobId = this.getNodeParameter('jobId', i, null);
responseData = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
method: 'DELETE',
url: `/v2/jobs/${jobId}`,
});
returnData = returnData.concat([{ success: true }]);
}
if (operation === 'create') {
const definition = this.getNodeParameter('definition', i, '');
const tag = this.getNodeParameter('tag', i, '');
const sync = this.getNodeParameter('sync', i, false);
const download = this.getNodeParameter('download', i, false);
const body = {
...JSON.parse(definition),
...(tag ? { tag } : {}),
};
const attachments = items[i].binary;
if (attachments) {
for (const binaryPropertyName of Object.keys(attachments)) {
const binaryData = attachments[binaryPropertyName];
body.tasks['autoimport-' + binaryPropertyName] = {
operation: 'import/base64',
file: binaryData.data,
filename: binaryData.fileName,
};
}
}
responseData = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
method: 'POST',
url: `/v2/jobs`,
body,
sync,
download,
});
if (sync && download && responseData) {
const binaryItem = {
json: responseData,
binary: {},
};
await GenericFunctions_1.downloadExports.call(this, responseData, binaryItem);
binaryItems.push(binaryItem);
}
returnData = returnData.concat(responseData);
}
}
if (resource === 'webhook') {
if (operation === 'list') {
const url = this.getNodeParameter('webhook_url', i, '');
responseData = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
url: `/v2/users/me/webhooks`,
qs: {
...(url && { 'filter[url]': url }),
},
});
returnData = returnData.concat(responseData || []);
}
if (operation === 'delete') {
const webhookId = this.getNodeParameter('webhook_id', i, '');
responseData = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
method: 'delete',
url: `/v2/webhooks/${webhookId}`,
});
returnData = returnData.concat([{ success: true }]);
}
}
}
return binaryItems.length > 0 ? [binaryItems] : [this.helpers.returnJsonArray(returnData)];
}
}
exports.CloudConvert = CloudConvert;
//# sourceMappingURL=CloudConvert.node.js.map