n8n/packages/nodes-base/nodes/Brandfetch/Brandfetch.node.ts
Mutasem Aldmour ce066a160f
Remove unnessasry <br/> (#2340)
* introduce analytics

* add user survey backend

* add user survey backend

* set answers on survey submit

Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>

* change name to personalization

* lint

Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>

* N8n 2495 add personalization modal (#2280)

* update modals

* add onboarding modal

* implement questions

* introduce analytics

* simplify impl

* implement survey handling

* add personalized cateogry

* update modal behavior

* add thank you view

* handle empty cases

* rename modal

* standarize modal names

* update image, add tags to headings

* remove unused file

* remove unused interfaces

* clean up footer spacing

* introduce analytics

* refactor to fix bug

* update endpoint

* set min height

* update stories

* update naming from questions to survey

* remove spacing after core categories

* fix bug in logic

* sort nodes

* rename types

* merge with be

* rename userSurvey

* clean up rest api

* use constants for keys

* use survey keys

* clean up types

* move personalization to its own file

Co-authored-by: ahsan-virani <ahsan.virani@gmail.com>

* update parameter inputs to be multiline

* update spacing

* Survey new options (#2300)

* split up options

* fix quotes

* remove unused import

* refactor node credentials

* add user created workflow event (#2301)

* update multi params

* simplify env vars

* fix versionCli on FE

* update personalization env

* clean up node detail settings

* fix event User opened Credentials panel

* fix font sizes across modals

* clean up input spacing

* fix select modal spacing

* increase spacing

* fix input copy

* fix webhook, tab spacing, retry button

* fix button sizes

* fix button size

* add mini xlarge sizes

* fix webhook spacing

* fix nodes panel event

* fix workflow id in workflow execute event

* improve telemetry error logging

* fix config and stop process events

* add flush call on n8n stop

* ready for release

* fix input error highlighting

* revert change

* update toggle spacing

* fix delete positioning

* keep tooltip while focused

* set strict size

* increase left spacing

* fix sort icons

* remove unnessasry <br/>

* remove unnessary break

* remove unnessary margin

* clean unused functionality

* remove unnessary css

* remove duplicate tracking

* only show tooltip when hovering over label

* remove extra space

* add br

* remove extra space

* clean up commas

* clean up commas

* remove extra space

* remove extra space

* rewrite desc

* add commas

* add space

* remove extra space

* add space

* add dot

* update credentials section

* use includes

Co-authored-by: ahsan-virani <ahsan.virani@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-10-27 15:00:13 -05:00

280 lines
6.4 KiB
TypeScript

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
brandfetchApiRequest,
} from './GenericFunctions';
export class Brandfetch implements INodeType {
description: INodeTypeDescription = {
displayName: 'Brandfetch',
name: 'Brandfetch',
icon: 'file:brandfetch.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Consume Brandfetch API',
defaults: {
name: 'Brandfetch',
color: '#1f1f1f',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'brandfetchApi',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Color',
value: 'color',
description: 'Return a company\'s colors',
},
{
name: 'Company',
value: 'company',
description: 'Return a company\'s data',
},
{
name: 'Font',
value: 'font',
description: 'Return a company\'s fonts',
},
{
name: 'Industry',
value: 'industry',
description: 'Return a company\'s industry',
},
{
name: 'Logo',
value: 'logo',
description: 'Return a company\'s logo & icon',
},
],
default: 'logo',
description: 'The operation to perform',
},
// ----------------------------------
// 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',
],
},
},
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 as unknown as number;
const operation = this.getNodeParameter('operation', 0) as string;
const responseData = [];
for (let i = 0; i < length; i++) {
try {
if (operation === 'logo') {
const domain = this.getNodeParameter('domain', i) as string;
const download = this.getNodeParameter('download', i) as boolean;
const body: IDataObject = {
domain,
};
const response = await brandfetchApiRequest.call(this, 'POST', `/logo`, body);
if (download === true) {
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.response;
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 });
newItem.binary![`${imageType}_${imageFormat}`] = await this.helpers.prepareBinaryData(data, `${imageType}_${domain}.${imageFormat}`);
items[i] = newItem;
}
items[i] = newItem;
}
}
if (Object.keys(items[i].binary!).length === 0) {
delete items[i].binary;
}
} else {
responseData.push(response.response);
}
}
if (operation === 'color') {
const domain = this.getNodeParameter('domain', i) as string;
const body: IDataObject = {
domain,
};
const response = await brandfetchApiRequest.call(this, 'POST', `/color`, body);
responseData.push(response.response);
}
if (operation === 'font') {
const domain = this.getNodeParameter('domain', i) as string;
const body: IDataObject = {
domain,
};
const response = await brandfetchApiRequest.call(this, 'POST', `/font`, body);
responseData.push(response.response);
}
if (operation === 'company') {
const domain = this.getNodeParameter('domain', i) as string;
const body: IDataObject = {
domain,
};
const response = await brandfetchApiRequest.call(this, 'POST', `/company`, body);
responseData.push(response.response);
}
if (operation === 'industry') {
const domain = this.getNodeParameter('domain', i) as string;
const body: IDataObject = {
domain,
};
const response = await brandfetchApiRequest.call(this, 'POST', `/industry`, body);
responseData.push.apply(responseData, response.response);
}
} catch (error) {
if (this.continueOnFail()) {
responseData.push({ error: error.message });
continue;
}
throw error;
}
}
if (operation === 'logo' && this.getNodeParameter('download', 0) === true) {
// For file downloads the files get attached to the existing items
return this.prepareOutputData(items);
} else {
return [this.helpers.returnJsonArray(responseData)];
}
}
}