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,18 @@
|
||||
{
|
||||
"node": "n8n-nodes-base.Brandfetch",
|
||||
"nodeVersion": "1.0",
|
||||
"codexVersion": "1.0",
|
||||
"categories": ["Utility", "Sales"],
|
||||
"resources": {
|
||||
"credentialDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/credentials/brandfetch/"
|
||||
}
|
||||
],
|
||||
"primaryDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.brandfetch/"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes } from 'n8n-workflow';
|
||||
|
||||
import { brandfetchApiRequest, fetchAndPrepareBinaryData } from './GenericFunctions';
|
||||
|
||||
export class Brandfetch implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Brandfetch',
|
||||
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
|
||||
name: 'Brandfetch',
|
||||
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
||||
icon: 'file:brandfetch.png',
|
||||
group: ['output'],
|
||||
version: 1,
|
||||
subtitle: '={{$parameter["operation"]}}',
|
||||
description: 'Consume Brandfetch API',
|
||||
defaults: {
|
||||
name: 'Brandfetch',
|
||||
},
|
||||
usableAsTool: true,
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
credentials: [
|
||||
{
|
||||
name: 'brandfetchApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Color',
|
||||
value: 'color',
|
||||
description: "Return a company's colors",
|
||||
action: "Return a company's colors",
|
||||
},
|
||||
{
|
||||
name: 'Company',
|
||||
value: 'company',
|
||||
description: "Return a company's data",
|
||||
action: "Return a company's data",
|
||||
},
|
||||
{
|
||||
name: 'Font',
|
||||
value: 'font',
|
||||
description: "Return a company's fonts",
|
||||
action: "Return a company's fonts",
|
||||
},
|
||||
{
|
||||
name: 'Industry',
|
||||
value: 'industry',
|
||||
description: "Return a company's industry",
|
||||
action: "Return a company's industry",
|
||||
},
|
||||
{
|
||||
name: 'Logo',
|
||||
value: 'logo',
|
||||
description: "Return a company's logo & icon",
|
||||
action: "Return a company's logo & icon",
|
||||
},
|
||||
],
|
||||
default: 'logo',
|
||||
},
|
||||
|
||||
// ----------------------------------
|
||||
// All
|
||||
// ----------------------------------
|
||||
{
|
||||
displayName: 'Domain',
|
||||
name: 'domain',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'The domain name of the company',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Download',
|
||||
name: 'download',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['logo'],
|
||||
},
|
||||
},
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
|
||||
description: 'Name of the binary property to which to write the data of the read file',
|
||||
},
|
||||
{
|
||||
displayName: 'Image Type',
|
||||
name: 'imageTypes',
|
||||
type: 'multiOptions',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['logo'],
|
||||
download: [true],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Icon',
|
||||
value: 'icon',
|
||||
},
|
||||
{
|
||||
name: 'Logo',
|
||||
value: 'logo',
|
||||
},
|
||||
],
|
||||
default: ['logo', 'icon'],
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Image Format',
|
||||
name: 'imageFormats',
|
||||
type: 'multiOptions',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['logo'],
|
||||
download: [true],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'PNG',
|
||||
value: 'png',
|
||||
},
|
||||
{
|
||||
name: 'SVG',
|
||||
value: 'svg',
|
||||
},
|
||||
],
|
||||
default: ['png'],
|
||||
description: 'The image format in which the logo should be returned as',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const length = items.length;
|
||||
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
const responseData: INodeExecutionData[] = [];
|
||||
for (let i = 0; i < length; i++) {
|
||||
try {
|
||||
const domain = this.getNodeParameter('domain', i) as string;
|
||||
if (operation === 'logo') {
|
||||
const download = this.getNodeParameter('download', i);
|
||||
|
||||
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||
|
||||
if (download) {
|
||||
const imageTypes = this.getNodeParameter('imageTypes', i) as string[];
|
||||
|
||||
const imageFormats = this.getNodeParameter('imageFormats', i) as string[];
|
||||
|
||||
const newItem: INodeExecutionData = {
|
||||
json: {},
|
||||
binary: {},
|
||||
};
|
||||
|
||||
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.
|
||||
Object.assign(newItem.binary!, items[i].binary);
|
||||
}
|
||||
|
||||
newItem.json = response.logos;
|
||||
|
||||
for (const imageType of imageTypes) {
|
||||
for (const imageFormat of imageFormats) {
|
||||
const logoUrls = response.logos;
|
||||
|
||||
for (const logoUrl of logoUrls) {
|
||||
if (logoUrl.type !== imageType) {
|
||||
continue;
|
||||
}
|
||||
for (const logoFormats of logoUrl.formats) {
|
||||
if (logoFormats.format === imageFormat && logoFormats.src !== null) {
|
||||
await fetchAndPrepareBinaryData.call(
|
||||
this,
|
||||
imageType,
|
||||
imageFormat,
|
||||
logoFormats,
|
||||
domain,
|
||||
newItem,
|
||||
);
|
||||
items[i] = newItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Object.keys(items[i].binary!).length === 0) {
|
||||
delete items[i].binary;
|
||||
}
|
||||
} else {
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(response.logos as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
responseData.push(...executionData);
|
||||
}
|
||||
}
|
||||
if (operation === 'color') {
|
||||
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(response.colors as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
responseData.push(...executionData);
|
||||
}
|
||||
if (operation === 'font') {
|
||||
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(response.fonts as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
responseData.push(...executionData);
|
||||
}
|
||||
if (operation === 'company') {
|
||||
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(response.company as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
responseData.push(...executionData);
|
||||
}
|
||||
if (operation === 'industry') {
|
||||
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(response as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
responseData.push(...executionData);
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
responseData.push({ error: error.message, json: {}, itemIndex: i });
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'logo' && this.getNodeParameter('download', 0)) {
|
||||
// For file downloads the files get attached to the existing items
|
||||
return [items];
|
||||
} else {
|
||||
return [responseData];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
IHookFunctions,
|
||||
IHttpRequestMethods,
|
||||
IRequestOptions,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError } from 'n8n-workflow';
|
||||
|
||||
export async function brandfetchApiRequest(
|
||||
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
resource: string,
|
||||
body: any = {},
|
||||
qs: IDataObject = {},
|
||||
uri?: string,
|
||||
option: IDataObject = {},
|
||||
): Promise<any> {
|
||||
try {
|
||||
let options: IRequestOptions = {
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
url: uri || `https://api.brandfetch.io/v2${resource}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
options = Object.assign({}, options, option);
|
||||
|
||||
if (this.getNodeParameter('operation', 0) === 'logo' && options.json === false) {
|
||||
delete options.headers;
|
||||
}
|
||||
|
||||
if (!Object.keys(body as IDataObject).length) {
|
||||
delete options.body;
|
||||
}
|
||||
if (!Object.keys(qs).length) {
|
||||
delete options.qs;
|
||||
}
|
||||
|
||||
const response = await this.helpers.requestWithAuthentication.call(
|
||||
this,
|
||||
'brandfetchApi',
|
||||
options,
|
||||
);
|
||||
|
||||
if (response.statusCode && response.statusCode !== 200) {
|
||||
throw new NodeApiError(this.getNode(), response as JsonObject);
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchAndPrepareBinaryData(
|
||||
this: IExecuteFunctions,
|
||||
imageType: string,
|
||||
imageFormat: string,
|
||||
logoFormats: IDataObject,
|
||||
domain: string,
|
||||
newItem: INodeExecutionData,
|
||||
) {
|
||||
const data = await brandfetchApiRequest.call(this, 'GET', '', {}, {}, logoFormats.src as string, {
|
||||
json: false,
|
||||
encoding: null,
|
||||
});
|
||||
|
||||
newItem.binary![`${imageType}_${imageFormat}`] = await this.helpers.prepareBinaryData(
|
||||
Buffer.from(data),
|
||||
`${imageType}_${domain}.${imageFormat}`,
|
||||
);
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 555 B |
@@ -0,0 +1,48 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
IHttpRequestMethods,
|
||||
INode,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { brandfetchApiRequest } from '../GenericFunctions';
|
||||
|
||||
export const node: INode = {
|
||||
id: 'c4a5ca75-18c7-4cc8-bf7d-5d57bb7d84da',
|
||||
name: 'Brandfetch',
|
||||
type: 'n8n-nodes-base.Brandfetch',
|
||||
typeVersion: 1,
|
||||
position: [0, 0],
|
||||
parameters: {
|
||||
operation: 'font',
|
||||
domain: 'n8n.io',
|
||||
},
|
||||
};
|
||||
|
||||
describe('Brandfetch', () => {
|
||||
describe('brandfetchApiRequest', () => {
|
||||
const mockThis = {
|
||||
helpers: {
|
||||
requestWithAuthentication: jest.fn().mockResolvedValue({ statusCode: 200 }),
|
||||
},
|
||||
getNode() {
|
||||
return node;
|
||||
},
|
||||
getNodeParameter: jest.fn(),
|
||||
} as unknown as IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions;
|
||||
|
||||
it('should make an authenticated API request to Brandfetch', async () => {
|
||||
const method: IHttpRequestMethods = 'GET';
|
||||
const resource = '/brands/n8n.io';
|
||||
|
||||
await brandfetchApiRequest.call(mockThis, method, resource);
|
||||
|
||||
expect(mockThis.helpers.requestWithAuthentication).toHaveBeenCalledWith('brandfetchApi', {
|
||||
method: 'GET',
|
||||
url: 'https://api.brandfetch.io/v2/brands/n8n.io',
|
||||
json: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user