Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n8n-4008 - Update DeepL to support more characters #3651

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/nodes-base/credentials/DeepLApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ICredentialType, INodeProperties } from 'n8n-workflow';
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class DeepLApi implements ICredentialType {
name = 'deepLApi';
Expand Down Expand Up @@ -28,4 +33,20 @@ export class DeepLApi implements ICredentialType {
default: 'pro',
},
];

authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
qs: {
auth_key: '={{$credentials.apiKey}}',
},
},
};

test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.apiPlan === "pro" ? "https://api.deepl.com/v2" : "https://api-free.deepl.com/v2" }}',
url: '/usage',
},
};
}
8 changes: 4 additions & 4 deletions packages/nodes-base/nodes/DeepL/DeepL.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ export class DeepL implements INodeType {
if (resource === 'language') {

if (operation === 'translate') {

let body: IDataObject = {};
const text = this.getNodeParameter('text', i) as string;
const translateTo = this.getNodeParameter('translateTo', i) as string;
const qs = { target_lang: translateTo, text } as IDataObject;
body = { target_lang: translateTo, 'text': text } as IDataObject;

if (additionalFields.sourceLang !== undefined) {
qs.source_lang = ['EN-GB', 'EN-US'].includes(additionalFields.sourceLang as string)
body.source_lang = ['EN-GB', 'EN-US'].includes(additionalFields.sourceLang as string)
? 'EN'
: additionalFields.sourceLang;
}

const response = await deepLApiRequest.call(this, 'GET', '/translate', {}, qs);
const response = await deepLApiRequest.call(this, 'GET', '/translate', body);
responseData.push(response.translations[0]);
}
}
Expand Down
16 changes: 7 additions & 9 deletions packages/nodes-base/nodes/DeepL/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
} from 'n8n-core';

import {
IDataObject, NodeApiError, NodeOperationError,
IDataObject,
JsonObject,
NodeApiError,
} from 'n8n-workflow';

export async function deepLApiRequest(
Expand All @@ -29,10 +31,10 @@ export async function deepLApiRequest(

const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
method,
body,
form: body,
qs,
uri: uri || `${credentials.apiPlan === 'pro' ? proApiEndpoint : freeApiEndpoint}${resource}`,
json: true,
Expand All @@ -47,13 +49,9 @@ export async function deepLApiRequest(
delete options.body;
}

const credentials = await this.getCredentials('deepLApi');

options.qs.auth_key = credentials.apiKey;

return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'deepLApi', options);

} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}