first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
INodePropertyOptions,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes } 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',
|
||||
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
||||
icon: 'file:bannerbear.png',
|
||||
group: ['output'],
|
||||
version: 1,
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Consume Bannerbear API',
|
||||
defaults: {
|
||||
name: 'Bannerbear',
|
||||
},
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
credentials: [
|
||||
{
|
||||
name: 'bannerbearApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Image',
|
||||
value: 'image',
|
||||
},
|
||||
{
|
||||
name: 'Template',
|
||||
value: 'template',
|
||||
},
|
||||
],
|
||||
default: 'image',
|
||||
},
|
||||
// IMAGE
|
||||
...imageOperations,
|
||||
...imageFields,
|
||||
// TEMPLATE
|
||||
...templateOperations,
|
||||
...templateFields,
|
||||
],
|
||||
};
|
||||
|
||||
methods = {
|
||||
loadOptions: {
|
||||
// Get all the available templates to display them to user so that they 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;
|
||||
},
|
||||
|
||||
// Get all the available modifications to display them to user so that they can
|
||||
// select them easily
|
||||
async getModificationNames(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const templateId = this.getCurrentNodeParameter('templateId');
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const { available_modifications } = await bannerbearApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
`/templates/${templateId}`,
|
||||
);
|
||||
for (const modification of available_modifications) {
|
||||
const modificationName = modification.name;
|
||||
const modificationId = modification.name;
|
||||
returnData.push({
|
||||
name: modificationName,
|
||||
value: modificationId,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: IDataObject[] = [];
|
||||
const length = items.length;
|
||||
let responseData;
|
||||
const resource = this.getNodeParameter('resource', 0);
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
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);
|
||||
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);
|
||||
// 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);
|
||||
if (additionalFields.waitForImage && responseData.status !== 'completed') {
|
||||
let maxTries = (additionalFields.waitForImageMaxTries as number) || 3;
|
||||
|
||||
const promise = async (uid: string) => {
|
||||
let data: IDataObject = {};
|
||||
return await new Promise((resolve, reject) => {
|
||||
const timeout = setInterval(async () => {
|
||||
data = await bannerbearApiRequest.call(this, 'GET', `/images/${uid}`);
|
||||
|
||||
if (data.status === 'completed') {
|
||||
clearInterval(timeout);
|
||||
resolve(data);
|
||||
}
|
||||
if (--maxTries === 0) {
|
||||
clearInterval(timeout);
|
||||
reject(new Error('Image did not finish processing after multiple tries.'));
|
||||
}
|
||||
}, 2000);
|
||||
});
|
||||
};
|
||||
|
||||
responseData = await promise(responseData.uid as string);
|
||||
}
|
||||
}
|
||||
//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 as IDataObject);
|
||||
}
|
||||
}
|
||||
return [this.helpers.returnJsonArray(returnData)];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user