Add DeepL Node (#1551)

* Created the node for DeepL translator tool

Still missing a few additional fields but functionality is ok for most cases.

* Added optional fields for Deepl and separated description to a separate file

* Fixed linting issue

* 🎨 Replace PNG with SVG icon

* 💄 Adjust style to codebase conventions

* 🔨 Refactor types

*  Simplify error handling

*  Add always open edit window for text field

* ✏️ Edit descriptions in text operations

*  Fix source language for English

EN-GB and EN-US are not supported as source languages, but EN is.

* 💄 Apply cosmetic changes

*  Small improvement

*  Remove not needed Authentication selection

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Omar Ajoue 2021-03-18 23:34:15 +01:00 committed by GitHub
parent b879755f0b
commit 30d83d0bc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 336 additions and 0 deletions

View file

@ -0,0 +1,15 @@
import { ICredentialType, NodePropertyTypes } from 'n8n-workflow';
export class DeepLApi implements ICredentialType {
name = 'deepLApi';
displayName = 'DeepL API';
documentationUrl = 'deepL';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,131 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
deepLApiRequest,
} from './GenericFunctions';
import {
textOperations
} from './TextDescription';
export class DeepL implements INodeType {
description: INodeTypeDescription = {
displayName: 'DeepL',
name: 'deepL',
icon: 'file:deepl.svg',
group: ['input', 'output'],
version: 1,
description: 'Translate data using DeepL',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
defaults: {
name: 'DeepL',
color: '#0f2b46',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'deepLApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Language',
value: 'language',
},
],
default: 'language',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'language',
],
},
},
options: [
{
name: 'Translate',
value: 'translate',
description: 'Translate data',
},
],
default: 'translate',
description: 'The operation to perform',
},
...textOperations,
],
};
methods = {
loadOptions: {
async getLanguages(this: ILoadOptionsFunctions) {
const returnData: INodePropertyOptions[] = [];
const languages = await deepLApiRequest.call(this, 'GET', '/languages', {}, { type: 'target' });
for (const language of languages) {
returnData.push({
name: language.name,
value: language.language,
});
}
return returnData;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const length = items.length;
const responseData = [];
for (let i = 0; i < length; i++) {
const resource = this.getNodeParameter('resource', i) as string;
const operation = this.getNodeParameter('operation', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (resource === 'language') {
if (operation === 'translate') {
const text = this.getNodeParameter('text', i) as string;
const translateTo = this.getNodeParameter('translateTo', i) as string;
const qs = { target_lang: translateTo, text } as IDataObject;
if (additionalFields.sourceLang !== undefined) {
qs.source_lang = ['EN-GB', 'EN-US'].includes(additionalFields.sourceLang as string)
? 'EN'
: additionalFields.sourceLang;
}
const response = await deepLApiRequest.call(this, 'GET', '/translate', {}, qs);
responseData.push(response.translations[0]);
}
}
}
return [this.helpers.returnJsonArray(responseData)];
}
}

View file

@ -0,0 +1,62 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function deepLApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
resource: string,
body: IDataObject = {},
qs: IDataObject = {},
uri?: string,
headers: IDataObject = {},
) {
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: uri || `https://api.deepl.com/v2${resource}`,
json: true,
};
try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
const credentials = this.getCredentials('deepLApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.qs.auth_key = credentials.apiKey;
return await this.helpers.request!(options);
} catch (error) {
if (error?.response?.body?.message) {
// Try to return the error prettier
throw new Error(`DeepL error response [${error.statusCode}]: ${error.response.body.message}`);
}
throw error;
}
}

View file

