-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
69 lines (64 loc) · 1.86 KB
/
index.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
var isEmpty = require('./isEmpty');
var staticOptions = {};
function apiResponse(req, res, next) {
var successStatus;
var failureStatus;
var shouldNotHaveData;
var shouldSendErrorMessage;
switch (req.method.toLowerCase()) {
case 'get':
successStatus = 200;
failureStatus = res.err ? 400 : 404;
shouldNotHaveData = res.shouldNotHaveData;
break;
case 'post':
shouldNotHaveData = typeof res.shouldNotHaveData !== 'undefined' ?
res.shouldNotHaveData : true;
successStatus = 201;
failureStatus = res.err ? 400 : 404;
break;
case 'put':
case 'patch':
case 'delete':
shouldNotHaveData = typeof res.shouldNotHaveData !== 'undefined' ?
res.shouldNotHaveData : true;
successStatus = shouldNotHaveData && isEmpty(res.data, staticOptions) ? 204 : 200;
failureStatus = res.err ? 400 : 404;
break;
default:
successStatus = 200;
failureStatus = res.err ? 400 : 404;
}
if (res.err !== undefined && res.err !== null && res.err.message !== undefined && res.err.message !== null) {
if(res.shouldSendErrorMessage === undefined) {
shouldSendErrorMessage = true;
} else {
shouldSendErrorMessage = res.shouldSendErrorMessage;
}
} else {
shouldSendErrorMessage = false;
}
successStatus = res.successStatus || successStatus;
failureStatus = res.failureStatus || failureStatus;
if (res.err) {
if (shouldSendErrorMessage) {
return res.status(failureStatus).send(res.err.message);
} else {
return res.status(failureStatus).end();
}
} else if ((isEmpty(res.data, staticOptions))) {
if (!shouldNotHaveData) {
return res.status(failureStatus).end();
} else {
return res.status(successStatus).end();
}
} else {
return res.status(successStatus).json(res.data);
}
};
apiResponse.options = function(options){
if (typeof options === 'object'){
staticOptions = options;
}
};
module.exports = apiResponse;