mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 17:42:19 +08:00
Convert props to native for POST, PUT, PATCH
This commit is contained in:
@ -53,6 +53,10 @@ routes.show = function(req, res, next) {
|
|||||||
|
|
||||||
// POST /:resource
|
// POST /:resource
|
||||||
routes.create = function(req, res, next) {
|
routes.create = function(req, res, next) {
|
||||||
|
for (var key in req.body) {
|
||||||
|
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()
|
||||||
@ -63,6 +67,10 @@ routes.create = function(req, res, next) {
|
|||||||
// PUT /:resource/:id
|
// PUT /:resource/:id
|
||||||
// PATCH /:resource/:id
|
// PATCH /:resource/:id
|
||||||
routes.update = function(req, res, next) {
|
routes.update = function(req, res, next) {
|
||||||
|
for (var key in req.body) {
|
||||||
|
req.body[key] = utils.toNative(req.body[key])
|
||||||
|
}
|
||||||
|
|
||||||
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()
|
||||||
|
@ -92,9 +92,9 @@ describe('Server', function() {
|
|||||||
it('should respond with json and create a resource', function(done) {
|
it('should respond with json and create a resource', function(done) {
|
||||||
request(server)
|
request(server)
|
||||||
.post('/posts')
|
.post('/posts')
|
||||||
.send({body: 'foo'})
|
.send({body: 'foo', booleanValue: 'true', integerValue: '1'})
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect({id: 3, body: 'foo'})
|
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.end(function(err, res){
|
.end(function(err, res){
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
@ -108,13 +108,13 @@ describe('Server', function() {
|
|||||||
it('should respond with json and update resource', function(done) {
|
it('should respond with json and update resource', function(done) {
|
||||||
request(server)
|
request(server)
|
||||||
.put('/posts/1')
|
.put('/posts/1')
|
||||||
.send({id: 1, body: 'foo'})
|
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect({id: 1, body: 'foo'})
|
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.end(function(err, res){
|
.end(function(err, res){
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
assert.deepEqual(low.db.posts[0], {id: 1, body: 'foo'})
|
assert.deepEqual(low.db.posts[0], {id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user