restful-goose
Advanced tools
Comparing version 1.3.7 to 1.3.8
@@ -53,3 +53,3 @@ var _ = require('lodash'); | ||
omittedPaths = _.concat(omittedPaths, Object.keys(refs)); | ||
attrs = _.mapKeys(_.omit(doc.toObject(), omittedPaths), function(v,k) { | ||
attrs = _.mapKeys(_.omit(doc.toObject({ virtuals: true }), omittedPaths), function(v,k) { | ||
return _.kebabCase(k).toLowerCase(); | ||
@@ -56,0 +56,0 @@ }); |
@@ -15,2 +15,5 @@ var crypto = require('crypto'); | ||
case 'MongoError': | ||
return 409; | ||
case 'ValidationError': | ||
@@ -35,3 +38,3 @@ return 409; | ||
var obj = {}; | ||
//TODO Fill out the About links object | ||
obj.id = crypto.randomBytes(4).toString('hex'); | ||
@@ -41,9 +44,21 @@ obj.links = { | ||
}; | ||
obj.status = res.status; | ||
obj.title = error.message; | ||
obj.detail = error.errors; | ||
//TODO Figure out how to satisfy JSON API spec for source string | ||
obj.source = error.detail; | ||
if (error.name === 'ValidationError') { | ||
obj.status = res.status; | ||
obj.title = error.message; | ||
obj.detail = error.errors || error.message; | ||
obj.source = error.path; | ||
} else if (error.name === 'MongoError' && error.code === 11000) { | ||
obj.status = res.status; | ||
obj.title = 'Invalid ' + error.path; | ||
obj.detail = error.value + ' is already used'; | ||
obj.source = error.detail; | ||
} else { | ||
obj.status = res.status; | ||
obj.title = error.message; | ||
obj.detail = error.name; | ||
obj.source = error.detail; | ||
} | ||
return obj; | ||
@@ -55,3 +70,2 @@ } | ||
} | ||
var errorsArray; | ||
@@ -58,0 +72,0 @@ var errStatus = getErrCode(err[0].name); |
@@ -36,7 +36,5 @@ var express = require('express'); | ||
if (req.parent && req.parentPath) { | ||
console.log('hasParent'); | ||
if (!req.query) { | ||
req.query = {}; | ||
} | ||
console.log('parentPath: ' + req.parentPath); | ||
req.query[req.parentPath] = req.parent._id; | ||
@@ -80,3 +78,3 @@ } | ||
if (!obj || obj.hasOwnProperty('attributes') === false) { | ||
if (!obj || (obj.hasOwnProperty('attributes') === false && obj.hasOwnProperty('relationships') === false)) { | ||
return res.__onError(req, res, { name: 'BadRequest', message: 'Request not a JSON object or attributes property missing' }); | ||
@@ -116,3 +114,3 @@ } | ||
if (!data || !data.attributes) { | ||
if (!data || (data.hasOwnProperty('attributes') === false && data.hasOwnProperty('relationships') === false)) { | ||
return res.__onError(req, res, { name: 'BadRequest', message: 'Request not an object or missing attributes property', detail: { request_body: req.body }}); | ||
@@ -154,3 +152,3 @@ } | ||
res.status(200).json({}); | ||
res.status(204).send(); | ||
}); | ||
@@ -157,0 +155,0 @@ }); |
{ | ||
"name": "restful-goose", | ||
"version": "1.3.7", | ||
"version": "1.3.8", | ||
"description": "Yet another RESTful microservice generator for Mongoose with an emphasis on flexibility", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,3 @@ var assert = require('chai').assert; | ||
var restfulGoose = require('../index'); | ||
var _ = require('lodash'); | ||
@@ -27,2 +28,5 @@ var createTestObjects = 10; | ||
rank: Number, | ||
uniquePath: { type: Number, unique: true, default: function() { | ||
return _.random(1, 900000); | ||
}}, | ||
createdAt: { type: Date, default: Date.now } | ||
@@ -47,3 +51,3 @@ }); | ||
var _create = function(next) { | ||
Model.create({ name: faker.name.firstName(), rank: faker.random.number() }, function(err, doc) { | ||
Model.create({ name: faker.name.firstName(), rank: faker.random.number(), uniquePath: _.random(1, 600000) + count }, function(err, doc) { | ||
count++; | ||
@@ -50,0 +54,0 @@ testDocs.push(doc); |
@@ -78,3 +78,3 @@ var chai = require('chai'); | ||
if (err) { | ||
console.error(err); | ||
return n(err); | ||
} | ||
@@ -156,3 +156,3 @@ count++; | ||
}); | ||
it('should respond with 200 success on /:item DELETE', function(done) { | ||
it('should respond with 204 success on /:item DELETE', function(done) { | ||
var item = _.sample(items); | ||
@@ -162,4 +162,3 @@ chai.request(app) | ||
.end(function(err, res) { | ||
expect(res.status).to.equal(200); | ||
expect(res).to.be.json; | ||
expect(res.status).to.equal(204); | ||
expect(res.body).to.be.empty; | ||
@@ -239,3 +238,3 @@ items.splice(_.findIndex(items, item), 1); | ||
}); | ||
it('should respond with 200 success on /:item DELETE', function(done) { | ||
it('should respond with 204 success on /:item DELETE', function(done) { | ||
var item = _.sample(items); | ||
@@ -245,4 +244,3 @@ chai.request(app) | ||
.end(function(err, res) { | ||
expect(res.status).to.equal(200); | ||
expect(res).to.be.json; | ||
expect(res.status).to.equal(204); | ||
expect(res.body).to.be.empty; | ||
@@ -249,0 +247,0 @@ items.splice(_.findIndex(items, item), 1); |
@@ -48,4 +48,3 @@ var chai = require('chai'); | ||
.end(function(err, res) { | ||
expect(res.status).to.equal(200); | ||
expect(res).to.be.json; | ||
expect(res.status).to.equal(204); | ||
expect(res.body).to.be.empty; | ||
@@ -52,0 +51,0 @@ done(); |
@@ -67,3 +67,3 @@ var chai = require('chai'); | ||
if (err) { | ||
console.error(err); | ||
return n(err); | ||
} | ||
@@ -70,0 +70,0 @@ count++; |
@@ -32,3 +32,4 @@ var chai = require('chai'); | ||
name: faker.name.firstName(), | ||
rank: getRank() | ||
rank: getRank(), | ||
uniquePath: _.random(1, 600000) + count | ||
}; | ||
@@ -80,3 +81,16 @@ Model.create(data, function (err, doc) { | ||
it('should get error status 409 when trying to create a duplicate item on / POST', function(done) { | ||
var data = { data: { attributes: { name: faker.name.firstName(), rank: faker.random.number(), uniquePath: _.sample(items).uniquePath }}}; | ||
chai.request(app) | ||
.post('/tests') | ||
.set('Content-Type', 'application/vnd.api+json') | ||
.send(JSON.stringify(data)) | ||
.end(function(err, res) { | ||
expect(res.status).to.equal(409); | ||
expect(res).to.be.json; | ||
expect(res.body).to.have.property('errors'); | ||
expect(res.body.errors).to.be.a('array'); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -78,3 +78,3 @@ var faker = require('faker'); | ||
if (err) { | ||
console.error(err); | ||
return n(err); | ||
} | ||
@@ -102,3 +102,3 @@ count++; | ||
if (err) { | ||
console.error(err); | ||
return n(err); | ||
} | ||
@@ -105,0 +105,0 @@ items = results; |
@@ -67,3 +67,3 @@ var chai = require('chai'); | ||
if (err) { | ||
console.error(err); | ||
return n(err); | ||
} | ||
@@ -70,0 +70,0 @@ count++; |
69083
4
1742