Add X-Count header when slicing collection

This commit is contained in:
Emmanuel Quentin
2014-08-26 13:35:15 +02:00
parent 96f2112231
commit daf3da70e0
2 changed files with 13 additions and 6 deletions

View File

@ -36,6 +36,9 @@ routes.list = function(req, res, next) {
} }
if (_start) { if (_start) {
res.setHeader('X-Count', resource.length)
res.setHeader('Access-Control-Expose-Headers', 'X-Count')
resource = resource.slice(_start, _end) resource = resource.slice(_start, _end)
} }
@ -56,7 +59,7 @@ routes.create = function(req, res, next) {
for (var key in req.body) { for (var key in req.body) {
req.body[key] = utils.toNative(req.body[key]) req.body[key] = utils.toNative(req.body[key])
} }
var resource = low(req.params.resource) var resource = low(req.params.resource)
.insert(req.body) .insert(req.body)
.value() .value()
@ -74,14 +77,14 @@ routes.update = function(req, res, next) {
var resource = low(req.params.resource) var resource = low(req.params.resource)
.update(+req.params.id, req.body) .update(+req.params.id, req.body)
.value() .value()
res.jsonp(resource) res.jsonp(resource)
} }
// DELETE /:resource/:id // DELETE /:resource/:id
routes.destroy = function(req, res, next) { routes.destroy = function(req, res, next) {
low(req.params.resource).remove(+req.params.id) low(req.params.resource).remove(+req.params.id)
// Remove dependents documents // Remove dependents documents
var removable = utils.getRemovable(low.db) var removable = utils.getRemovable(low.db)
@ -92,4 +95,4 @@ routes.destroy = function(req, res, next) {
res.send(204) res.send(204)
} }
module.exports = routes module.exports = routes

View File

@ -61,7 +61,11 @@ describe('Server', function() {
.get('/comments?_start=1&_end=2') .get('/comments?_start=1&_end=2')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(low.db.comments.slice(1, 2)) .expect(low.db.comments.slice(1, 2))
.expect(200, done) .expect(200)
.end(function(err, res){
assert.equal(res.headers['x-count'], 5)
done()
})
}) })
}) })
@ -171,4 +175,4 @@ describe('Server', function() {
}); });
}) })
}) })