mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
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
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:
parent
d74cff2030
commit
08ba9a36a4
|
@ -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 {
|
export class BrandfetchApi implements ICredentialType {
|
||||||
name = 'brandfetchApi';
|
name = 'brandfetchApi';
|
||||||
|
@ -16,4 +21,20 @@ export class BrandfetchApi implements ICredentialType {
|
||||||
default: '',
|
default: '',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
authenticate: IAuthenticateGeneric = {
|
||||||
|
type: 'generic',
|
||||||
|
properties: {
|
||||||
|
headers: {
|
||||||
|
Authorization: '=Bearer {{$credentials.apiKey}}',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
test: ICredentialTestRequest = {
|
||||||
|
request: {
|
||||||
|
baseURL: 'https://api.brandfetch.io',
|
||||||
|
url: '/v2/brands/brandfetch.com',
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import type {
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
import { NodeConnectionType } from 'n8n-workflow';
|
import { NodeConnectionType } from 'n8n-workflow';
|
||||||
|
|
||||||
import { brandfetchApiRequest } from './GenericFunctions';
|
import { brandfetchApiRequest, fetchAndPrepareBinaryData } from './GenericFunctions';
|
||||||
|
|
||||||
export class Brandfetch implements INodeType {
|
export class Brandfetch implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -155,15 +155,11 @@ export class Brandfetch implements INodeType {
|
||||||
const responseData: INodeExecutionData[] = [];
|
const responseData: INodeExecutionData[] = [];
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
try {
|
try {
|
||||||
|
const domain = this.getNodeParameter('domain', i) as string;
|
||||||
if (operation === 'logo') {
|
if (operation === 'logo') {
|
||||||
const domain = this.getNodeParameter('domain', i) as string;
|
|
||||||
const download = this.getNodeParameter('download', i);
|
const download = this.getNodeParameter('download', i);
|
||||||
|
|
||||||
const body: IDataObject = {
|
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||||
domain,
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await brandfetchApiRequest.call(this, 'POST', '/logo', body);
|
|
||||||
|
|
||||||
if (download) {
|
if (download) {
|
||||||
const imageTypes = this.getNodeParameter('imageTypes', i) as string[];
|
const imageTypes = this.getNodeParameter('imageTypes', i) as string[];
|
||||||
|
@ -182,29 +178,30 @@ export class Brandfetch implements INodeType {
|
||||||
Object.assign(newItem.binary!, items[i].binary);
|
Object.assign(newItem.binary!, items[i].binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
newItem.json = response.response;
|
newItem.json = response.logos;
|
||||||
|
|
||||||
for (const imageType of imageTypes) {
|
for (const imageType of imageTypes) {
|
||||||
for (const imageFormat of imageFormats) {
|
for (const imageFormat of imageFormats) {
|
||||||
const url = response.response[imageType][
|
const logoUrls = response.logos;
|
||||||
imageFormat === 'png' ? 'image' : imageFormat
|
|
||||||
] as string;
|
|
||||||
|
|
||||||
if (url !== null) {
|
for (const logoUrl of logoUrls) {
|
||||||
const data = await brandfetchApiRequest.call(this, 'GET', '', {}, {}, url, {
|
if (logoUrl.type !== imageType) {
|
||||||
json: false,
|
continue;
|
||||||
encoding: null,
|
}
|
||||||
});
|
for (const logoFormats of logoUrl.formats) {
|
||||||
|
if (logoFormats.format === imageFormat && logoFormats.src !== null) {
|
||||||
newItem.binary![`${imageType}_${imageFormat}`] =
|
await fetchAndPrepareBinaryData.call(
|
||||||
await this.helpers.prepareBinaryData(
|
this,
|
||||||
data as Buffer,
|
imageType,
|
||||||
`${imageType}_${domain}.${imageFormat}`,
|
imageFormat,
|
||||||
);
|
logoFormats,
|
||||||
|
domain,
|
||||||
items[i] = newItem;
|
newItem,
|
||||||
|
);
|
||||||
|
items[i] = newItem;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
items[i] = newItem;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Object.keys(items[i].binary!).length === 0) {
|
if (Object.keys(items[i].binary!).length === 0) {
|
||||||
|
@ -212,62 +209,38 @@ export class Brandfetch implements INodeType {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const executionData = this.helpers.constructExecutionMetaData(
|
const executionData = this.helpers.constructExecutionMetaData(
|
||||||
this.helpers.returnJsonArray(response.response as IDataObject),
|
this.helpers.returnJsonArray(response.logos as IDataObject),
|
||||||
{ itemData: { item: i } },
|
{ itemData: { item: i } },
|
||||||
);
|
);
|
||||||
responseData.push(...executionData);
|
responseData.push(...executionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (operation === 'color') {
|
if (operation === 'color') {
|
||||||
const domain = this.getNodeParameter('domain', i) as string;
|
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||||
|
|
||||||
const body: IDataObject = {
|
|
||||||
domain,
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await brandfetchApiRequest.call(this, 'POST', '/color', body);
|
|
||||||
const executionData = this.helpers.constructExecutionMetaData(
|
const executionData = this.helpers.constructExecutionMetaData(
|
||||||
this.helpers.returnJsonArray(response as IDataObject),
|
this.helpers.returnJsonArray(response.colors as IDataObject),
|
||||||
{ itemData: { item: i } },
|
{ itemData: { item: i } },
|
||||||
);
|
);
|
||||||
responseData.push(...executionData);
|
responseData.push(...executionData);
|
||||||
}
|
}
|
||||||
if (operation === 'font') {
|
if (operation === 'font') {
|
||||||
const domain = this.getNodeParameter('domain', i) as string;
|
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||||
|
|
||||||
const body: IDataObject = {
|
|
||||||
domain,
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await brandfetchApiRequest.call(this, 'POST', '/font', body);
|
|
||||||
const executionData = this.helpers.constructExecutionMetaData(
|
const executionData = this.helpers.constructExecutionMetaData(
|
||||||
this.helpers.returnJsonArray(response as IDataObject),
|
this.helpers.returnJsonArray(response.fonts as IDataObject),
|
||||||
{ itemData: { item: i } },
|
{ itemData: { item: i } },
|
||||||
);
|
);
|
||||||
responseData.push(...executionData);
|
responseData.push(...executionData);
|
||||||
}
|
}
|
||||||
if (operation === 'company') {
|
if (operation === 'company') {
|
||||||
const domain = this.getNodeParameter('domain', i) as string;
|
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||||
|
|
||||||
const body: IDataObject = {
|
|
||||||
domain,
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await brandfetchApiRequest.call(this, 'POST', '/company', body);
|
|
||||||
const executionData = this.helpers.constructExecutionMetaData(
|
const executionData = this.helpers.constructExecutionMetaData(
|
||||||
this.helpers.returnJsonArray(response as IDataObject),
|
this.helpers.returnJsonArray(response.company as IDataObject),
|
||||||
{ itemData: { item: i } },
|
{ itemData: { item: i } },
|
||||||
);
|
);
|
||||||
responseData.push(...executionData);
|
responseData.push(...executionData);
|
||||||
}
|
}
|
||||||
if (operation === 'industry') {
|
if (operation === 'industry') {
|
||||||
const domain = this.getNodeParameter('domain', i) as string;
|
const response = await brandfetchApiRequest.call(this, 'GET', `/brands/${domain}`);
|
||||||
|
|
||||||
const body: IDataObject = {
|
|
||||||
domain,
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await brandfetchApiRequest.call(this, 'POST', '/industry', body);
|
|
||||||
|
|
||||||
const executionData = this.helpers.constructExecutionMetaData(
|
const executionData = this.helpers.constructExecutionMetaData(
|
||||||
this.helpers.returnJsonArray(response as IDataObject),
|
this.helpers.returnJsonArray(response as IDataObject),
|
||||||
|
|
|
@ -2,10 +2,11 @@ import type {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
IHookFunctions,
|
IHookFunctions,
|
||||||
ILoadOptionsFunctions,
|
|
||||||
JsonObject,
|
|
||||||
IRequestOptions,
|
|
||||||
IHttpRequestMethods,
|
IHttpRequestMethods,
|
||||||
|
IRequestOptions,
|
||||||
|
ILoadOptionsFunctions,
|
||||||
|
INodeExecutionData,
|
||||||
|
JsonObject,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
import { NodeApiError } from 'n8n-workflow';
|
import { NodeApiError } from 'n8n-workflow';
|
||||||
|
|
||||||
|
@ -13,22 +14,17 @@ export async function brandfetchApiRequest(
|
||||||
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
||||||
method: IHttpRequestMethods,
|
method: IHttpRequestMethods,
|
||||||
resource: string,
|
resource: string,
|
||||||
|
|
||||||
body: any = {},
|
body: any = {},
|
||||||
qs: IDataObject = {},
|
qs: IDataObject = {},
|
||||||
uri?: string,
|
uri?: string,
|
||||||
option: IDataObject = {},
|
option: IDataObject = {},
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const credentials = await this.getCredentials('brandfetchApi');
|
|
||||||
let options: IRequestOptions = {
|
let options: IRequestOptions = {
|
||||||
headers: {
|
method: method as IHttpRequestMethods,
|
||||||
'x-api-key': credentials.apiKey,
|
|
||||||
},
|
|
||||||
method,
|
|
||||||
qs,
|
qs,
|
||||||
body,
|
body,
|
||||||
uri: uri || `https://api.brandfetch.io/v1${resource}`,
|
url: uri || `https://api.brandfetch.io/v2${resource}`,
|
||||||
json: true,
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -45,7 +41,11 @@ export async function brandfetchApiRequest(
|
||||||
delete options.qs;
|
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) {
|
if (response.statusCode && response.statusCode !== 200) {
|
||||||
throw new NodeApiError(this.getNode(), response as JsonObject);
|
throw new NodeApiError(this.getNode(), response as JsonObject);
|
||||||
|
@ -56,3 +56,22 @@ export async function brandfetchApiRequest(
|
||||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
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}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue