diff --git a/History.md b/History.md index 44611e0..8e048cb 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +3.0.2 /2018-11-14 +================= + +* map query string parameters from original url to rewrite url 3.0.1 / 2018-05-08 ================== diff --git a/Readme.md b/Readme.md index a6312e0..32883b6 100644 --- a/Readme.md +++ b/Readme.md @@ -35,6 +35,10 @@ app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst')); app.use(rewrite('/js/(.*)', '/public/assets/js/$1')); ``` +```js +app.use(rewrite('/home?foo=bar', '/new-home?foo=$1')); +``` + ## Debugging Use the __DEBUG__ environment variable with "koa-rewrite": diff --git a/example.js b/example.js index 6adcf2a..c69bfdb 100644 --- a/example.js +++ b/example.js @@ -15,6 +15,9 @@ app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst')); // GET /js/jquery.js app.use(rewrite('/js/(.*)', '/public/assets/js/$1')); +//Get /home?foo=bar +app.use(rewrite('/home/:foo', '/new-home?foo=$1')); + app.use(function(ctx) { ctx.body = ctx.url + '\n'; }); diff --git a/index.js b/index.js index a732924..f85f801 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ const debug = require('debug')('koa-rewrite'); const toRegexp = require('path-to-regexp'); + const parse = require('url-parse'); + const queryString = require('query-string'); /** * Expose `expose`. @@ -30,7 +32,8 @@ function rewrite(src, dst) { return function(ctx, next) { const orig = ctx.url; - const m = re.exec(orig); + const pathUrl = handleQueryString(orig); + const m = re.exec(pathUrl); if (m) { ctx.url = dst.replace(/\$(\d+)|(?::(\w+))/g, (_, n, name) => { @@ -67,3 +70,21 @@ function toMap(params) { return map; } + +/** + * convert query string url to path string. + * + * @param {String} src + * @return {String} + * @api private + */ + +function handleQueryString(src) { + let url = parse(src, false); + if(url.query){ + let queryparams = queryString.parse(url.query); + return `${url.pathname}/${Object.values(queryparams).join('/')}`; + }else{ + return src; + } +} diff --git a/package.json b/package.json index 7d6750f..3937d97 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "koa-rewrite", "description": "URL rewrite middleware for koa", "repository": "koajs/rewrite", - "version": "3.0.1", + "version": "3.0.2", "scripts": { "test": "mocha test/test" }, @@ -24,7 +24,9 @@ "license": "MIT", "dependencies": { "debug": "^3.1.0", - "path-to-regexp": "^2.1.0" + "path-to-regexp": "^2.1.0", + "query-string": "^6.2.0", + "url-parse": "1.4.4" }, "engines": { "node": ">= 4" diff --git a/test/test.js b/test/test.js index 10a9168..8cd7f40 100644 --- a/test/test.js +++ b/test/test.js @@ -92,7 +92,7 @@ describe('new Koa-rewrite', () => { it('rewrite /?foo=bar -> /home?foo=bar', done => { const app = new Koa(); app.use(differentPathHelper); - app.use(rewrite(/^\/((\?)(.*?))?$/, '/home$2$3')); + app.use(rewrite('/(.*)/:foo', '/home?foo=$2')); app.use((ctx) => { ctx.body = ctx.url; }); @@ -115,4 +115,30 @@ describe('new Koa-rewrite', () => { .expect('/home', done); }); + it('rewrite /home?foo=bar -> /new-home?foo=:foo', done => { + const app = new Koa(); + app.use(differentPathHelper); + app.use(rewrite('/home/:foo', '/new-home?foo=$1')); + app.use((ctx) => { + ctx.body = ctx.url; + }); + + request(app.callback()) + .get('/home?foo=bar') + .expect('/new-home?foo=bar', done); + }); + + it('rewrite /home?foo=bar&test=bar2 -> /new-home?foo=:foo&test=:test', done => { + const app = new Koa(); + app.use(differentPathHelper); + app.use(rewrite('/home/:foo/:test', '/new-home?foo=$1&test=$2')); + app.use((ctx) => { + ctx.body = ctx.url; + }); + + request(app.callback()) + .get('/home?foo=bar&test=bar2') + .expect('/new-home?foo=bar&test=bar2', done); + }); + });