2021-03-18 15:34:15 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2021-03-18 15:34:15 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function deepLApiRequest(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
|
|
|
) {
|
|
|
|
|
2021-05-30 10:33:43 -07:00
|
|
|
const proApiEndpoint = 'https://api.deepl.com/v2';
|
|
|
|
const freeApiEndpoint = 'https://api-free.deepl.com/v2';
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('deepLApi');
|
2021-05-30 10:33:43 -07:00
|
|
|
|
2021-03-18 15:34:15 -07:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2021-05-30 10:33:43 -07:00
|
|
|
uri: uri || `${credentials.apiPlan === 'pro' ? proApiEndpoint : freeApiEndpoint}${resource}`,
|
2021-03-18 15:34:15 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('deepLApi');
|
|
|
|
|
2021-03-18 15:34:15 -07:00
|
|
|
options.qs.auth_key = credentials.apiKey;
|
|
|
|
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-03-18 15:34:15 -07:00
|
|
|
}
|
|
|
|
}
|