Completed
Push — master ( 1c23c6...f10b86 )
by Neil
02:06
created

module.exports.ok   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 4 1
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)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
34
      res.json(obj)
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
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)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
43
      res.redirect(url)
44
    },
45
    json: () => res.json(obj)
46
  })
47
}
48