mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
✨ 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:
62
packages/nodes-base/nodes/DeepL/GenericFunctions.ts
Normal file
62
packages/nodes-base/nodes/DeepL/GenericFunctions.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user