forked from travis4all/baucis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
431 lines (332 loc) · 12 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Dependencies
// ------------
var express = require('express');
var mongoose = require('mongoose');
var lingo = require('lingo');
var path = require('path');
// Private Members
// ---------------
var app = express();
// Set a field or array off fields to be populated
function populateQuery (query, populate) {
if (!query) throw new Error('Query was undefined');
if (!populate) return;
populate = JSON.parse(populate);
if (!Array.isArray(populate)) populate = [ populate ];
populate.forEach(function (field) { query.populate(field) });
}
// Module Definition
// -----------------
var baucis = module.exports = function (options) {
options = options || {};
//if (options.prefixUrl) app.set('urlPrefix', options.urlPrefix);
return app;
};
// Default Settings
// ----------------
//app.set('urlPrefix', '/api');
// Middleware
// ----------
// Functions to return middleware for HTTP verbs
// Retrieve header for the addressed document
function head (options) {
var f = function (request, response, next) {
var id = request.params.id;
var query = mongoose.model(options.singular).findById(id);
if (options.restrict) options.restrict(query, request);
populateQuery(query, request.query.populate);
query.count(function (error, count) {
if (error) return next(error);
if (count === 0) return response.send(404);
response.send(200);
});
};
return f;
}
// Retrieve the addressed document
function get (options) {
var f = function (request, response, next) {
var id = request.params.id;
var query = mongoose.model(options.singular).findById(id);
if (options.restrict) options.restrict(query, request);
populateQuery(query, request.query.populate);
query.exec(function (error, doc) {
if (error) return next(error);
if (!doc) return response.send(404);
response.json(doc);
});
};
return f;
}
// Treat the addressed document as a collection, and push
// the addressed object to it
function post (options) {
var f = function (request, response, next) {
response.send(405); // method not allowed (as of yet unimplemented)
};
return f;
}
// Replace the addressed document, or create it if it doesn't exist
function put (options) {
var f = function (request, response, next) {
// Can't send id for update, even if unchanged
delete request.body._id;
var id = request.params.id || null;
var create = (id === null);
var query = mongoose.model(options.singular).findByIdAndUpdate(id, request.body, {upsert: true});
if (options.restrict) options.restrict(query, request);
populateQuery(query, request.query.populate);
query.exec(function (error, doc) {
if (error) return next(error);
if (create) response.status(201);
else response.status(200);
response.set('Location', path.join(options.basePath, doc.id));
response.json(doc);
});
};
return f;
}
// Delete the addressed object
function del (options) {
var f = function (request, response, next) {
var id = request.params.id;
var query = mongoose.model(options.singular).remove({ _id: id });
if (options.restrict) options.restrict(query, request);
populateQuery(query, request.query.populate);
query.exec(function (error, count) {
if (error) return next(error);
response.json(count);
});
};
return f;
}
// Retrieve documents matching conditions
function headCollection (options) {
var f = function (request, response, next) {
var conditions;
var query;
if (request.query && request.query.conditions) {
conditions = JSON.parse(request.query.conditions);
}
query = mongoose.model(options.singular).find(conditions);
if (options.restrict) options.restrict(query, request);
populateQuery(query, request.query.populate);
query.count(function (error, count) {
if (error) return next(error);
response.send(200);
});
};
return f;
}
// retrieve documents matching conditions
function getCollection (options) {
var f = function (request, response, next) {
var firstWasProcessed = false;
var conditions;
if (request.query && request.query.conditions) {
conditions = JSON.parse(request.query.conditions);
}
var query = mongoose.model(options.singular).find(conditions);
if (options.restrict) options.restrict(query, request);
populateQuery(query, request.query.populate);
// Stream the array to the client
response.set('Content-Type', 'application/json');
response.write('[');
query.stream()
.on('data', function (doc) {
if (firstWasProcessed) response.write(', ');
response.write(JSON.stringify(doc.toJSON()));
firstWasProcessed = true;
})
.on('error', next)
.on('close', function () {
response.write(']');
response.send();
});
};
return f;
}
// Create a new document and return its ID
function postCollection (options) {
var f = function (request, response, next) {
var body = request.body;
// Must be object or array
if (!body || typeof body !== 'object') {
return next(new Error('Must supply a document or array to POST'));
}
response.status(201);
// If just one object short circuit
if (!Array.isArray(body)) {
return mongoose.model(options.singular).create(body, function (error, doc) {
if (error) return next(error);
response.set('Location', path.join(options.basePath, doc.id));
response.json(doc);
});
}
// No empty arrays
if (body.length === 0) return next(new Error('Array was empty.'));
// Create and save given documents
var promises = body.map(mongoose.model(options.singular).create);
var ids = [];
var processedCount = 0;
var location;
// Stream the response JSON array
response.set('Content-Type', 'application/json');
response.write('[');
promises.forEach(function (promise) {
promise.then(function (doc) {
response.write(JSON.stringify(doc.toJSON()));
ids.push(doc.id);
// Still more to process?
if (processedCount < body.length) return response.write(', ');
// Last one was processed
response.write(']');
location = options.basePath + '?query={ id: { $in: [' + ids.join(',') + '] } }';
response.set('Location', location);
response.send();
});
promise.error(function (error) {
next(new Error(error));
});
});
};
return f;
}
// Replace all docs with given docs ...
function putCollection (options) {
var f = function (request, response, next) {
response.send(405); // method not allowed (as of yet unimplemented)
};
return f;
}
// Delete all documents matching conditions
function delCollection (options) {
var f = function (request, response, next) {
var conditions = request.body || {};
var query = mongoose.model(options.singular).remove(conditions);
if (options.restrict) options.restrict(query, request);
populateQuery(query, request.query.populate);
query.exec(function (error, count) {
if (error) return next(error);
response.json(count);
});
};
return f;
}
// Add "Link" header field, with some basic defaults
function addLinkRelations (options) {
var f = function (request, response, next) {
response.links({
collection: options.basePath,
search: options.basePath,
edit: path.join(options.basePath, request.params.id),
self: path.join(options.basePath, request.params.id),
'latest-version': path.join(options.basePath, request.params.id)
});
next();
};
return f;
}
// Add "Link" header field, with some basic defaults (for collection routes)
function addLinkRelationsCollection (options) {
var f = function (request, response, next) {
response.links({
search: options.basePath,
self: options.basePath,
'latest-version': options.basePath
});
next();
};
return f;
}
// Build the "Allow" response header
function addAllowResponseHeader (options) {
var f = function (request, response, next) {
var allowed = [];
if (options.head !== false) allowed.push('HEAD');
if (options.get !== false) allowed.push('GET');
if (options.post !== false) allowed.push('POST');
if (options.put !== false) allowed.push('PUT');
if (options.del !== false) allowed.push('DELETE');
response.set('Allow', allowed.join(', '));
next();
};
return f;
}
// Build the "Accept" response header
function addAcceptResponseHeader (options) {
var f = function (request, response, next) {
response.set('Accept', 'application/json');
next();
};
return f;
}
// Validation
// ----------
// var validation = function (options) {
// var validators = {};
// var f = function (request, response, next) {
// response.json(validators);
// };
// Object.keys(s.paths).forEach(function (path) {
// var pathValidators = [];
// if (path.enumValues.length > 0) {
// // TODO
// pathValidators.push( );
// }
// if (path.regExp !== null) {
// // TODO
// pathValidators.push( );
// }
// // test path.instance TODO or path.options.type
// // TODO use any path.validators?
// // TODO other path.options?
// validators[path.path] = pathValidators;
// });
// return f;
// };
// Public Methods
// --------------
baucis.rest = function (options) {
options || (options = {}); // TODO clone
if (!options.singular) throw new Error('Must provide the Mongoose schema name');
if (!options.plural) options.plural = lingo.en.pluralize(options.singular);
var basePath = options.basePath = path.join('/', options.basePath);
var basePathWithId = options.basePathWithId = path.join(basePath, ':id');
var basePathWithOptionalId = options.basePathWithOptionalId = path.join(basePath, ':id?');
var controller = express();
controller.use(express.json());
controller.all(basePathWithId, addAllowResponseHeader(options));
controller.all(basePathWithId, addAcceptResponseHeader(options));
controller.all(basePath, addAllowResponseHeader(options));
controller.all(basePath, addAcceptResponseHeader(options));
if (options.configure) options.configure(controller);
if (options.relations === true) {
controller.head(basePathWithId, addLinkRelations(options));
controller.get(basePathWithId, addLinkRelations(options));
controller.post(basePathWithId, addLinkRelations(options));
controller.put(basePathWithId, addLinkRelations(options));
controller.head(basePath, addLinkRelationsCollection(options));
controller.get(basePath, addLinkRelationsCollection(options));
controller.post(basePath, addLinkRelationsCollection(options));
controller.put(basePath, addLinkRelationsCollection(options));
}
if (options.all) controller.all(basePathWithOptionalId, options.all);
if (options.head) controller.head(basePathWithOptionalId, options.head);
if (options.get) controller.get(basePathWithOptionalId, options.get);
if (options.post) controller.post(basePathWithOptionalId, options.post);
if (options.put) controller.put(basePathWithOptionalId, options.put);
if (options.del) controller.del(basePathWithOptionalId, options.del);
if (options.head !== false) controller.head(basePathWithId, head(options));
if (options.get !== false) controller.get(basePathWithId, get(options));
if (options.post !== false) controller.post(basePathWithId, post(options));
if (options.put !== false) controller.put(basePathWithId, put(options));
if (options.del !== false) controller.del(basePathWithId, del(options));
if (options.head !== false) controller.head(basePath, headCollection(options));
if (options.get !== false) controller.get(basePath, getCollection(options));
if (options.post !== false) controller.post(basePath, postCollection(options));
if (options.put !== false) controller.put(basePath, putCollection(options));
if (options.del !== false) controller.del(basePath, delCollection(options));
if (options.publish !== false) app.use(path.join('/', options.plural), controller);
return controller;
};