Skip to content

Commit

Permalink
feat(Brandfetch Node): Update to use new API (#10877)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joffcom authored Sep 23, 2024
1 parent d74cff2 commit 08ba9a3
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 68 deletions.
23 changes: 22 additions & 1 deletion packages/nodes-base/credentials/BrandfetchApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { ICredentialType, INodeProperties } from 'n8n-workflow';
import type {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class BrandfetchApi implements ICredentialType {
name = 'brandfetchApi';
Expand All @@ -16,4 +21,20 @@ export class BrandfetchApi implements ICredentialType {
default: '',
},
];

authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
},
},
};

test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.brandfetch.io',
url: '/v2/brands/brandfetch.com',
},
};
}
87 changes: 30 additions & 57 deletions packages/nodes-base/nodes/Brandfetch/Brandfetch.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from 'n8n-workflow';
import { NodeConnectionType } from 'n8n-workflow';

import { brandfetchApiRequest } from './GenericFunctions';
import { brandfetchApiRequest, fetchAndPrepareBinaryData } from './GenericFunctions';

export class Brandfetch implements INodeType {
description: INodeTypeDescription = {
Expand Down Expand Up @@ -155,15 +155,11 @@ export class Brandfetch implements INodeType {
const responseData: INodeExecutionData[] = [];
for (let i = 0; i < length; i++) {
try {
const domain = this.getNodeParameter('domain', i) as string;
if (operation === 'logo') {
const domain = this.getNodeParameter('domain', i) as string;
const download = this.getNodeParameter('download', i);

const body: IDataObject = {
domain,
};

const response = await brandfetchApiRequest.call(this, 'POST', '/logo', body);
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);

if (download) {
const imageTypes = this.getNodeParameter('imageTypes', i) as string[];
Expand All @@ -182,92 +178,69 @@ export class Brandfetch implements INodeType {
Object.assign(newItem.binary!, items[i].binary);
}

newItem.json = response.response;
newItem.json = response.logos;

for (const imageType of imageTypes) {
for (const imageFormat of imageFormats) {
const url = response.response[imageType][
imageFormat === 'png' ? 'image' : imageFormat
] as string;

if (url !== null) {
const data = await brandfetchApiRequest.call(this, 'GET', '', {}, {}, url, {
json: false,
encoding: null,
});
const logoUrls = response.logos;

newItem.binary![`${imageType}_${imageFormat}`] =
await this.helpers.prepareBinaryData(
data as Buffer,
`${imageType}_${domain}.${imageFormat}`,
);

items[i] = newItem;
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;
}
}
}
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.response as IDataObject),
this.helpers.returnJsonArray(response.logos as IDataObject),
{ itemData: { item: i } },
);
responseData.push(...executionData);
}
}
if (operation === 'color') {
const domain = this.getNodeParameter('domain', i) as string;

const body: IDataObject = {
domain,
};

const response = await brandfetchApiRequest.call(this, 'POST', '/color', body);
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(response as IDataObject),
this.helpers.returnJsonArray(response.colors as IDataObject),
{ itemData: { item: i } },
);
responseData.push(...executionData);
}
if (operation === 'font') {
const domain = this.getNodeParameter('domain', i) as string;

const body: IDataObject = {
domain,
};

const response = await brandfetchApiRequest.call(this, 'POST', '/font', body);
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(response as IDataObject),
this.helpers.returnJsonArray(response.fonts as IDataObject),
{ itemData: { item: i } },
);
responseData.push(...executionData);
}
if (operation === 'company') {
const domain = this.getNodeParameter('domain', i) as string;

const body: IDataObject = {
domain,
};

const response = await brandfetchApiRequest.call(this, 'POST', '/company', body);
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(response as IDataObject),
this.helpers.returnJsonArray(response.company as IDataObject),
{ itemData: { item: i } },
);
responseData.push(...executionData);
}
if (operation === 'industry') {
const domain = this.getNodeParameter('domain', i) as string;

const body: IDataObject = {
domain,
};

const response = await brandfetchApiRequest.call(this, 'POST', '/industry', body);
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);

const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(response as IDataObject),
Expand Down
39 changes: 29 additions & 10 deletions packages/nodes-base/nodes/Brandfetch/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@ import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
IHttpRequestMethods,
IRequestOptions,
ILoadOptionsFunctions,
INodeExecutionData,
JsonObject,
IRequestOptions,
IHttpRequestMethods,
} 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 {
const credentials = await this.getCredentials('brandfetchApi');
let options: IRequestOptions = {
headers: {
'x-api-key': credentials.apiKey,
},
method,
method: method as IHttpRequestMethods,
qs,
body,
uri: uri || `https://api.brandfetch.io/v1${resource}`,
url: uri || `https://api.brandfetch.io/v2${resource}`,
json: true,
};

Expand All @@ -45,7 +41,11 @@ export async function brandfetchApiRequest(
delete options.qs;
}

const response = await this.helpers.request(options);
const response = await this.helpers.requestWithAuthentication.call(
this,
'brandfetchApi',
options,
);

if (response.statusCode && response.statusCode !== 200) {
throw new NodeApiError(this.getNode(), response as JsonObject);
Expand All @@ -56,3 +56,22 @@ export async function brandfetchApiRequest(
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}`,
);
}
48 changes: 48 additions & 0 deletions packages/nodes-base/nodes/Brandfetch/test/GenericFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
});

0 comments on commit 08ba9a3

Please sign in to comment.