-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
app.js
122 lines (90 loc) · 3.46 KB
/
app.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const { JSDOM } = require('jsdom'),
express = require('express'),
rateLimit = require("express-rate-limit"),
utils = require('./modules/utils.js'),
errors = require('./modules/errors.js'),
dictionary = require('./modules/dictionary.js'),
// HTML Parser
{ DOMParser } = new JSDOM().window,
parser = new DOMParser(),
app = express(),
limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 450 // limit each IP to 450 requests per windowMs
}),
// Versions
V1 = 'v1',
V2 = 'v2',
// Status Codes
REQUEST_TYPE_STATUS_CODE = {
notFound: 404,
rateLimit: 429,
serverError: 500
},
// Headers
HEADER_CONTENT_TYPE = 'Content-Type',
HEADER_ACCESS_CONTROL_ALLOW_ORIGIN = 'Access-Control-Allow-Origin';
// GLOBALS
global._ = require('lodash');
function cleanText (text) {
if (!text) { return text; }
return parser
.parseFromString(text, "text/html")
.body.textContent;
}
function handleError (error = {}) {
// Using duck typing to know if we explicitly threw this error
// If not then wrapping original error into UnexpectedError
if (!error.requestType) { error = new errors.UnexpectedError({ original_error: error }); }
const { requestType, title, message, resolution } = error;
status = REQUEST_TYPE_STATUS_CODE[requestType],
body = JSON.stringify({
title,
message,
resolution
});
this.set(HEADER_CONTENT_TYPE, 'application/json');
this.set(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, '*');
return this.status(status).send(body);
};
app.set('trust proxy', true);
app.use(limiter);
app.get('/api/:version/entries/:language/:word', async (req, res) => {
let { word, language, version } = req.params,
include = _.reduce(_.get(req.query, 'include', '').split(','), (accumulator, current) => {
accumulator[current] = true;
return accumulator;
}, {});
word = decodeURIComponent(word);
if (!word || !language || !version) {
return handleError.call(res, new errors.NoDefinitionsFound());
}
// @todo: Find better error.
if (!utils.isVersionSupported(version)) { return handleError.call(res, new errors.NoDefinitionsFound()); }
// Todo: Figure out better strategy.
if (language === 'en_US' || language === 'en_GB') { language = 'en'; }
// By default we are assuming person means American English
// This is needed for backward compatibility.
language = language.toLowerCase();
// @todo: Find better error.
if (!utils.isLanguageSupported(language)) { return handleError.call(res, new errors.NoDefinitionsFound()); }
word = word.trim().toLocaleLowerCase(language);
try {
let definitions = await dictionary.findDefinitions(word, language, { include }),
status = 200,
body;
if (version === V1) {
definitions = dictionary.transformV2toV1(definitions);
}
body = JSON.stringify(definitions, (key, value) => {
if (typeof value === 'object') { return value; }
return cleanText(value);
});
res.set(HEADER_CONTENT_TYPE, 'application/json');
res.set(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, '*');
return res.status(status).send(body);
} catch (error) {
return handleError.call(res, error);
}
});
app.listen(3000, () => console.log('Server running on port 3000'));