-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (62 loc) · 1.79 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
'use strict';
var _util = {
isObject : function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj && !(this.isArray(obj)) ;
},
isArray : function(obj) {
return toString.call(obj) === '[object Array]';
},
isDate : function(obj) {
return toString.call(obj) === '[object Date]';
},
isSpecObj : function(obj) {
return (this.isObject(obj) && ('code' in obj) && ('data' in obj));
}
};
var extendOwn = function extend(source, dest) {
for(var prop in source) {
if (source.hasOwnProperty(prop)) {
dest[prop] = source[prop];
}
}
return dest;
}
module.exports = function(express) {
var res;
if (express && express.Router) {
if (!express.wr) {
express.wr = function(res, spec, meta) {
var result = {},
spec = spec || '',
code = spec.code || 200,
data = !(_util.isObject(spec)) ? (_util.isDate(spec) ? spec.toString() : spec) : (_util.isSpecObj(spec) ? spec.data : spec),
message = spec.message || '',
status = (code >= 500 && code < 600) ? 'fail' : (code >= 400 && code < 500) ? 'error' : 'success';
if (!(res && res.send)) throw {
name : 'Illegal argument',
message : '@res MUST be a express.Response object'
};
result = {
code : code,
status : status,
data : data,
message : message
};
if (meta) {
if (!_util.isObject(meta)) throw {
name : 'Illegal argument',
message : '@meta MUST be an object'
};
result = extendOwn(meta, result);
}
return res.status(code).json(result);
};
}
} else {
throw {
name : 'Illegal argument',
message : '@app argument MUST be express'
};
}
};