| Total Complexity | 13 |
| Complexity/F | 1.3 |
| Lines of Code | 47 |
| Function Count | 10 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
| 1 | const env = require('../config').env |
||
| 2 | const logger = require('winston') |
||
| 3 | |||
| 4 | /** |
||
| 5 | Returns a function that will write the result as a JSON to the response |
||
| 6 | */ |
||
| 7 | module.exports.ok = function (res) { |
||
| 8 | return (data) => { |
||
| 9 | logger.debug('Returning JSON data to client: ' + data) |
||
| 10 | res.json(data) |
||
| 11 | } |
||
| 12 | } |
||
| 13 | |||
| 14 | /** |
||
| 15 | Depending on the error type, will perform the following: |
||
| 16 | Object was not found - 404 Not Found |
||
| 17 | Invalid or missing input parameter - 400 Bad request |
||
| 18 | Not enough privileges - 401 Unauthorized |
||
| 19 | Unknown error - 500 Internal server error |
||
| 20 | */ |
||
| 21 | module.exports.fail = function (res) { |
||
| 22 | return (error) => { |
||
| 23 | logger.error(error) |
||
| 24 | res.sendStatus(404).end() |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | |||
| 29 | module.exports.respond = function (res, tpl, obj, status) { |
||
| 30 | res.format({ |
||
| 31 | html: () => res.render(tpl, obj), |
||
| 32 | json: () => { |
||
| 33 | if (status) return res.status(status).json(obj) |
||
|
|
|||
| 34 | res.json(obj) |
||
| 35 | } |
||
| 36 | }) |
||
| 37 | } |
||
| 38 | |||
| 39 | module.exports.respondOrRedirect = function respondOrRedirect ({ req, res }, url = '/', obj = {}, flash) { |
||
| 40 | res.format({ |
||
| 41 | html: () => { |
||
| 42 | if (req && flash) req.flash(flash.type, flash.text) |
||
| 43 | res.redirect(url) |
||
| 44 | }, |
||
| 45 | json: () => res.json(obj) |
||
| 46 | }) |
||
| 47 | } |
||
| 48 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.