@ -0,0 +1,125 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const textOperations = [
{
displayName: 'Text',
name: 'text',
type: 'string',
default: '',
description: 'Input text to translate.',
required: true,
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
operation: [
'translate',
],
},
},
},
{
displayName: 'Target Language',
name: 'translateTo',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getLanguages',
},
default: '',
description: 'Language to translate to.',
required: true,
displayOptions: {
show: {
operation: [
'translate',
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
options: [
{
displayName: 'Source Language',
name: 'sourceLang',
type: 'options',
default: '',
description: 'Language to translate from.',
typeOptions: {
loadOptionsMethod: 'getLanguages',
},
},
{
displayName: 'Split Sentences',
name: 'splitSentences',
type: 'options',
default: '1',
description: 'How the translation engine should split sentences.',
options: [
{
name: 'Interpunction Only',
value: 'nonewlines',
description: 'Split text on interpunction only, ignoring newlines.',
},
{
name: 'No Splitting',
value: '0',
description: 'Treat all text as a single sentence.',
},
{
name: 'On Punctuation and Newlines',
value: '1',
description: 'Split text on interpunction and newlines.',
},
],
},
{
displayName: 'Preserve Formatting',
name: 'preserveFormatting',
type: 'options',
default: '0',
description: 'Whether the translation engine should respect the original formatting, even if it would usually correct some aspects.',
options: [
{
name: 'Apply corrections',
value: '0',
description: 'Fix punctuation at the beginning and end of sentences and fixes lower/upper caseing at the beginning.',
},
{
name: 'Do not correct',
value: '1',
description: 'Keep text as similar as possible to the original.',
},
],
},
{
displayName: 'Formality',
name: 'formality',
type: 'options',
default: 'default',
description: 'How formal or informal the target text should be. May not be supported with all languages.',
options: [
{
name: 'Formal',
value: 'more',
},
{
name: 'Informal',
value: 'less',
},
{
name: 'Neutral',
value: 'default',
},
],
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1 @@
<svg enable-background="new 0 0 64 64" height="64" viewBox="0 0 64 64" width="64" xmlns="http://www.w3.org/2000/svg"><path d="m2.7 28.8-16.3 9.3c-1.1.6-2.5.6-3.6 0l-16.3-9.3c-1.1-.6-1.8-1.8-1.8-3.1v-19c0-1.3.7-2.5 1.8-3.1l28.1-16.2v11.5l8.1 4.7c1.1.6 1.8 1.8 1.8 3.1v19c.1 1.3-.7 2.5-1.8 3.1zm-20.6-20.9c0-1.9-1.5-3.4-3.4-3.4s-3.4 1.5-3.4 3.4 1.5 3.4 3.4 3.4c.9 0 1.6-.3 2.2-.8l6 3.4c.3-.7.7-1.4 1.1-2l-6-3.4c.1-.2.1-.4.1-.6zm10.4 4.5c-1.9 0-3.4 1.5-3.4 3.4 0 .2 0 .4.1.6l-8.2 4.7c-.6-.5-1.4-.8-2.2-.8-1.9 0-3.4 1.5-3.4 3.4s1.5 3.4 3.4 3.4 3.4-1.5 3.4-3.4c0-.2 0-.4-.1-.6l8.2-4.7c.6.5 1.4.9 2.3.9 1.9 0 3.4-1.5 3.4-3.4-.1-1.9-1.6-3.5-3.5-3.5z" fill="#042b48" transform="matrix(1.25 0 0 -1.25 51.203755 48.267815)"/></svg>

After

Width:  |  Height:  |  Size: 721 B

View file

@ -65,6 +65,7 @@
"dist/credentials/CustomerIoApi.credentials.js", "dist/credentials/CustomerIoApi.credentials.js",
"dist/credentials/S3.credentials.js", "dist/credentials/S3.credentials.js",
"dist/credentials/CrateDb.credentials.js", "dist/credentials/CrateDb.credentials.js",
"dist/credentials/DeepLApi.credentials.js",
"dist/credentials/DemioApi.credentials.js", "dist/credentials/DemioApi.credentials.js",
"dist/credentials/DiscourseApi.credentials.js", "dist/credentials/DiscourseApi.credentials.js",
"dist/credentials/DisqusApi.credentials.js", "dist/credentials/DisqusApi.credentials.js",
@ -319,6 +320,7 @@
"dist/nodes/CustomerIo/CustomerIo.node.js", "dist/nodes/CustomerIo/CustomerIo.node.js",
"dist/nodes/CustomerIo/CustomerIoTrigger.node.js", "dist/nodes/CustomerIo/CustomerIoTrigger.node.js",
"dist/nodes/DateTime.node.js", "dist/nodes/DateTime.node.js",
"dist/nodes/DeepL/DeepL.node.js",
"dist/nodes/Demio/Demio.node.js", "dist/nodes/Demio/Demio.node.js",
"dist/nodes/Discord/Discord.node.js", "dist/nodes/Discord/Discord.node.js",
"dist/nodes/Discourse/Discourse.node.js", "dist/nodes/Discourse/Discourse.node.js",