Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

baucis

Package Overview
Dependencies
Maintainers
1
Versions
202
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baucis - npm Package Compare versions

Comparing version 0.5.6 to 0.5.7

40

Controller.js

@@ -19,2 +19,4 @@ // Dependencies

// ----------------------
// Create a data structure to store user-defined middleware
function createEmptyMiddlewareHash () {

@@ -36,2 +38,3 @@ var o = {};

// Cascade optional paramaters into a single hash
function cascadeArguments (stage, howMany, verbs, middleware) {

@@ -53,2 +56,4 @@ if (!stage) throw new Error('Must supply stage.');

if (middleware.verbs) middleware.verbs = middleware.verbs.toLowerCase();
return { stage: stage, howMany: howMany, verbs: verbs, middleware: middleware };

@@ -58,8 +63,9 @@ }

// Module Definition
// -----------------
var Controller = module.exports = function (options) {
// Marshal string into a hash
if (typeof options === 'string') options = { singular: options };
// Validation
// ----------
if (!options.singular) throw new Error('Must provide the Mongoose schema name');
if (options.basePath && options.basePath !== '/') {
if (options.basePath) {
if (options.basePath.indexOf('/') !== 0) throw new Error('basePath must start with a "/"');

@@ -70,3 +76,2 @@ if (options.basePath.lastIndexOf('/') === options.basePath.length - 1) throw new Error('basePath must not end with a "/"');

// Private Instance Members
// --------------------------
var controller = express();

@@ -80,2 +85,4 @@ var initialized = false;

// Parse the options hash and recurse `f` with parsed paramaters. Execute `g`
// for each verb.
function traverseMiddleware (options, f, g) {

@@ -104,5 +111,12 @@ if (!options.stage) throw new Error('Must supply stage.');

// Register user middleware to be activated later
function registerMiddleware (options) {
if (initialized) throw new Error("Can't add middleware after the controller has been initialized.");
if (initialized) {
throw new Error("Can't add middleware after the controller has been initialized.");
}
if (options.verbs && options.verbs.indexOf('post') !== -1) {
throw new Error('Query stage not executed for POST.');
}
traverseMiddleware(options, registerMiddleware, function (verb) {

@@ -120,2 +134,3 @@ if (controller.get(verb) === false) return;

// Activate user middleware that was registered previously
function activateMiddleware (options) {

@@ -148,2 +163,7 @@ if (initialized) throw new Error("Can't activate middleware after the controller has been initialized.");

var cascaded = cascadeArguments('query', howMany, verbs, middleware);
// Prevent explicitly setting query:post middleware. Implicitly adding
// query:post middleware is ignored.
if (cascaded.verbs && cascaded.verbs.indexOf('post') !== -1) {
throw new Error('POST cannot have query middleware');
}
registerMiddleware(cascaded);

@@ -189,5 +209,11 @@ return controller;

});
// Delete any POST-stage query middleware that was added implicitly
delete userMiddlewareFor['query']['instance']['post'];
delete userMiddlewareFor['query']['collection']['post'];
activateMiddleware({
stage: 'query',
middleware: userMiddlewareFor['query'] // TODO post doesn't have query
verbs: 'head get put del',
middleware: userMiddlewareFor['query']
});

@@ -245,2 +271,4 @@ activateMiddleware({

// The controller is initialized and we don't need the intermiediate data
// structure any more.
delete userMiddlewareFor;

@@ -247,0 +275,0 @@ initialized = true;

2

package.json

@@ -5,3 +5,3 @@ {

"homepage": "https://github.com/wprl/baucis",
"version": "0.5.6",
"version": "0.5.7",
"main": "index.js",

@@ -8,0 +8,0 @@ "scripts": {

@@ -1,2 +0,2 @@

baucis v0.5.6
baucis v0.5.7
=============

@@ -39,10 +39,5 @@

// Create the API routes
baucis.rest({
singular: 'vegetable',
});
baucis.rest('vegetable');
baucis.rest('fruit');
baucis.rest({
singular: 'fruit'
});
// Create the app and listen for API requests

@@ -106,5 +101,3 @@ var app = express();

var controller = baucis.rest({
singular: 'robot'
});
var controller = baucis.rest({ singular: 'robot' });

@@ -139,3 +132,3 @@ // Add middleware before API routes

| request | This stage of middleware will be called after baucis applies defaults based on the request, but before the Mongoose query is generated |
| query | This stage of middleware will be called after baucis applies defaults to the Mongoose query object, but before the documents or count are retrieved from the database. The query can be accessed in your custom middleware via `request.baucis.query`. |
| query | This stage of middleware will be called after baucis applies defaults to the Mongoose query object, but before the documents or count are retrieved from the database. The query can be accessed in your custom middleware via `request.baucis.query`. Query middleware cannot be added explicitly for POST and will be ignored when added for POST implicitly. |
| documents | This stage of middleware will be called after baucis executes the query, but before the documents or count are sent in the response. The documents/count can be accessed in your custom middleware via `request.baucis.documents`. |

@@ -152,3 +145,3 @@

controller.query('post put', function (request, response, next) {
controller.query('get put', function (request, response, next) {
// do something with request.baucis.query

@@ -155,0 +148,0 @@ next();

@@ -16,2 +16,8 @@ var expect = require('expect.js');

it('should allow passing string name only to create', function (done) {
var makeController = function () { baucis.rest('store') };
expect(makeController).to.not.throwException();
done();
});
it('should support select options', function (done) {

@@ -18,0 +24,0 @@ var options = {

var expect = require('expect.js');
var request = require('request');
var baucis = require('..');

@@ -64,2 +65,16 @@ var fixtures = require('./fixtures');

it('should not allow query middleware to be explicitly registered', function (done) {
var badController = baucis.rest('vegetable');
var registerQueryMiddleware = function () { badController.query('post', function () {}) };
expect(registerQueryMiddleware).to.throwException();
done();
});
it('should allow ignore implicitly registered query middleware', function (done) {
var controller = baucis.rest('vegetable');
var registerQueryMiddleware = function () { controller.query(function () {}) };
expect(registerQueryMiddleware).not.to.throwException();
done();
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc