feat(Brandfetch Node): Update to use new API (#10877)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions

This commit is contained in:
Jon 2024-09-23 21:19:16 +01:00 committed by GitHub
parent d74cff2030
commit 08ba9a36a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 130 additions and 69 deletions

View file

@ -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';
@ -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',
},
};
}

View file

@ -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 = {
@ -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[];
@ -182,29 +178,30 @@ 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;
const logoUrls = response.logos;
if (url !== null) {
const data = await brandfetchApiRequest.call(this, 'GET', '', {}, {}, url, {
json: false,
encoding: null,
});
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) {
@ -212,62 +209,38 @@ export class Brandfetch implements INodeType {
}
} 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),

View file

@ -2,10 +2,11 @@ import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
JsonObject,
IRequestOptions,
IHttpRequestMethods,
IRequestOptions,
ILoadOptionsFunctions,
INodeExecutionData,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
@ -13,22 +14,17 @@ 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,
};
@ -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);
@ -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}`,
);
}

View file

@ -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,
});
});
});
});