-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
82 lines (65 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const got = require('got');
const languages = require('./languages.js');
function translate(text, options, gotopts) {
options = options || {};
gotopts = gotopts || {};
let error;
for (const lang of [options.from, options.to]) {
if (lang && !languages.isSupported(lang)) {
error = new Error('The language \'' + lang + '\' is not supported');
error.code = 400;
}
}
options.apiKey = options.apiKey || '';
if (options.apiKey === '') {
error = new Error('Please provide an API Key');
error.code = 400;
}
if (error) {
return new Promise((resolve, reject) => {
reject(error);
});
}
options.from = options.from || 'auto';
options.to = options.to || 'en';
options.from = languages.getCode(options.from);
options.to = languages.getCode(options.to);
options.domain = options.domain || 'https://api-free.deepl.com';
let url = options.domain + '/v2/translate?auth_key=' + options.apiKey + '&text=' + encodeURIComponent(text) + '&target_lang=' + options.to;
if (options.from !== undefined && options.from !== 'auto') {
url += '&source_lang=' + options.from;
}
if (gotopts.headers === undefined) {
gotopts.headers = {};
}
gotopts.headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
return got.post(url, gotopts).then(response => {
const result = {
text: '',
from: {
language: {
didYouMean: false,
iso: '',
},
},
raw: '',
};
if (options.raw) {
result.raw = response.body;
}
const json = JSON.parse(response.body)?.translations?.[0];
if (json === undefined) {
return result;
}
result.text = json.text;
result.from.language.didYouMean = json.detected_source_language.toString().toLowerCase() !== options.from.toLowerCase();
result.from.language.iso = languages.getCode(json.detected_source_language.toLowerCase());
return result;
}).catch(error => {
error.message += `\nUrl: ${url}`;
error.code = error.statusCode !== undefined && error.statusCode !== 200 ? 'BAD_REQUEST' : 'BAD_NETWORK';
throw error;
});
}
module.exports = translate;
module.exports.languages = languages;