Comparing version 0.6.3 to 0.7.0
@@ -10,3 +10,2 @@ /*jshint camelcase:false */ | ||
var __ = require('lodash'); | ||
var middlewarify = require('middlewarify'); | ||
var appErr = require('nodeon-error'); | ||
@@ -65,8 +64,17 @@ | ||
// define CRUD handlers | ||
middlewarify.make(this, 'create', this._create.bind(this)); | ||
middlewarify.make(this, 'readOne', this._readOne.bind(this)); | ||
middlewarify.make(this, 'readList', this._readList.bind(this)); | ||
middlewarify.make(this, 'update', this._update.bind(this)); | ||
middlewarify.make(this, 'delete', this._delete.bind(this)); | ||
this.create = [this._create.bind(this)]; | ||
this.readOne = [this._readOne.bind(this)]; | ||
this.readList = [this._readList.bind(this)]; | ||
this.update = [this._update.bind(this)]; | ||
this.delete = [this._delete.bind(this)]; | ||
function use(middleware) { | ||
this.unshift(middleware); | ||
} | ||
this.create.use = use; | ||
this.readOne.use = use; | ||
this.readList.use = use; | ||
this.update.use = use; | ||
this.delete.use = use; | ||
this._setDefaults(); | ||
@@ -73,0 +81,0 @@ |
{ | ||
"name": "crude", | ||
"description": "Creates CRUD RESTfull endpoints for a given route", | ||
"version": "0.6.3", | ||
"version": "0.7.0", | ||
"homepage": "https://github.com/thanpolas/crude", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -56,2 +56,4 @@ # Crude | ||
- **v0.7.0**, *14 Sep 2014* | ||
- Middleware are now of express type vs being Promises. | ||
- **v0.6.3**, *11 Sep 2014* | ||
@@ -58,0 +60,0 @@ - Issue a HTTP Bad Request error code (400) by default vs Internal Error (500). |
@@ -39,7 +39,7 @@ /** | ||
expect(this.crude.onError).to.be.a('function'); | ||
expect(this.crude.create).to.be.a('function'); | ||
expect(this.crude.readOne).to.be.a('function'); | ||
expect(this.crude.readList).to.be.a('function'); | ||
expect(this.crude.update).to.be.a('function'); | ||
expect(this.crude.delete).to.be.a('function'); | ||
expect(this.crude.create).to.be.an('array'); | ||
expect(this.crude.readOne).to.be.an('array'); | ||
expect(this.crude.readList).to.be.an('array'); | ||
expect(this.crude.update).to.be.an('array'); | ||
expect(this.crude.delete).to.be.an('array'); | ||
}); | ||
@@ -46,0 +46,0 @@ it('should generate a new instance', function() { |
@@ -39,5 +39,5 @@ /** | ||
return Promise.all([ | ||
this.crude.readOne(this.reqres.req, this.reqres.res), | ||
this.crude.update(this.reqres.req, this.reqres.res), | ||
this.crude.delete(this.reqres.req, this.reqres.res), | ||
this.crude._readOne(this.reqres.req, this.reqres.res), | ||
this.crude._update(this.reqres.req, this.reqres.res), | ||
this.crude._delete(this.reqres.req, this.reqres.res), | ||
]); | ||
@@ -44,0 +44,0 @@ }); |
@@ -44,3 +44,3 @@ /** | ||
this.ctrl.readLimit.throws(this.err); | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -54,3 +54,3 @@ .then(runAssert('paginate')) | ||
this.crude.config({pagination: false}); | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -63,3 +63,3 @@ .then(runAssert('read')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.readOne(this.reqres.req, this.reqres.res) | ||
return this.crude._readOne(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -72,3 +72,3 @@ .then(runAssert('readOne')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.update(this.reqres.req, this.reqres.res) | ||
return this.crude._update(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -81,3 +81,3 @@ .then(runAssert('update')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.delete(this.reqres.req, this.reqres.res) | ||
return this.crude._delete(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -89,3 +89,3 @@ .then(runAssert('delete')) | ||
this.ctrl.create.throws(this.err); | ||
return this.crude.create(this.reqres.req, this.reqres.res) | ||
return this.crude._create(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -113,3 +113,3 @@ .then(runAssert('create')) | ||
this.ctrl.readLimit.throws(this.err); | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -123,3 +123,3 @@ .then(runAssert('paginate')) | ||
this.crude.config({pagination: false}); | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -132,3 +132,3 @@ .then(runAssert('read')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.readOne(this.reqres.req, this.reqres.res) | ||
return this.crude._readOne(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -141,3 +141,3 @@ .then(runAssert('readOne')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.update(this.reqres.req, this.reqres.res) | ||
return this.crude._update(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -150,3 +150,3 @@ .then(runAssert('update')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.delete(this.reqres.req, this.reqres.res) | ||
return this.crude._delete(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -158,3 +158,3 @@ .then(runAssert('delete')) | ||
this.ctrl.create.throws(this.err); | ||
return this.crude.create(this.reqres.req, this.reqres.res) | ||
return this.crude._create(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -193,3 +193,3 @@ .then(runAssert('create')) | ||
it('should work on pagination', function (done) { | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -202,3 +202,3 @@ .then(runAssert('paginate', null, true)) | ||
this.crude.config({pagination: false}); | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -210,3 +210,3 @@ .then(runAssert('read', null, true)) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.readOne(this.reqres.req, this.reqres.res) | ||
return this.crude._readOne(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -218,3 +218,3 @@ .then(runAssert('readOne')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.update(this.reqres.req, this.reqres.res) | ||
return this.crude._update(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -226,3 +226,3 @@ .then(runAssert('update')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.delete(this.reqres.req, this.reqres.res) | ||
return this.crude._delete(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -233,3 +233,3 @@ .then(runAssert('delete')) | ||
it('should work on create', function (done) { | ||
return this.crude.create(this.reqres.req, this.reqres.res) | ||
return this.crude._create(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -249,3 +249,3 @@ .then(runAssert('create', 201)) | ||
it('should work on pagination', function (done) { | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -258,3 +258,3 @@ .then(runAssert('paginate', null, true)) | ||
this.crude.config({pagination: false}); | ||
return this.crude.readList(this.reqres.req, this.reqres.res) | ||
return this.crude._readList(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -266,3 +266,3 @@ .then(runAssert('read', null, true)) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.readOne(this.reqres.req, this.reqres.res) | ||
return this.crude._readOne(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -274,3 +274,3 @@ .then(runAssert('readOne')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.update(this.reqres.req, this.reqres.res) | ||
return this.crude._update(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -282,3 +282,3 @@ .then(runAssert('update')) | ||
this.reqres.req.params.id = 'one'; | ||
return this.crude.delete(this.reqres.req, this.reqres.res) | ||
return this.crude._delete(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -289,3 +289,3 @@ .then(runAssert('delete')) | ||
it('should work on create', function (done) { | ||
return this.crude.create(this.reqres.req, this.reqres.res) | ||
return this.crude._create(this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
@@ -292,0 +292,0 @@ .then(runAssert('create', 201)) |
/** | ||
* @fileOverview Middleware tests. | ||
*/ | ||
var Promise = require('bluebird'); | ||
var chai = require('chai'); | ||
@@ -21,2 +20,8 @@ var expect = chai.expect; | ||
function runMiddleware(op) { | ||
op.forEach(function (midd) { | ||
midd(this.reqres.req, this.reqres.res); | ||
}, this); | ||
} | ||
function runAssert () { | ||
@@ -34,58 +39,8 @@ expect(this.stub).to.have.been.calledOnce; | ||
} | ||
this.crude[operation](this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
.then(runAssert) | ||
.then(done, done); | ||
runMiddleware.call(this, this.crude[operation]); | ||
runAssert.call(this); | ||
done(); | ||
}); | ||
it('should accept a promise and be async. OP: ' + operation + ' single: ' + isSingle, function (done) { | ||
var defer = Promise.defer(); | ||
var asyncOk = false; | ||
this.stub.returns(defer.promise); | ||
if (isSingle) { | ||
this.crude[operation].use(this.stub); | ||
} else { | ||
this.crude.use(this.stub); | ||
} | ||
this.crude[operation](this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
.then(runAssert) | ||
.then(function () { | ||
expect(asyncOk).to.be.true; | ||
}) | ||
.then(done, done); | ||
setTimeout(function () { | ||
asyncOk = true; | ||
defer.resolve(); | ||
}, 30); | ||
}); | ||
it('should not invoke CRUD OP if error thrown for OP: ' + operation + ' single:' + isSingle, function (done) { | ||
this.stub.throws(new Error()); | ||
if (isSingle) { | ||
this.crude[operation].use(this.stub); | ||
} else { | ||
this.crude.use(this.stub); | ||
} | ||
var crudeOp = operation; | ||
if (operation === 'readList') { | ||
crudeOp = 'readLimit'; | ||
} | ||
this.crude[operation](this.reqres.req, this.reqres.res) | ||
.bind(this) | ||
.catch(runAssert) | ||
.bind(this) | ||
.then(function () { | ||
expect(this.ctrl[crudeOp]).to.not.have.been.called; | ||
}) | ||
.then(done, done); | ||
}); | ||
} | ||
describe('Per CRUD OP', function () { | ||
@@ -92,0 +47,0 @@ // create tests |
89
68839
1623