-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
36 lines (34 loc) · 882 Bytes
/
main.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
const paths = {};
let errors = [];
const rules = {};
context.read('this:imports.js');
const handleInternal = function (path, methodInfo) {
if (methodInfo.request) {
let result = context.match(request.body, checks[methodInfo.request]);
if (!result.pass) errors.push(result.message);
}
if (methodInfo.response) {
response.body = samples[methodInfo.response];
}
let rule = rules[path];
if (rule) {
rule();
}
if (errors.length) {
response.body = errors.join('\n');
response.status = 400;
}
};
const handle = function () {
for (const [path, methods] of Object.entries(paths)) {
if (request.pathMatches(path)) {
const methodInfo = methods[request.method.toLowerCase()];
if (methodInfo) {
handleInternal(path, methodInfo);
return true;
}
}
}
return false;
};
if (!handle()) response.status = 404;