n8n/packages/nodes-base/nodes/Bannerbear/Bannerbear.node.ts
2020-04-26 23:12:51 +02:00

160 lines
4.4 KiB
TypeScript

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
bannerbearApiRequest,
keysToSnakeCase,
} from './GenericFunctions';
import {
imageFields,
imageOperations,
} from './ImageDescription';
import {
templateFields,
templateOperations,
} from './TemplateDescription';
export class Bannerbear implements INodeType {
description: INodeTypeDescription = {
displayName: 'Bannerbear',
name: 'bannerbear',
icon: 'file:bannerbear.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Bannerbear API',
defaults: {
name: 'Bannerbear',
color: '#f9d749',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'bannerbearApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Image',
value: 'image',
},
{
name: 'Template',
value: 'template',
},
],
default: 'image',
description: 'Resource to consume.',
},
// IMAGE
...imageOperations,
...imageFields,
// TEMPLATE
...templateOperations,
...templateFields,
],
};
methods = {
loadOptions: {
// Get all the available escalation policies to display them to user so that he can
// select them easily
async getTemplates(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const templates = await bannerbearApiRequest.call(this, 'GET', '/templates');
for (const template of templates) {
const templateName = template.name;
const templateId = template.uid;
returnData.push({
name: templateName,
value: templateId,
});
}
return returnData;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
let responseData;
const qs: IDataObject = {};
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'image') {
//https://developers.bannerbear.com/#create-an-image
if (operation === 'create') {
const templateId = this.getNodeParameter('templateId', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const modifications = (this.getNodeParameter('modificationsUi', i) as IDataObject).modificationsValues as IDataObject;
const body: IDataObject = {
template: templateId,
};
if (additionalFields.webhookUrl) {
body.webhook_url = additionalFields.webhookUrl as string;
}
if (additionalFields.metadata) {
body.metadata = additionalFields.metadata as string;
}
if (modifications) {
body.modifications = keysToSnakeCase(modifications) as IDataObject[];
// delete all fields set to empty
for (const modification of body.modifications as IDataObject[]) {
for (const key of Object.keys(modification)) {
if (modification[key] === '') {
delete modification[key];
}
}
}
}
responseData = await bannerbearApiRequest.call(this, 'POST', '/images', body);
}
//https://developers.bannerbear.com/#get-a-specific-image
if (operation === 'get') {
const imageId = this.getNodeParameter('imageId', i) as string;
responseData = await bannerbearApiRequest.call(this, 'GET', `/images/${imageId}`);
}
}
if (resource === 'template') {
//https://developers.bannerbear.com/#get-a-specific-template
if (operation === 'get') {
const templateId = this.getNodeParameter('templateId', i) as string;
responseData = await bannerbearApiRequest.call(this, 'GET', `/templates/${templateId}`);
}
//https://developers.bannerbear.com/#list-templates
if (operation === 'getAll') {
responseData = await bannerbearApiRequest.call(this, 'GET', '/templates');
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData);
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}