Skip to content

Commit

Permalink
Add support for query params in rewrite targets (#373)
Browse files Browse the repository at this point in the history
* Add support for query params in rewrite targets

Re-parse the rewrite target to add query params, fixes #372

* Use lodash for Object.assign for node v0.12 compatibility
  • Loading branch information
ashleahhill authored and typicode committed Oct 1, 2016
1 parent e547381 commit b06bc27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/server/rewriter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var express = require('express')
var url = require('url')
var _ = require('lodash')

module.exports = function (routes) {
var router = express.Router()
Expand All @@ -12,6 +14,10 @@ module.exports = function (routes) {
target = target.replace(':' + param, req.params[param])
}
req.url = target
if (target.indexOf('?')) {
// create query from target
_.assign(req.query, url.parse(target, true).query)
}
next()
})
} else {
Expand Down
10 changes: 9 additions & 1 deletion test/server/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ describe('Server', function () {
server.use(jsonServer.defaults())
server.use(jsonServer.rewriter({
'/api/': '/',
'/blog/posts/:id/show': '/posts/:id'
'/blog/posts/:id/show': '/posts/:id',
'/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body'
}))
server.use(router)
})
Expand Down Expand Up @@ -647,6 +648,13 @@ describe('Server', function () {
.expect(db.posts[0])
.end(done)
})

it('should rewrite using params and query', function (done) {
request(server)
.get('/comments/special/1-quux')
.expect([db.comments[4]])
.end(done)
})
})

describe('router.render', function (done) {
Expand Down

0 comments on commit b06bc27

Please sign in to comment.