mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-17 01:54:06 -08:00
62 lines
1.3 KiB
TypeScript
62 lines
1.3 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IExecuteSingleFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject, NodeApiError, NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
export async function deepLApiRequest(
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
method: string,
|
|
resource: string,
|
|
body: IDataObject = {},
|
|
qs: IDataObject = {},
|
|
uri?: string,
|
|
headers: IDataObject = {},
|
|
) {
|
|
|
|
const proApiEndpoint = 'https://api.deepl.com/v2';
|
|
const freeApiEndpoint = 'https://api-free.deepl.com/v2';
|
|
|
|
const credentials = this.getCredentials('deepLApi');
|
|
|
|
if (credentials === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
|
}
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
method,
|
|
body,
|
|
qs,
|
|
uri: uri || `${credentials.apiPlan === 'pro' ? proApiEndpoint : freeApiEndpoint}${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;
|
|
}
|
|
|
|
options.qs.auth_key = credentials.apiKey;
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|