-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
executable file
·149 lines (127 loc) · 3.35 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var STATUSES = {
success: { required: ['status', 'data'], allowed:['status', 'data'] },
fail: { required: ['status', 'data'], allowed:['status', 'data'] },
error: { required: ['status', 'message'], allowed:['status', 'message', 'data', 'code'] }
};
function requireKeys(keys, json) {
return keys.every(function(key) {
return key in json;
});
}
function allowKeys(keys, json) {
return Object.keys(json).every(function(key) {
return ~keys.indexOf(key);
});
}
function jsend(config, host) {
config = config || {};
host = host || {};
function isValid(json) {
var spec = STATUSES[json && json.status],
valid = !!spec && requireKeys(spec.required, json);
if(config.strict) valid = valid && allowKeys(spec.allowed, json);
return valid;
}
function forward(json, done) {
if(!isValid(json))
json = {
status: 'error',
message: 'Invalid jsend object.',
data: { originalObject: json }
};
if(json.status === 'success')
done(null, json.data);
else {
var err = new Error(json.message || ('Jsend response status: ' + json.status));
if('code' in json) err.code = json.code;
if('data' in json) err.data = json.data;
done(err, json.data);
}
}
function fromArguments(err, json) {
if(arguments.length === 1 && err.length === 2) {
json = err[1];
err = err[0];
}
if(err) {
json = {
status: 'error',
message: (typeof err === 'string')
? err
: err && err.message || 'Unknown error. (jsend)'
};
if(err && err.stack) json.data = { stack:err.stack };
} else if(json === undefined) {
json = {
status: 'error',
message: 'No data returned.'
};
} else if(!isValid(json)) {
json = {
status: 'success',
data: json
};
}
return json;
}
function success(data) {
if(data === undefined) throw new Error('"data" must be defined when calling jsend.success. (jsend)');
return {
status: 'success',
data: (data && data.status === 'success' && isValid(data))
? data.data
: data
};
}
function fail(data) {
if(data === undefined) throw new Error('"data" must be defined when calling jsend.fail. (jsend)');
return{
status: 'fail',
data: (data && data.status === 'fail' && isValid(data))
? data.data
: data
};
}
function error(message, rest) {
var json = {
status: 'error'
};
if(typeof message === 'string') {
json.message = message;
if(rest) {
if(rest.code !== undefined) json.code = rest.code;
if(rest.data !== undefined) json.data = rest.data;
}
} else if(message && message.message) {
json.message = message.message;
if(message.code !== undefined) json.code = message.code;
if(message.data !== undefined) json.data = message.data;
} else {
throw new Error('"message" must be defined when calling jsend.error. (jsend)');
}
return json;
}
host.isValid = isValid;
host.forward = forward;
host.fromArguments = fromArguments;
host.success = success;
host.fail = fail;
host.error = error;
host.middleware = function(req, res, next) {
var middleware = res.jsend = function(err, json) {
res.json(fromArguments(err, json));
};
middleware.success = function(data) {
res.json(success(data));
};
middleware.fail = function(data) {
res.json(fail(data));
};
middleware.error = function(message) {
res.json(error(message));
};
next();
};
return host;
}
module.exports = jsend(null, jsend);