mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-13 05:47:31 -08:00
feat(DeepL Node): Add support for longer texts + Credential tests (#3651)
* Updated DeepL to send data with Body instead of QS allowing larger translations * Updated Credentials and added test
This commit is contained in:
parent
088daf952e
commit
88d6cfc07b
|
@ -1,4 +1,9 @@
|
||||||
import { ICredentialType, INodeProperties } from 'n8n-workflow';
|
import {
|
||||||
|
IAuthenticateGeneric,
|
||||||
|
ICredentialTestRequest,
|
||||||
|
ICredentialType,
|
||||||
|
INodeProperties,
|
||||||
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
export class DeepLApi implements ICredentialType {
|
export class DeepLApi implements ICredentialType {
|
||||||
name = 'deepLApi';
|
name = 'deepLApi';
|
||||||
|
@ -28,4 +33,20 @@ export class DeepLApi implements ICredentialType {
|
||||||
default: 'pro',
|
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',
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,18 +116,18 @@ export class DeepL implements INodeType {
|
||||||
if (resource === 'language') {
|
if (resource === 'language') {
|
||||||
|
|
||||||
if (operation === 'translate') {
|
if (operation === 'translate') {
|
||||||
|
let body: IDataObject = {};
|
||||||
const text = this.getNodeParameter('text', i) as string;
|
const text = this.getNodeParameter('text', i) as string;
|
||||||
const translateTo = this.getNodeParameter('translateTo', 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) {
|
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'
|
? 'EN'
|
||||||
: additionalFields.sourceLang;
|
: 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]);
|
responseData.push(response.translations[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,9 @@ import {
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IDataObject, NodeApiError, NodeOperationError,
|
IDataObject,
|
||||||
|
JsonObject,
|
||||||
|
NodeApiError,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
export async function deepLApiRequest(
|
export async function deepLApiRequest(
|
||||||
|
@ -29,10 +31,10 @@ export async function deepLApiRequest(
|
||||||
|
|
||||||
const options: OptionsWithUri = {
|
const options: OptionsWithUri = {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
},
|
},
|
||||||
method,
|
method,
|
||||||
body,
|
form: body,
|
||||||
qs,
|
qs,
|
||||||
uri: uri || `${credentials.apiPlan === 'pro' ? proApiEndpoint : freeApiEndpoint}${resource}`,
|
uri: uri || `${credentials.apiPlan === 'pro' ? proApiEndpoint : freeApiEndpoint}${resource}`,
|
||||||
json: true,
|
json: true,
|
||||||
|
@ -47,13 +49,9 @@ export async function deepLApiRequest(
|
||||||
delete options.body;
|
delete options.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
const credentials = await this.getCredentials('deepLApi');
|
return await this.helpers.requestWithAuthentication.call(this, 'deepLApi', options);
|
||||||
|
|
||||||
options.qs.auth_key = credentials.apiKey;
|
|
||||||
|
|
||||||
return await this.helpers.request!(options);
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new NodeApiError(this.getNode(), error);
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue