-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
164 lines (131 loc) · 3.45 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*!
* Moajs Middle for Koajs.com
* Copyright(c) 2015-2019 Alfred Sang <[email protected]>
* MIT Licensed
*/
var debug = require('debug')('res.api')
// "use strict";
/**
* Add X-Response-Time header field.
*
* @return {Function}
* @api public
*/
function api_middleware(options) {
options = options || {};
return function middleware(ctx, next){
debug('use this.api method to render json data...');
ctx.type = 'application/json; charset=utf-8';
ctx.set('Content-type', 'application/json');
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'GET, POST, PATCH ,DELETE');
ctx.set('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
ctx.api = api;
ctx.api_error = api_error;
if (ctx.is_jsonp && ctx.is_jsonp == true) {
jsonp = require('koa-jsonp')
}
return next().then(() => {
});
};
}
module.exports = api_middleware;
/**
Usages:
// 最通用的api接口
return res.api(404 ,err, {
code : 1,
msg : 'delete failed!'
});
// 返回成功带有状态的json数据
return res.api(err, {
code : 1,
msg : 'delete failed!'
});
// 最常用的成功返回json数据
return res.api(err);
// 出错
返回code : 222222222
*/
function api(){
var ctx = this;
var _res = this.response;
// console.dir()
// return _res.body = {
// a:1
// }
if(typeof(_res) != "object" || _is_not_has_prototype('end')){
arguments = [];
console.dir('not a object')
}
if (arguments.length == 1) {
var http_code = 200;
var api_data = arguments[0];
var api_status = {
code : 0,
msg : 'request success!'
}
return _api(http_code, api_data, api_status);
} else if (arguments.length == 2) {
var http_code = arguments[0];
var api_data = arguments[1];
var api_status = {
code : 0,
msg : 'request success!'
}
return _api(http_code, api_data, api_status);
} else if (arguments.length == 3) {
var http_code = arguments[0];
var api_data = arguments[1];
var api_status = arguments[2];
return _api(http_code, api_data, api_status);
} else {
var http_code = 200;
var api_data = {};
var api_status = {
code : 222222222,
msg : 'res.api params error or res is not a express.response object!'
}
return _api(http_code, api_data, api_status);
}
function _is_not_has_prototype(name){
return !_res.hasOwnProperty(name)&&(name in _res);
}
function _api (http_code, data, status) {
debug(http_code)
debug(data)
debug(status)
if (ctx.is_jsonp && ctx.is_jsonp == true) {
ctx.status = http_code
return ctx.body = {
data : data,
status : status
}
} else {
ctx.status = http_code
return ctx.body = {
data : data,
status : status
}
}
}
}
function api_error(data){
var _res = this;
var _error_code = 200;
var _error_status_code = -1;
var _error_status_msg = 'api error';
if(_res.api_error_code){
_error_code = _res.api_error_code;
}
if(_res.api_error_status_code){
_error_status_code = _res.api_error_status_code;
}
if(_res.api_error_status_msg){
_error_status_msg = _res.api_error_status_msg;
}
_res.api(_error_code, data, {
code : _error_status_code,
msg : _error_status_msg
});
}