-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressRest.js
260 lines (234 loc) · 9.54 KB
/
expressRest.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var concatStream = require('concat-stream'),
stringDecoder = require('string_decoder');
module.exports = function(app, options) {
if (!app) throw new Error('Must provide express instance.');
var rest = {
get: get,
put: put,
post: post,
'delete': delete_
};
var serializers = {
json: jsonSerializer()
// TODO: xml: xmlSerializer()
};
var methods = ['GET', 'PUT', 'POST', 'DELETE'];
var maxParams = 0;
init();
return rest;
// reference; http://www.w3schools.com/tags/ref_httpmessages.asp
function init() {
if (options) {
if (options.serializers) {
for (var key in options.serializers) {
serializers[key] = options.serializers[key];
}
}
}
}
function register(app, path, controller) {
if (typeof controller.get === 'function') {
// Register GET handlers
var handler = anyHandler(controller.get.bind(controller));
app.get(path, handler);
var getPath = path;
for (var i = 1; i < controller.get.length; i++) {
getPath += '/:api' + i;
app.get(getPath, handler);
}
}
}
function get(path, handler) {
app.get(path, anyHandler(handler));
return this;
}
function put(path, handler) {
app.put(path, anyHandler(handler));
return this;
}
function post(path, handler) {
app.post(path, anyHandler(handler));
return this;
}
function delete_(path, handler) {
app.delete(path, anyHandler(handler));
return this;
}
function anyHandler(apiHandler) {
return function(req, res) {
// parse JSON body
var contentType = req.headers['content-type'];
if (contentType) {
// Parse request body
for (var key in serializers) {
if (req.is(key) || contentType == key) {
var ser = serializers[key];
try {
ser.deserialize(req, res, continueRequest);
} catch (ex) {
continueRequest(ex);
}
return;
}
}
// Could not find a parser.
rest.unsupportedMediaType();
} else {
return continueRequest();
}
function continueRequest(err) {
var rest = restResponse(req, res, continueResponse);
if (err) {
res.set('Content-Type', 'text/plain');
res.status(500);
res.send(err.toString());
} else {
apiHandler(req, rest);
}
}
function continueResponse(statusCode, message, data) {
res.status(statusCode);
res.body = data;
for (var key in serializers) {
if (req.accepts(key) || req.headers.accept == key) {
var ser = serializers[key];
try {
ser.serialize(req, res, finishResponse);
} catch (ex) {
finishResponse(ex);
}
return;
}
}
res.status(415);
finishResponse();
function finishResponse(err) {
if (err) {
// Serialization failed, revert to plain text.
try {
res.set('Content-Type', 'text/plain');
res.status(500);
res.send(err.toString());
} catch (ex) {
console.error('Unable to return exception: ', err, ex);
}
}
res.end();
}
}
}
}
function restResponse(req, res, next) {
var rest = {
request: req,
response: res,
body: req.body,
continue: status(100, 'Continue'),
switchingProtocols: status(101, 'Switching Protocols'),
checkpoint: status(103, 'Checkpoint'),
ok: status(200, 'OK'),
created: status(201, 'Created', 'Location'),
accepted: status(202, 'Accepted', 'Location'),
nonAuthoritativeInformation: status(203, 'Non Authoritative Information'),
noContent: status(204, 'No Content'),
resetContent: status(205, 'Reset Content'),
partialContent: status(206, 'Partial Content'),
multipleChoices: status(300, 'Multiple Choices'),
movedPermanently: status(301, 'Moved Permanently', 'Location'),
found: status(302, 'Found', 'Location'),
seeOther: status(303, 'See Other'),
notModified: status(304, 'Not Modified'),
switchProxy: status(306, 'Switch Proxy'),
temporaryRedirect: status(307, 'Temporary Redirect', 'Location'),
resumeIncomplete: status(308, 'Resume Incomplete'),
badRequest: status(400, 'Bad Request'),
unauthorized: status(401, 'Unauthorized'),
paymentRequired: status(402, 'Payment Required'),
forbidden: status(403, 'Forbidden'),
notFound: status(404, 'Not Found'),
methodNotAllowed: status(405, 'Method Not Allowed'),
notAcceptable: status(406, 'Not Acceptable'),
proxyAuthenticationRequired: status(407, 'Proxy Authentication Required'),
requestTimeout: status(408, 'Request Timeout'),
conflict: status(409, 'Conflict'),
gone: status(410, 'Gone'),
lengthRequired: status(411, 'Length Required'),
preconditionFailed: status(412, 'Precondition Failed'),
requestEntityTooLarge: status(413, 'Request Entity Too Large'),
requestURITooLong: status(414, 'Request URI Too Long'),
unsupportedMediaType: status(415, 'Unsupported Media Type'),
requestedRangeNotSatisfiable: status(416, 'Requested Range Not Satisfiable'),
expectationFailed: status(417, 'Expectation Failed'),
internalServerError: status(500, 'Internal Server Error'),
notImplemented: status(501, 'Not Implemented'),
badGateway: status(502, 'Bad Gateway'),
serviceUnavailable: status(503, 'Service Unavailable'),
gatewayTimeout: status(504, 'Gateway Timeout'),
httpVersionNotSupported: status(505, 'HTTP Version Not Supported'),
networkAuthenticationRequired: status(511, 'Network Authentication Required')
};
// prototypical inheritance from res
rest.__proto__ = res;
return rest;
function status(statusCode, message, header) {
return function(data) {
if (header) {
// response data should go into a header, not body.
res.set(header, data);
data = arguments[1];
}
next(statusCode, message, data);
return rest;
}
}
}
function jsonSerializer() {
var ser = {
serialize: serialize,
deserialize: deserialize
}
return ser;
function deserialize(req, res, next) {
req.pipe(concatStream(function(data) {
try {
var decoder = new stringDecoder.StringDecoder();
var json = decoder.write(data);
req.body = JSON.parse(json);
next();
} catch (ex) {
next(ex);
}
}));
}
function serialize(req, res, next) {
try {
res.set('Content-Type', 'application/json');
var json = JSON.stringify(res.body);
res.send(json);
next();
} catch (ex) {
next(ex);
}
}
}
function xmlSerializer() {
var ser = {
serialize: serialize,
deserialize: deserialize
}
return ser;
function deserialize(req, rest, next) {
req.pipe(concatStream(function(data) {
var decoder = new stringDecoder.StringDecoder();
var xml = decoder.write(data);
//TODO: req.body = XML.parse(xml);
next();
}));
}
function serialize(req, rest, next) {
res.set('Content-Type', 'application/xml');
//TODO: res.send(XML.stringify(res.body));
next();
}
}
};