-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.js
355 lines (321 loc) · 10.5 KB
/
api.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
var config = require('./config');
var FS = require('./utils/FS');
var Mongo = require('./utils/Mongo');
var Get = require('./utils/Get');
var Post = require('./utils/Post');
Object.toArray = o => Object.keys(o).map(x => o[x]);
Array.toDict = (arr, key = 'id') => {var o = {}; for (var i in arr) o[arr[i][key]] = arr[i]; return o;};
Object.defineProperty(global, '__stack', {
get: function() {
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack) {
return stack;
};
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});
Object.defineProperty(global, '__line', {
get: function() {
return __stack[1].getLineNumber();
}
});
Object.defineProperty(global, '__function', {
get: function() {
return __stack[1].getFunctionName();
}
});
global.__aio_err = (_function, _filename, _line) => {
return ` at ${_function} (${_filename}:${_line})`;
};
var dump = i => {
var o = {};
for (k in i) {
if (i[k].test)
o[k] = '' + i[k];
else if (Array.isArray(i[k]))
o[k] = i[k];
else if (typeof i[k] == 'object')
o[k] = dump(i[k]);
else
o[k] = i[k];
}
return o;
};
var check = (key, value, rule, query) => {
if (Array.isArray(rule)) {
if (rule.length > 0) {
if (rule[0].substr) {
// enum
if (rule.indexOf(value) == -1)
throw [-4, "value '" + value + "' of parameter '" + key + "' is not in ['" + rule.join("', '") + "']"];
} else if (rule.length == 1 && Array.isArray(rule[0])) {
// array of enum
if (!Array.isArray(value))
throw [-5, "type of parameter '" + key + "' must be array"];
for (var i in value)
if (rule[0].indexOf(value[i]) == -1)
throw [-6, "value '" + value[i] + "' of parameter '" + key + "[" + i + "]' is not in ['" + rule[0].join("', '") + "']"];
} else if (rule.length == 2) {
// range
value = parseInt(value);
if (value < rule[0] || value > rule[1])
throw [-3, "value " + value + " of parameter '" + key + "' is not in the range of (" + rule[0] + ", " + rule[1] + ")"];
query[key] = value;
}
} else {
// array
if (!Array.isArray(value))
throw [-5, "type of parameter '" + key + "' must be array"];
}
} else if (rule.test) {
// regex
if (!rule.test(value))
throw [-7, "value '" + value + "' of parameter '" + key + "' does not match pattern " + rule];
} else if (rule == 'int' || rule === parseInt(rule)) {
// int
if (isNaN(parseInt(value)))
throw [-8, "value '" + value + "' of parameter '" + key + "' is not an integer"];
query[key] = parseInt(value);
} else if (rule == 'float' || rule === parseFloat(rule)) {
// float
if (isNaN(parseFloat(value)))
throw [-8, "value '" + value + "' of parameter '" + key + "' is not an float"];
query[key] = parseFloat(value);
} else if (rule === Object(rule)) {
// object
if (value !== Object(value))
throw [-9, "value '" + value + "' of parameter '" + key + "' is not an object"];
} else if (rule == 'date') {
// date
//TODO
} else if (rule == '') {
// any
}
};
var callback = (rules, func) =>
async (req, res, next) => {
// write API log
var writeLog = log => {
// ignore local test
if (req.ip == '::1') return;
// send API log to logger asynchronously
Post.json(config.logger_url, Object.assign({
ip: req.ip,
host: req.hostname,
base: req.baseUrl,
url: req.originalUrl,
path: req.path,
method: req.method,
query: req.query,
body: req.body,
signedCookies: req.signedCookies,
//session: req.sessoin.toJSON(),
session: req.session || null,
headers: req.headers,
date: new Date(),
}, log)).catch(e => e);
};
res.rtn = (json) => {
if (req.query.callback) {
res.setHeader("Content-Type", "application/javascript; charset=utf-8");
res.jsonp(json);
} else {
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.json(json);
}
};
res.data = (data, file, cache) => {
writeLog({type: 'data', data, file});
var values = [];
for (var key in data) {
var json = JSON.stringify(data[key]);
values.push('data.' + key + ' = ' + json + ';');
}
var html = FS(file).replace('//data//', values.join('\n'));
if (cache)
FS.write(config.cache + '/' + cache, html);
res.setHeader("Content-Type", "application/javascript; charset=utf-8");
res.end(html);
}
res.render = (data, file, cache) => {
writeLog({type: 'render', data, file});
var values = [];
for (var key in data) {
var json = JSON.stringify(data[key]);
if (data[key]) {
while (json.match(/<\/?script[^>]*>/i)) {
json = json.replace(/<\/?script[^>]*>/ig, '');
}
}
values.push('data.' + key + ' = ' + json + ';');
}
var html = FS(file).replace('//data//', values.join('\n'));
//html = html.replace('//facebook//', FS('public/js/fb.js'));
if (req.user && req.user.en)
html = html.replace(/\/assets\/images\//g, '/assets/images/english/');
if (cache)
FS.write(config.cache + '/' + cache, html);
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(html);
}
res.facebook = (data, file, og, cache) => {
writeLog({type: 'facebook', data, file, og});
var values = [];
for (var key in data) {
var json = JSON.stringify(data[key]);
while (json.match(/<\/?script[^>]*>/i)) {
json = json.replace(/<\/?script[^>]*>/ig, '');
}
values.push('data.' + key + ' = ' + json + ';');
}
var html = FS(file).replace('//data//', values.join('\n'));
//html = html.replace('//facebook//', FS('public/js/fb.js'));
//if (req.user && req.user.en) html = html.replace(/\/assets\/images\//g, '/assets/images/english/');
html = html.replace(/=og:title/, og.title);
html = html.replace(/=og:description/, og.description);
html = html.replace(/=og:url/, og.url);
html = html.replace(/=og:image/, og.image);
if (cache)
FS.write(config.cache + '/' + cache, html);
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(html);
}
res.ok = (data) => {
writeLog({type: 'ok', data});
res.rtn({code: 0, message: 'ok', data: data});
};
res.err = (code, message, error) => {
writeLog({type: 'error', code, message, error});
res.rtn({code: code, message: message, error: error});
};
try {
req.checked = {};
var query = (req.method == 'POST')? req.body: req.query;
for (var key in rules) {
// login
if (key == '$') { // require user login
if (rules[key] == 'user') {
// get user from session_key
var session = await Mongo.one('session', {session_key: req.session.key});
if (!session) return res.redirect('/token/user_login?redirect=' + encodeURIComponent(req.originalUrl));
req.user = session.user;
}
continue;
}
// optional
if (key == '_') {
for (var _key in rules['_']) {
if (_key in query) {
var value = query[_key];
check(_key, value, rules['_'][_key], query);
req.checked[_key] = value;
}
}
continue;
}
if (rules[key] == 'file') {
if (!req.files[key])
throw [-10, "missing file '" + key + "'"];
continue;
}
var value = query[key];
if (typeof value === 'undefined')
throw [-2, "missing parameter '" + key + "'"];
if (rules[key] == 'user') {
// update session_key if provided
if (value) req.session.key = value;
// get user from session_key
var session = await Mongo.one('session', {session_key: req.session.key});
if (!session) throw [-1, 'Please login again!'];
req.user = session.user;
if (session.appID) req.appID = session.appID;
}
else {
check(key, value, rules[key], query);
req.checked[key] = value;
}
}
func(req, res, next);
} catch(e) {
console.log(e);
//return res.err(-99, '系統忙碌中請稍候');
if (Array.isArray(e))
return res.err(e[0], e[1]);
res.err(e.code? e.code: -99, e.message? e.message: 'unknown error', e);
}
};
var api = base => {
api = require('express').Router();
api._get = api.get;
api._post = api.post;
api._put = api.put;
api._delete = api.delete;
var help = {};
var unit = {};
api.get = (route, rules, func) => {
if (typeof rules == 'function') {
func = rules;
rules = {};
}
help['GET ' + base + route] = rules;
api._get(route, callback(rules, func));
return api;
};
api.post = (route, rules, func) => {
if (typeof rules == 'function') {
func = rules;
rules = {};
}
help['POST ' + base + route] = rules;
api._post(route, require('connect-multiparty')(), callback(rules, func));
return api;
};
api.put = (route, rules, func) => {
if (typeof rules == 'function') {
func = rules;
rules = {};
}
help['PUT ' + base + route] = rules;
api._put(route, require('connect-multiparty')(), callback(rules, func));
return api;
};
api.delete = (route, rules, func) => {
if (typeof rules == 'function') {
func = rules;
rules = {};
}
help['DELETE + ' + base + route] = rules;
api._delete(route, callback(rules, func));
return api;
};
api.help = (route) => {
api._get(route, (req, res) => res.json(dump(help)));
return api;
};
api.unit = (route, qs) => {
if (!unit[route])
unit[route] = [];
unit[route].push(qs);
return api;
};
api.test = (route) => {
api._get(route, async (req, res) => {
var Jobs = {};
var http = Post.json;
for (var path in unit)
if (unit[path].length == 1)
Jobs[base + path] = await http(process.env.URI + base + path, unit[path][0]);
else
for (var i in unit[path])
Jobs[base + path + ':' + i] = await http(process.env.URI + base + path, unit[path][i]);
res.json(Jobs);
});
return api;
};
return api;
};
module.exports = api;