From 712f134266604e66eacb72e497721593be95365c Mon Sep 17 00:00:00 2001 From: ahmed ayoub Date: Wed, 7 Dec 2016 18:30:12 +0100 Subject: [PATCH 1/3] update req.query when rewrite without params #431 --- src/server/rewriter.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/server/rewriter.js b/src/server/rewriter.js index 718220f37..a48e91069 100644 --- a/src/server/rewriter.js +++ b/src/server/rewriter.js @@ -1,7 +1,9 @@ const express = require('express') const url = require('url') const _ = require('lodash') - +function updateQueryString(target,sourceUrl) { + return !!~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {}; +} module.exports = (routes) => { const router = express.Router() @@ -14,16 +16,14 @@ module.exports = (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) - } + req.query = updateQueryString(req.query,req.url) next() }) } else { router.all(route + '*', (req, res, next) => { // Rewrite url by replacing prefix req.url = req.url.replace(route, routes[route]) + req.query = updateQueryString(req.query,req.url) next() }) } From f44372643fe46fad98ef1a105a64bfaf68a396b4 Mon Sep 17 00:00:00 2001 From: ahmed ayoub Date: Thu, 8 Dec 2016 04:26:33 +0100 Subject: [PATCH 2/3] add tests for rewrite rules with query and without paramas for PR #432 --- test/server/plural.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/server/plural.js b/test/server/plural.js index 9a0918b04..690a8a268 100644 --- a/test/server/plural.js +++ b/test/server/plural.js @@ -78,7 +78,9 @@ describe('Server', () => { server.use(jsonServer.rewriter({ '/api/': '/', '/blog/posts/:id/show': '/posts/:id', - '/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body' + '/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body', + '/firstpostwithcomment': '/posts/1?_embed=comments' + })) server.use(router) }) @@ -686,6 +688,15 @@ describe('Server', () => { .end(done) }) + it('should rewrite using query without params', function (done) { + const expectedPost = _.cloneDeep(db.posts[0]) + expectedPost.comments = [ db.comments[0], db.comments[1] ] + request(server) + .get('/firstpostwithcomment') + .expect(expectedPost) + .end(done) + }) + it('should rewrite using params and query', function (done) { request(server) .get('/comments/special/1-quux') From 4bcb4d8a7b2534583f3c4ae03de426945ad0cd40 Mon Sep 17 00:00:00 2001 From: ahmed ayoub Date: Thu, 8 Dec 2016 04:31:53 +0100 Subject: [PATCH 3/3] [typo] add tests for rewrite rules with query and without paramas for PR #432 --- test/server/plural.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/server/plural.js b/test/server/plural.js index 690a8a268..25103b67d 100644 --- a/test/server/plural.js +++ b/test/server/plural.js @@ -79,7 +79,7 @@ describe('Server', () => { '/api/': '/', '/blog/posts/:id/show': '/posts/:id', '/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body', - '/firstpostwithcomment': '/posts/1?_embed=comments' + '/firstpostwithcomments': '/posts/1?_embed=comments' })) server.use(router) @@ -692,7 +692,7 @@ describe('Server', () => { const expectedPost = _.cloneDeep(db.posts[0]) expectedPost.comments = [ db.comments[0], db.comments[1] ] request(server) - .get('/firstpostwithcomment') + .get('/firstpostwithcomments') .expect(expectedPost) .end(done) })