mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
Support string id in /resources/:id
This commit is contained in:
@ -132,7 +132,7 @@ module.exports = function(source) {
|
|||||||
// GET /:resource/:id
|
// GET /:resource/:id
|
||||||
function show(req, res, next) {
|
function show(req, res, next) {
|
||||||
var resource = db(req.params.resource)
|
var resource = db(req.params.resource)
|
||||||
.get(+req.params.id)
|
.get(utils.toNative(req.params.id))
|
||||||
|
|
||||||
if (resource) {
|
if (resource) {
|
||||||
res.jsonp(resource)
|
res.jsonp(resource)
|
||||||
@ -161,7 +161,7 @@ module.exports = function(source) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var resource = db(req.params.resource)
|
var resource = db(req.params.resource)
|
||||||
.update(+req.params.id, req.body)
|
.update(utils.toNative(req.params.id), req.body)
|
||||||
|
|
||||||
if (resource) {
|
if (resource) {
|
||||||
res.jsonp(resource)
|
res.jsonp(resource)
|
||||||
@ -172,7 +172,7 @@ module.exports = function(source) {
|
|||||||
|
|
||||||
// DELETE /:resource/:id
|
// DELETE /:resource/:id
|
||||||
function destroy(req, res, next) {
|
function destroy(req, res, next) {
|
||||||
db(req.params.resource).remove(+req.params.id)
|
db(req.params.resource).remove(utils.toNative(req.params.id))
|
||||||
|
|
||||||
// Remove dependents documents
|
// Remove dependents documents
|
||||||
var removable = utils.getRemovable(db.object)
|
var removable = utils.getRemovable(db.object)
|
||||||
|
@ -167,6 +167,14 @@ describe('Server', function() {
|
|||||||
.expect(200, done)
|
.expect(200, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should support string id, respond with json and corresponding resource', function(done) {
|
||||||
|
request(server)
|
||||||
|
.get('/refs/abcd-1234')
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(db.refs[0])
|
||||||
|
.expect(200, done)
|
||||||
|
})
|
||||||
|
|
||||||
it('should respond with 404 if resource is not found', function(done) {
|
it('should respond with 404 if resource is not found', function(done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/posts/9001')
|
.get('/posts/9001')
|
||||||
|
Reference in New Issue
Block a user