nodejs-rigorous
Version:
Rigorous Framework
88 lines (60 loc) • 2.2 kB
JavaScript
/*
* CHART CODE FOR ROUTE:
*
* 1/4 SECURE INPUT (avoid script injection)
* 2/4 CHECK CONFORMITY INPUT (to avoid integrity issues when editing multiple collections, if one fail because of validity)
* 3/4 CHECK AUTHORIZATION (is the user legitim ?)
* 4/4 PROCESS (handle not found case)
*/
/* abstract */ class RigorousRoute {
constructor(middlewares, operationParams) {
this.operationParams = operationParams;
this.userIdAsking = null;
this.middlewares = middlewares;
}
async exec(req) {
/* ------- 1/5 SECURE INPUT ----------------- */
const inputs = await this.secureInput(req);
/* ------- 2/5 CHECK CONFORMITY INPUT ------- */
const checker = await this.checkConformityInput(inputs);
/* ------- 3/5 CHECK AUTHORIZATION ---------- */
await this.checkAuthorization(checker);
/* ------- 4/5 PROCESS ---------------------- */
const object = await this.processData(inputs, checker);
/* ------- 5/5 NOTIFICATION ---------------------- */
await this.sendNotification(inputs, object.notificationParam);
return object.result;
}
sucess(res, data) {
this;
res.status(200).json({ data: data });
}
error(res, err) {
this;
res.status(500).json({ stack: err.stack, response: err.response, created_at: new Date() });
}
/**
*
* @param {*} callType : 'get' or 'post'
* @param {*} relativePath
* @param {*} middlewares
*/
routeIt(app, callType, relativePath) {
try {
app[callType](`${relativePath}`, this.middlewares,
async (req, res) => {
try {
const object = await this.exec(req);
this.sucess(res, object);
} catch (err) {
console.log(err);
this.error(res, err);
}
});
} catch (err) {
console.log('relativePath ', relativePath);
console.log('err ', err);
}
}
}
module.exports = RigorousRoute;