-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (95 loc) · 2.28 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const http = require('http');
const { URL } = require('url');
const assert = require('assert');
const get = o =>
new Promise(done => http.get(o, done));
const readStream = stream => {
const buffer = [];
return new Promise((resolve, reject) => {
stream
.on('error', reject)
.on('data', chunk => buffer.push(chunk))
.on('end', () => resolve(Buffer.concat(buffer)))
});
};
const parseURL = (url, query) => {
const o = new URL(url);
Object.keys(query).forEach(k =>
o.searchParams.append(k, query[k]));
return o;
}
const ERROR_CODES = {
// 0: '正常',
20: "要翻译的文本过长",
30: "无法进行有效的翻译",
40: "不支持的语言类型",
50: "无效的key",
60: "无词典结果,仅在获取词典结果生效",
};
const parseJSON = buf => {
var data, error = new Error();
try {
data = JSON.parse(buf);
} catch (e) {
// console.log('[youdao-fanyi] parse json error:', e);
throw new Error(buf.toString());
}
const { errorCode } = data;
if (errorCode in ERROR_CODES) {
error.message = ERROR_CODES[errorCode];
error.code = errorCode;
throw error;
}
return data;
};
/**
* Youdao Translate API
* @docs http://fanyi.youdao.com/openapi?path=data-mode
* @param {*} options
*/
function Youdao(options) {
if (typeof options === 'string')
return Youdao.fanyi.apply(this, arguments);
if (typeof options !== 'object')
options = {};
const {
key, keyfrom,
version = '1.1',
api = 'http://fanyi.youdao.com/openapi.do'
} = options;
assert(key, 'key is required');
assert(keyfrom, 'keyform is required');
function fanyi(q = '', query, cb) {
if (typeof query === 'function') {
cb = query;
query = {};
}
const params = parseURL(api, Object.assign({
q,
key,
keyfrom,
version,
type: 'data',
doctype: 'json',
}, query));
return Promise
.resolve(params)
.then(get)
.then(readStream)
.then(parseJSON)
.then(res => (cb && cb(null, res), res))
.catch(err => {
if (cb) cb(err)
else throw err;
});
}
return fanyi.fanyi = fanyi;
}
Youdao.__defineGetter__('fanyi', () => {
// defaults key
return Youdao({
key: '2001075261',
keyfrom: 'LSONGORG'
});
});
module.exports = Youdao;