/
index.js
374 lines (317 loc) · 11.9 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// This is an example route configuration file.
// This file manages two related resources:
// - /users/123 - Single user
// - /users - Collection of users
/*** Initial dependencies **/
var _ = require('lodash');
var Promise = require('bluebird');
var Router = require('../lib');
var Resource = require('../lib').Resource;
var OffsetPaginator = require('../lib').OffsetPaginator;
var Symlink = require('../lib').Symlink;
var fs = require('fs');
var path = require('path');
var chain = require('connect-chain');
/*** Set up simple HTTP server ***/
var connect = require('connect');
var app = connect();
// Allow method override via ?_method=METHOD query string parameter
var methodOverride = require('method-override');
app.use(methodOverride('_method', {
methods: [ 'GET', 'POST' ] // Specifies which methods can support overrides
}));
var bodyParser = require('body-parser');
app.use(bodyParser.json());
/*** Mock data ***/
var mockUserBasicInfo = require('./data/user-basic-info');
var mockUserEmails = require('./data/user-emails');
var mockPhotos = require('./data/photos');
/*** Define the schemas ***/
var userSchema = require('./schemas/user');
var userCollectionSchema = require('./schemas/user-collection');
var photoSchema = require('./schemas/photo');
var photoCollectionSchema = require('./schemas/photo-collection');
/*** Set up a new router ***/
var api = new Router({
"openapi": "3.0.0",
"info": {
"version": 1,
"description": "Some Demo Stuff",
"title": "Demo App"
}
});
/*** Define the routes ***/
// Define the route that points to a specific user.
// The URL may contain named parameters, which will be parsed and handed to the callback.
api.route('/users/:userId', userSchema, {
// Complex resources may need multiple callback handlers to support different properties.
// The API router will figure out which callbacks are necessary to satisfy the incoming request.
get: [
{
props: ['displayName', 'age', 'gender', 'birthDate', 'city', 'country'],
callback: getUserBasicInfo
},
{
props: ['email'],
callback: getUserEmailInfo
}
],
put: [
{
props: ['displayName', 'age', 'gender', 'birthDate', 'city', 'country'],
callback: putUserBasicInfo
},
{
props: ['email'],
callback: putUserEmailInfo
}
],
patch: function() {},
delete: function(request) {
var userId = request.getParam('userId');
return request.error(403, 'You are not allowed to delete this user.');
}
});
// Defines the route that manages a collection of users
api.route(['/users', 'limit=10&offset=0&search'], userCollectionSchema, {
get: getUsers,
post: createUser
});
api.route(['/users/:userId/photos', 'limit=10&offset=0'], photoCollectionSchema, {
// Complex resources may need multiple callback handlers to support different properties.
// The API router will figure out which callbacks are necessary to satisfy the incoming request.
get: function(request) {
var userId = request.getParam('userId');
//Get Parametersplice(0, limit).
var limit = request.getQuery('limit');
var offset = request.getQuery('offset');
var photos = mockPhotos.filter(function(photo) {
return photo.userId === userId;
})
.slice(offset, limit)
.map(function(photo) {
return new Resource('/users/' + userId + '/photos/' + photo.photoId, photo, 86400);
});
return new OffsetPaginator('/users/' + userId + '/photos')
.setItems(photos)
.setLimit(limit)
.setOffset(offset)
.setTotal(mockPhotos.length);
},
post: function(request) {
return request.getUpload('photo')
.then(function(upload) {
var photoFileName = Math.round(Math.random() * 1e16).toString(36);
switch (upload.mimeType) {
case 'image/png':
photoFileName += '.png';
break;
case 'image/jpeg':
photoFileName += '.jpg';
break;
case 'image/gif':
photoFileName += '.gif';
break;
default:
return request.error(442, 'Invalid upload type: ' + upload.mimeType);
}
var saveFilePath = path.join(__dirname, 'files', 'photos', photoFileName);
var wstream = fs.createWriteStream(saveFilePath, {
encoding: upload.encoding
});
wstream.write(upload.buffer);
wstream.end();
var userId = request.getParam('userId');
var photoId = mockPhotos.length;
var photo = request.getResource();
photo.userId = userId;
photo.photoId = photoId;
photo.url = 'http://localhost:5150/photos/' + photoFileName;
mockPhotos.push(photo);
return new Resource('/users/' + userId + '/photos/' + photo.photoId, photo, 86400);
});
}
});
api.route('/users/:userId/photos/:photoId', photoSchema, {
// Complex resources may need multiple callback handlers to support different properties.
// The API router will figure out which callbacks are necessary to satisfy the incoming request.
get: function(request) {
var userId = request.getParam('userId');
var photoId = request.getParam('photoId');
for (var i = 0, len = mockPhotos.length; i < len; i++) {
var photo = mockPhotos[i];
if (photo.photoId === photoId && photo.userId === userId) {
return new Resource('/users/' + userId + '/photos/' + photoId, photo, 86400);
}
}
return Promise.reject("Unable to find photo for user " + userId + " and photo id " + photoId);
},
put: function(request) {
var userId = request.getParam('userId');
var photoId = request.getParam('photoId');
var submittedPhoto = request.getResource();
var photo;
for (var i = 0, len = mockPhotos.length; i < len; i++) {
if (mockPhotos[i].photoId === photoId && mockPhotos[i].userId === userId) {
photo = mockPhotos[i];
break;
}
}
if (!photo) {
return Promise.reject("Unable to find photo for user " + userId + " and photo id " + photoId);
}
photo.caption = submittedPhoto.caption;
// mockPhotos[i] = photo;
return new Resource('/users/' + userId + '/photos/' + photo.photoId, photo, 86400);
},
delete: function() {},
});
/*** Define the callback functions ***/
// Returns basic user info for a given user id
function getUserBasicInfo(request) {
// Get the user ID from the URL params
var userId = request.getParam('userId'); // ex: 123
return new Promise(function(resolve, reject) {
for (var i = 0, len = mockUserBasicInfo.length; i < len; i++) {
if (mockUserBasicInfo[i].userId === userId) {
return resolve(new Resource('/users/' + userId, mockUserBasicInfo[i], 3600));
}
}
reject('Unable to find basic info for user id ' + userId);
});
}
// Returns email info for a given user id
function getUserEmailInfo(request) {
// Get the user ID from the URL params
var userId = request.getParam('userId'); // ex: 123
return new Promise(function(resolve, reject) {
var index = userId - 1;
var userEmail = mockUserEmails[index];
if (userEmail) {
resolve(new Resource('/users/' + userId, userEmail, 3600));
} else {
reject('Unable to find email for user id ' + userId);
}
});
}
// Returns basic info for a collection of users
function getUsersBasicInfo(request) {
return new Promise(function(resolve, reject) {
resolve(mockUserBasicInfo.map(function(info, i) {
return new Resource('/users/' + info.userId, info);
}));
});
}
// Returns email info for a collection of users
function getUsersEmailInfo(request) {
return new Promise(function(resolve, reject) {
resolve(mockUserEmails.map(function(info, i) {
return new Resource('/users/' + info.userId, info);
}));
});
}
// Updates a user
function putUserBasicInfo(request) {
var userId = request.getParam('userId');
var resource = request.getResource();
return new Promise(function(resolve, reject) {
for (var i = 0, len = mockUserBasicInfo.length; i < len; i++) {
if (mockUserBasicInfo[i].userId === userId) {
for (var k in resource) {
mockUserBasicInfo[i][k] = resource[k];
}
return resolve(new Resource('/users/' + userId, mockUserBasicInfo[i], 3600));
}
}
reject('Unable to find basic info for user id ' + userId);
});
}
function putUserEmailInfo(request) {
var userId = request.getParam('userId');
var resource = request.getResource();
return new Resource('/users/' + userId, resource, 3600);
}
// Gets all users
function getUsers(request, connection) {
// The limit is guaranteed to be an integer and within range, or default value
var limit = request.getQuery('limit');
// The offset is guaranteed to be an integer and within range, or default value
var offset = request.getQuery('offset');
var search = request.getQuery('search');
var users = mockUserBasicInfo.slice(0); // Make a copy of users
if (search) {
search = search.toLowerCase();
users = users.filter(function(user) {
return (-1 !== user.displayName.toLowerCase().indexOf(search));
});
}
var users = users.slice(offset, offset + limit).map(function(user) {
return new Symlink('/users/' + user.userId);
});
return new OffsetPaginator('/users')
.setItems(users)
.setExpires(1000)
.setLimit(limit)
.setOffset(offset);
}
// Create a user
function createUser(request) {
}
// A very simple photo upload page
app.use(function(req, res, next) {
if (req.url !== '/upload-photo' || req.method !== 'GET') {
return next();
}
res.setHeader('Content-Type', 'text/html');
res.write('\
<form method="POST" enctype="multipart/form-data" action="/my-api/users/1/photos">\
<input type="file" name="photo" />\
<textarea name="caption" placeholder="caption"></textarea>\
<button>Upload</button>\
</form>\
');
res.end();
});
/*** Set up some aliases ***/
api.alias('/alice', '/users/1');
/*** Set up some aliases ***/
api.alias('/random', function(request, connection) {
var userId = Math.ceil(Math.random() * mockUserBasicInfo.length);
request.setResourceId('/users/' + userId);
return request;
});
/*** Set up some aliases ***/
api.alias('/me', function(request, connection) {
request.setResourceId('/users/2');
return request;
});
// Allow uploaded files to be served
var serveStatic = require('serve-static');
app.use(serveStatic(path.join(__dirname, 'files')));
var swagger = require('swagger-ui-dist').getAbsoluteFSPath();
app.use("/swagger", chain(
function(req, res, next) {
if(req.method === "GET" && req.url === '/') {
res.writeHead(302, {'location': '?url=http://localhost:5150/my-api'});
return res.end();
}
next();
},
serveStatic(swagger)
));
// Mount the API as middleware
app.use(api.middleware({
basePath: '/my-api'
}));
api.on('api:success', function(data) {
console.log('Success!!', data.resourceId);
});
api.on('api:error', function(data) {
console.log('Error :(', data.resourceId);
});
// Create web server and listen on port 5150
var http = require('http');
http.createServer(app).listen(5150, function() {
console.log("API Router Demo listening on port 5150");
console.log("View documentation: http://localhost:5150/my-api");
});