UNPKG

node-rigorous

Version:
69 lines (52 loc) 1.95 kB
/* * 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) */ class RigorousRoute { constructor(middlewares, operationParams = {}) { this.middlewares = middlewares; this.operationParams = operationParams; this.userIdAsking = null; } routeIt(app, method, path) { try { app[method](path, this.middlewares, async (req, res) => { try { const object = await this.rigorousHandler(req); this.sucess(res, object); } catch (err) { console.log(err); this.error(res, err); } }); } catch (err) { console.log('Route path:', path); console.log(err); } } async rigorousHandler(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) { res.status(200).json({ data: data }); } error(res, err) { res.status(500).json({ error: err }); } } module.exports = RigorousRoute;