Comparing version 1.0.0-prerelease.3 to 1.0.0-prerelease.4
@@ -14,24 +14,8 @@ // __Dependencies__ | ||
api.add = function (controller) { | ||
controller.api(api); | ||
controllers.push(controller); | ||
return api; | ||
}; | ||
// Check the requested API version is valid. | ||
api.middleware.use(function (request, response, next) { | ||
var range = request.headers['api-version'] || '*'; | ||
if (semver.validRange(range)) return next(); | ||
next(BaucisError.BadRequest('The requested API version range "%s" was not a valid semver range', range)); | ||
}); | ||
// Check for API version unsatisfied and give a 400 if no versions match. | ||
api.middleware.use(function (request, response, next) { | ||
var range = request.headers['api-version'] || '*'; | ||
var apiVersionMatch = api.releases().some(function (release) { | ||
return semver.satisfies(release, range); | ||
}); | ||
if (apiVersionMatch) return next(); | ||
next(BaucisError.BadRequest('The requested API version range "%s" could not be satisfied', range)); | ||
}); | ||
// Find the correct controller to handle the request. | ||
api.middleware.use('/:path', function (request, response, next) { | ||
var stop = false; | ||
var found = false; | ||
var path = '/' + request.params.path; | ||
@@ -41,6 +25,16 @@ // Requested range is used to select highest possible release number. | ||
var range = request.headers['api-version'] || '*'; | ||
// Check the requested API version is valid. | ||
if (!semver.validRange(range)) { | ||
next(BaucisError.BadRequest('The requested API version range "%s" was not a valid semver range', range)); | ||
return; | ||
} | ||
var release = semver.maxSatisfying(api.releases(), range); | ||
// Check for API version unsatisfied and give a 400 if no versions match. | ||
if (!release) { | ||
next(BaucisError.BadRequest('The requested API version range "%s" could not be satisfied', range)); | ||
return; | ||
} | ||
// Set API-related headers | ||
response.set('API-Version', release); | ||
response.set('Vary', 'API-Version') | ||
response.set('Vary', 'API-Version'); | ||
// Filter to only controllers that match the requested release. | ||
@@ -50,12 +44,12 @@ var filteredControllers = controllers.filter(function (controller) { | ||
}); | ||
// Find the matching controller, if any. | ||
// Find the matching controller among controllers that match the requested release. | ||
filteredControllers.forEach(function (controller) { | ||
if (stop) return; | ||
if (found) return; | ||
if (path !== controller.baucisPath()) return; | ||
// Path and version match. | ||
stop = true; | ||
found = true; | ||
controller(request, response, next); | ||
}); | ||
if (!stop) next(BaucisError.Configuration('No mamsd')); | ||
if (!found) return next(); | ||
}); | ||
}; |
@@ -12,4 +12,11 @@ // __Dependencies__ | ||
var middleware = api.middleware = express(); | ||
var middleware = api.middleware = express.Router(); | ||
api.use(function (request, response, next) { | ||
if (request.baucis) return next(BaucisError.Configuration('Baucis request property already created')); | ||
request.baucis = { api: api }; | ||
response.removeHeader('x-powered-by'); | ||
next(); | ||
}); | ||
api.use(middleware); | ||
@@ -16,0 +23,0 @@ |
// __Dependencies__ | ||
var deco = require('deco'); | ||
var util = require('util'); | ||
// __Module Definition__ | ||
// Parent type for child baucis errors. | ||
var BaucisError = module.exports = deco().inherit(Error); | ||
// __Private Module Members__ | ||
@@ -29,3 +27,2 @@ // Build a constructor function for a Baucis error, with a custom default message | ||
}; | ||
// __Public Module Members__ | ||
@@ -32,0 +29,0 @@ BaucisError.BadRequest = buildConstructor({ |
# Baucis Change Log | ||
## v1.0.0-prerelease.4 | ||
Remove `request.baucis.controller`. Remove `controller.api`. | ||
## v1.0.0-prerelease.3 | ||
@@ -4,0 +8,0 @@ |
@@ -98,6 +98,2 @@ // __Dependencies__ | ||
}; | ||
// A method used to activate document-stage middleware. | ||
controller.documents = function (endpoint, methods, middleware) { | ||
throw BaucisError.Deprecated('The documents stage of middleware has been deprecated. Use an outgoing stream instead.') | ||
}; | ||
}; |
@@ -22,3 +22,2 @@ // __Dependencies__ | ||
protect.property('lastModified'); | ||
protect.property('api'); // TODO changing this is an NOP | ||
protect.property('parentPath'); | ||
@@ -34,3 +33,2 @@ | ||
// TODO changing after activation is an NOP | ||
protect.property('versions', '*', function (range) { | ||
@@ -41,3 +39,2 @@ if (semver.validRange(range)) return range; | ||
// TODO changing after activation is an NOP | ||
protect.property('model', undefined, function (m) { | ||
@@ -50,3 +47,2 @@ var baucis = require('..'); | ||
// TODO changing this after activation is a NOP | ||
protect.property( | ||
@@ -61,3 +57,2 @@ 'baucisPath', | ||
// TODO changing this after release init is an NOP | ||
protect.property('children', [], function (child) { | ||
@@ -72,7 +67,6 @@ var children = this.children(); | ||
if (!child.parentPath()) child.parentPath(controller.model().singular()); | ||
child.api(controller.api()); | ||
controller.use('/:parentId/:path', function (request, response, next) { | ||
var path = '/' + request.params.path; | ||
if (path !== child.baucisPath()) return next(); | ||
request.parentId = request.params.parentId; | ||
request.baucis.parentId = request.params.parentId; | ||
child(request, response, next); | ||
@@ -79,0 +73,0 @@ }); |
@@ -14,3 +14,2 @@ // __Dependencies__ | ||
'vivify', | ||
'initialize', | ||
'request', | ||
@@ -17,0 +16,0 @@ 'query', |
// __Module Definition__ | ||
var decorator = module.exports = function () { | ||
this.query('collection', '*', function (request, response, next) { | ||
var Model = request.baucis.controller.model().source(); | ||
var controller = this; | ||
controller.query('collection', '*', function (request, response, next) { | ||
var Model = controller.model().source(); | ||
request.baucis.query = Model.find(request.baucis.conditions); | ||
@@ -9,4 +11,4 @@ next(); | ||
this.query('instance', '*', function (request, response, next) { | ||
var Model = request.baucis.controller.model().source(); | ||
controller.query('instance', '*', function (request, response, next) { | ||
var Model = controller.model().source(); | ||
request.baucis.query = Model.findOne(request.baucis.conditions); | ||
@@ -13,0 +15,0 @@ next(); |
@@ -10,7 +10,2 @@ // __Dependencies__ | ||
function embed (controller, child) { | ||
controller.children(child); | ||
return controller; | ||
}; | ||
controller.vivify = function (path) { | ||
@@ -32,3 +27,3 @@ var definition = controller.model().schema().path(path); | ||
var path = child.parentPath(); | ||
if (!context.incoming[path]) context.incoming[path] = request.parentId; | ||
if (!context.incoming[path]) context.incoming[path] = request.baucis.parentId; | ||
callback(null, context); | ||
@@ -41,3 +36,3 @@ }); | ||
var conditions = {}; | ||
conditions[child.parentPath()] = request.parentId; | ||
conditions[child.parentPath()] = request.baucis.parentId; | ||
request.baucis.query.where(conditions); | ||
@@ -47,3 +42,3 @@ next(); | ||
embed(controller, child); | ||
controller.children(child); | ||
@@ -50,0 +45,0 @@ return child; |
@@ -14,3 +14,3 @@ { | ||
"homepage": "https://github.com/wprl/baucis", | ||
"version": "1.0.0-prerelease.3", | ||
"version": "1.0.0-prerelease.4", | ||
"main": "index.js", | ||
@@ -17,0 +17,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
# baucis v1.0.0-prerelease.3 | ||
# baucis v1.0.0-prerelease.4 | ||
@@ -3,0 +3,0 @@ Baucis enables you to build scalable REST APIs using the open source tools and standards you and your team already know. Like Baucis and Philemon of old, the module provides REST to the weary traveler. [Baucis](https://en.wikipedia.org/wiki/Baucis_and_Philemon) is not the same as [Bacchus](https://en.wikipedia.org/wiki/Dionysus). |
@@ -79,15 +79,5 @@ var mongoose = require('mongoose'); | ||
stores.request(function (request, response, next) { | ||
if (request.baucis.controller === stores) return next(); | ||
next(new Error('request.baucis.controller set incorrectly!')); | ||
}); | ||
// Tools embedded controller | ||
var storeTools = stores.vivify('tools'); | ||
storeTools.request(function (request, response, next) { | ||
if (request.baucis.controller === storeTools) return next(); | ||
next(new Error('request.baucis.controller set incorrectly!')); | ||
}); | ||
storeTools.query(function (request, response, next) { | ||
@@ -94,0 +84,0 @@ request.baucis.query.where('bogus', false); |
@@ -33,13 +33,3 @@ var mongoose = require('mongoose'); | ||
users.request(function (request, response, next) { | ||
if (request.baucis.controller === users) return next(); | ||
next(new Error('request.baucis.controller set incorrectly!')); | ||
}); | ||
tasks.request(function (request, response, next) { | ||
if (request.baucis.controller === tasks) return next(); | ||
next(new Error('request.baucis.controller set incorrectly!')); | ||
}); | ||
tasks.request(function (request, response, next) { | ||
request.baucis.outgoing(function (context, callback) { | ||
@@ -46,0 +36,0 @@ context.doc.name = 'Changed by Middleware'; |
348940
58
4409