2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2023-01-27 03:22:44 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { brandfetchApiRequest } from './GenericFunctions';
|
2020-12-15 07:56:28 -08:00
|
|
|
|
|
|
|
export class Brandfetch implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Brandfetch',
|
2023-01-02 00:22:33 -08:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
|
2020-12-15 07:56:28 -08:00
|
|
|
name: 'Brandfetch',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-12-15 07:56:28 -08:00
|
|
|
icon: 'file:brandfetch.png',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"]}}',
|
|
|
|
description: 'Consume Brandfetch API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Brandfetch',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'brandfetchApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-12-15 07:56:28 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Color',
|
|
|
|
value: 'color',
|
2022-08-01 13:47:55 -07:00
|
|
|
description: "Return a company's colors",
|
|
|
|
action: "Return a company's colors",
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Company',
|
|
|
|
value: 'company',
|
2022-08-01 13:47:55 -07:00
|
|
|
description: "Return a company's data",
|
|
|
|
action: "Return a company's data",
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Font',
|
|
|
|
value: 'font',
|
2022-08-01 13:47:55 -07:00
|
|
|
description: "Return a company's fonts",
|
|
|
|
action: "Return a company's fonts",
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Industry',
|
|
|
|
value: 'industry',
|
2022-08-01 13:47:55 -07:00
|
|
|
description: "Return a company's industry",
|
|
|
|
action: "Return a company's industry",
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Logo',
|
|
|
|
value: 'logo',
|
2022-08-01 13:47:55 -07:00
|
|
|
description: "Return a company's logo & icon",
|
|
|
|
action: "Return a company's logo & icon",
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'logo',
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// All
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Domain',
|
|
|
|
name: 'domain',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The domain name of the company',
|
2020-12-15 07:56:28 -08:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Download',
|
|
|
|
name: 'download',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['logo'],
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
},
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Name of the binary property to which to write the data of the read file',
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Image Type',
|
|
|
|
name: 'imageTypes',
|
|
|
|
type: 'multiOptions',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['logo'],
|
|
|
|
download: [true],
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Icon',
|
|
|
|
value: 'icon',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Logo',
|
|
|
|
value: 'logo',
|
|
|
|
},
|
|
|
|
],
|
2022-08-01 13:47:55 -07:00
|
|
|
default: ['logo', 'icon'],
|
2020-12-15 07:56:28 -08:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Image Format',
|
|
|
|
name: 'imageFormats',
|
|
|
|
type: 'multiOptions',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['logo'],
|
|
|
|
download: [true],
|
2020-12-15 07:56:28 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'PNG',
|
|
|
|
value: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SVG',
|
|
|
|
value: 'svg',
|
|
|
|
},
|
|
|
|
],
|
2022-08-01 13:47:55 -07:00
|
|
|
default: ['png'],
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The image format in which the logo should be returned as',
|
2020-12-15 07:56:28 -08:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2022-08-30 08:55:33 -07:00
|
|
|
const responseData: INodeExecutionData[] = [];
|
2020-12-15 07:56:28 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (operation === 'logo') {
|
|
|
|
const domain = this.getNodeParameter('domain', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', i);
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
domain,
|
|
|
|
};
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
const response = await brandfetchApiRequest.call(this, 'POST', '/logo', body);
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (download) {
|
2021-07-19 23:58:54 -07:00
|
|
|
const imageTypes = this.getNodeParameter('imageTypes', i) as string[];
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const imageFormats = this.getNodeParameter('imageFormats', i) as string[];
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: {},
|
|
|
|
binary: {},
|
|
|
|
};
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (items[i].binary !== undefined) {
|
|
|
|
// Create a shallow copy of the binary data so that the old
|
|
|
|
// data references which do not get changed still stay behind
|
|
|
|
// but the incoming data does not get changed.
|
2022-08-30 08:55:33 -07:00
|
|
|
Object.assign(newItem.binary!, items[i].binary);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
newItem.json = response.response;
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const imageType of imageTypes) {
|
|
|
|
for (const imageFormat of imageFormats) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const url = response.response[imageType][
|
|
|
|
imageFormat === 'png' ? 'image' : imageFormat
|
|
|
|
] as string;
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (url !== null) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const data = await brandfetchApiRequest.call(this, 'GET', '', {}, {}, url, {
|
|
|
|
json: false,
|
|
|
|
encoding: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
newItem.binary![`${imageType}_${imageFormat}`] =
|
|
|
|
await this.helpers.prepareBinaryData(
|
2023-02-27 19:39:43 -08:00
|
|
|
data as Buffer,
|
2022-08-01 13:47:55 -07:00
|
|
|
`${imageType}_${domain}.${imageFormat}`,
|
|
|
|
);
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
items[i] = newItem;
|
|
|
|
}
|
2020-12-15 07:56:28 -08:00
|
|
|
items[i] = newItem;
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Object.keys(items[i].binary!).length === 0) {
|
|
|
|
delete items[i].binary;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(response.response as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
responseData.push(...executionData);
|
2020-12-15 07:56:28 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'color') {
|
|
|
|
const domain = this.getNodeParameter('domain', i) as string;
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
domain,
|
|
|
|
};
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
const response = await brandfetchApiRequest.call(this, 'POST', '/color', body);
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(response as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
responseData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'font') {
|
|
|
|
const domain = this.getNodeParameter('domain', i) as string;
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
domain,
|
|
|
|
};
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
const response = await brandfetchApiRequest.call(this, 'POST', '/font', body);
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(response as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
responseData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'company') {
|
|
|
|
const domain = this.getNodeParameter('domain', i) as string;
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
domain,
|
|
|
|
};
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
const response = await brandfetchApiRequest.call(this, 'POST', '/company', body);
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(response as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
responseData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'industry') {
|
|
|
|
const domain = this.getNodeParameter('domain', i) as string;
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
domain,
|
|
|
|
};
|
2020-12-15 07:56:28 -08:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
const response = await brandfetchApiRequest.call(this, 'POST', '/industry', body);
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(response as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
responseData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData.push({ error: error.message, json: {}, itemIndex: i });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-12-15 07:56:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (operation === 'logo' && this.getNodeParameter('download', 0)) {
|
2020-12-15 07:56:28 -08:00
|
|
|
// For file downloads the files get attached to the existing items
|
|
|
|
return this.prepareOutputData(items);
|
|
|
|
} else {
|
2022-08-30 08:55:33 -07:00
|
|
|
return [responseData];
|
2020-12-15 07:56:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|