-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcuratorAPIRouter.js
107 lines (94 loc) · 3.43 KB
/
curatorAPIRouter.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
/** curatorAPI.js
@def:
@TODO:
[ ] add user Oath
[ ]
*/
"use strict"
// imports
const router = express.Router(),
Strider = require('./Strider.js'),
GetAllUsersApi = require('./endpointsConfig/GetAllUsers.js'),
GetUserByIdApi = require('./endpointsConfig/GetUserById.js'),
GetAllArticalsApi = require('./endpointsConfig/GetAllArticals.js'),
GetArticalByIdApi = require('./endpointsConfig/GetArticalById.js'),
PostArticalApi = require('./endpointsConfig/PostArtical.js'),
PatchArticalByIdApi = require('./endpointsConfig/PatchArticalById.js');
/** API end-point list and config */
const EndPoints = [
GetAllUsersApi,
GetUserByIdApi,
GetAllArticalsApi,
GetArticalByIdApi,
PostArticalApi,
PatchArticalByIdApi
];
/** A factory method which creates a function from given configs
to handle an API endpoint and returns the same. */
const getAPIHandler = (endpointConfig) => {
if (endpointConfig.fn && typeof endpointConfig.fn == 'function')
return endpointConfig.fn(endpointConfig);
// else
return function(...requestParam) {
Logger.log(requestParam[0]);
// TODO: check for oath permissions over here
let allowdRoles = endpointConfig.oath.allow;
let sessionToken = requestParam[0].headers['session-token'];
authenticationUtils
.checkAuthorization(sessionToken, allowdRoles)
.then((authorized) => {
if (authorized.authenticated) {
new Strider(...requestParam)
.setUser(authorized.user)
.setSessionToken(sessionToken)
.setup(this.steps, this.onErrorShouldBreak)
.run()
.then(() => {
Logger.dev.debug('promise resolved by Strider...');
});
} else {
requestParam[1].status(401)
.send({
"status": "401",
"error": {
"message": "UnAuthorized"
}
});
}
}).fail((err) => {
Logger.error("-->", err);
});
}.bind(endpointConfig);
};
/** Middle-ware for curatorAPI router
@def: add all common checks over here,
it gets call before assigned endpoint function
*/
router.use((req, res, next) => {
Logger.dev.debug('Router Middleware, Time: ', Date.now());
next();
});
/** Attach API handler function to each API end-point from the list */
for (let i in EndPoints) {
let endPoint = EndPoints[i];
Logger.dev.debug('registering end point: ', endPoint.method, endPoint.path);
// Convert path into an array of path, if provided a string
if (typeof endPoint.path == 'string') {
endPoint.path = [endPoint.path];
}
//
for (let i in endPoint.path) {
let path = endPoint.path[i];
if (endPoint.method == 'GET')
router.get(path, getAPIHandler(endPoint));
else if (endPoint.method == 'POST')
router.post(path, getAPIHandler(endPoint));
else if (endPoint.method == 'PATCH')
router.patch(path, getAPIHandler(endPoint));
else if (endPoint.method == 'DELETE')
router.delete(path, getAPIHandler(endPoint));
}
}
// Exports
module.exports = router;
//