diff --git a/lib/router.js b/lib/router.js index 146e765..f849f9a 100644 --- a/lib/router.js +++ b/lib/router.js @@ -7,6 +7,7 @@ var debug = require('debug')('Routr:router'); var pathToRegexp = require('path-to-regexp'); +var queryString = require('query-string'); var METHODS = { GET: 'get' }; @@ -101,20 +102,31 @@ Route.prototype.match = function (url, options) { } } - return routeParams; + // 4. query params + var queryParams = {}; + if (-1 !== pos) { + queryParams = queryString.parse(url.substring(pos)); + } + + return { + route: routeParams, + query: queryParams + }; }; /** * Generates a path string with this route, using the specified params. * @method makePath * @param {Object} params The route parameters to be used to create the path string + * @param {Object} query The query parameters to be used to create the path string * @return {String} The generated path string. * @for Route */ -Route.prototype.makePath = function (params) { +Route.prototype.makePath = function (params, query) { var routePath = this.config.path; var compiler; var err; + var url; if (Array.isArray(routePath)) { routePath = routePath[0]; @@ -125,7 +137,11 @@ Route.prototype.makePath = function (params) { cachedCompilers[routePath] = compiler; try { - return compiler(params); + url = compiler(params); + if (query) { + url += '?' + queryString.stringify(query); + } + return url; } catch (e) { err = e; } @@ -227,8 +243,9 @@ Router.prototype.getRoute = function (url, options) { return { name: keys[i], url: url, - params: match, - config: route.config + params: match.route, + config: route.config, + query: match.query }; } } @@ -240,10 +257,11 @@ Router.prototype.getRoute = function (url, options) { * @method makePath * @param {String} name The route name * @param {Object} params The route parameters to be used to create the path string + * @param {Object} query The query parameters to be used to create the path string * @return {String} The generated path string, null if there is no route with the given name. */ -Router.prototype.makePath = function (name, params) { - return (name && this._routes[name] && this._routes[name].makePath(params)) || null; +Router.prototype.makePath = function (name, params, query) { + return (name && this._routes[name] && this._routes[name].makePath(params, query)) || null; }; module.exports = Router; diff --git a/package.json b/package.json index 49578b7..14dac68 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ ], "dependencies": { "debug": "^2.0.0", - "path-to-regexp": "^1.1.1" + "path-to-regexp": "^1.1.1", + "query-string": "^3.0.1" }, "devDependencies": { "babel-polyfill": "^6.7.2", diff --git a/tests/unit/lib/router.js b/tests/unit/lib/router.js index 0ff9668..e149403 100644 --- a/tests/unit/lib/router.js +++ b/tests/unit/lib/router.js @@ -228,6 +228,8 @@ describe('Router', function () { route = router.getRoute('/new_article?foo=bar', {method: 'post'}); expect(route.name).to.equal('new_article'); + expect(route.params).to.deep.equal({}); + expect(route.query).to.deep.equal({foo: 'bar'}); }); it('method should be case-insensitive and defaults to get', function () { @@ -302,6 +304,16 @@ describe('Router', function () { }); expect(path).to.equal('/foo/bar'); }); + it('handle query params', function () { + var path = router.makePath('unamed_params', { + foo: 'foo', + 0: 'bar' + }, { + foo: 'bar', + baz: 'foo' + }); + expect(path).to.equal('/foo/bar?baz=foo&foo=bar'); + }); it('non-existing route', function () { var path = router.makePath('article_does_not_exist', { site: 'SITE',