forked from KaedeTai/aio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
195 lines (174 loc) · 5.27 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
var async = require('asyncawait/async');
var await = require('asyncawait/await');
var config = require('./config');
//var User = require('./models/User');
var Get = require('./utils/Get');
var Post = require('./utils/Post');
var File = require('./utils/File');
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) => {
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
if (value < rule[0] || value > rule[1])
throw [-3, "value " + value + " of parameter '" + key + "' is not in the range of (" + rule[0] + ", " + rule[1] + ")"];
}
} 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') {
// int
if (!/-?[0-9]+/.test(value) || isNaN(parseInt(value)))
throw [-8, "value '" + value + "' of parameter '" + key + "' is not an integer"];
} 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) => {
res.rtn = (json) => {
if (req.query.callback)
res.jsonp(json);
else
res.json(json);
};
res.render = (data, file) => {
var values = [];
for (var key in data)
values.push('data.' + key + ' = ' + JSON.stringify(data[key]) + ';');
res.end(File(file).replace('//data//', values.join('\n')));
}
res.ok = (data) =>
res.rtn({code: 0, message: 'ok', data: data});
res.err = (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) {
// optional
if (key == '_') {
for (var _key in rules['_']) {
if (_key in query) {
var value = query[_key];
check(_key, value, rules['_'][_key]);
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') {
// get user from id
req.user = {};//User(value);
if (!req.user) throw [-1, '請重新登入!'];
}
else {
check(key, value, rules[key]);
req.checked[key] = value;
}
}
func(req, res, next);
} catch(e) {
console.log(e);
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;
var help = {};
var unit = {};
api.get = (route, rules, func) => {
if (typeof rules == 'function') {
func = rules;
rules = {};
}
help[base + route] = {get: rules};
api._get(route, callback(rules, func));
return api;
};
api.post = (route, rules, func) => {
if (typeof rules == 'function') {
func = rules;
rules = {};
}
help[base + route] = {post: rules};
api._post(route, require('connect-multiparty')(), 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 = Get.json;
for (var path in unit)
if (unit[path].length == 1)
Jobs[base + path] = http(config.uri + base + path, unit[path][0]);
else
for (var i in unit[path])
Jobs[base + path + ':' + i] = http(config.uri + base + path, unit[path][i]);
res.json(await(Jobs));
}));
return api;
};
return api;
};
module.exports = api;