forked from alancieri/jresponse-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (84 loc) · 2.59 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var _ = require('underscore-node');
const setJResponse = () => {
return (req, res, next) => {
res.JRes = new JResponse(res)
next()
}
}
class JResponse {
constructor (res) {
this.res = res
this.code = 200
this.response = {
success: true, count: 0, data: [], errors: []
}
}
static success (data = []) {
data = (_.isArray(data)) ? data : [data]
return JResponse.send(true, data, [])
}
static errors (errors = []) {
errors = (_.isArray(errors)) ? errors : [errors]
return JResponse.send(false, [], errors)
}
static send (success, data, errors) {
let response = { success: true, count: 0, data: [], errors: [] }
if (!_.isEmpty(errors)) {
response.success = false
response.errors = errors
}
if (!_.isEmpty(data)) {
response.data = data
response.count = response.data.length
}
return response
}
sendResponse (success, data, errors) {
if (_.isUndefined(this.code)) { this.code = 200 }
if (_.isUndefined(this.response)) { this.response = { success: true, count: 0, data: [], errors: [] } }
if (!_.isUndefined(errors) && !_.isEmpty(errors)) {
this.response.success = false
this.code = 400
if (!_.isArray(errors)) {
this.response.errors.push(errors)
} else {
this.response.errors = errors
}
}
if (!_.isUndefined(data) && !_.isEmpty(data)) {
if (!_.isArray(data)) {
this.response.data.push(data)
} else {
this.response.data = data
}
}
this.response.count = this.response.data.length
if (!_.isUndefined(this.res)) {
return this.res.status(this.code).send(this.response)
} else {
return this.response
}
}
sendSuccess (data) {
if (!_.isUndefined(data) && !_.isEmpty(data)) {
(_.isArray(data)) ? this.response.data = this.response.data.concat(data) : this.response.data.push(data)
}
return this.sendResponse(true)
}
sendErrors (errors, code) {
if (!_.isUndefined(errors) && !_.isEmpty(errors)) {
(_.isArray(errors)) ? this.response.errors = this.response.errors.concat(errors) : this.response.errors.push(errors)
}
this.response.success = false
this.code = !_.isUndefined(code) ? code : 400
return this.sendResponse(false)
}
appendError (errors, code) {
if (!_.isUndefined(errors) && !_.isEmpty(errors)) {
(_.isArray(errors)) ? this.response.errors.concat(errors) : this.response.errors.push(errors)
}
this.code = !_.isUndefined(code) ? code : 400
}
}
module.exports.JResponse = JResponse;
module.exports.setJResponse = setJResponse;