-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[resolves #20] Add support for parsing and constructing urls with query params #31
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
}; | ||
|
@@ -17,13 +18,17 @@ var cachedCompilers = {}; | |
* @param {String} name The name of the route | ||
* @param {Object} config The configuration for this route. | ||
* @param {String} config.path The path of the route. | ||
* @param {Object} [options] Options for parsing and generating the urls | ||
* @param {String} [options.queryLib] Library to use for `parse` and `stringify` methods | ||
* @constructor | ||
*/ | ||
function Route(name, config) { | ||
function Route(name, config, options) { | ||
options = options || {}; | ||
this.name = name; | ||
this.config = config || {}; | ||
this.keys = []; | ||
this.regexp = pathToRegexp(this.config.path, this.keys); | ||
this._queryLib = options.queryLib || queryString; | ||
} | ||
|
||
/** | ||
|
@@ -101,20 +106,31 @@ Route.prototype.match = function (url, options) { | |
} | ||
} | ||
|
||
return routeParams; | ||
// 4. query params | ||
var queryParams = {}; | ||
if (-1 !== pos) { | ||
queryParams = self._queryLib.parse(url.substring(pos+1)); | ||
} | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update docblock |
||
var routePath = this.config.path; | ||
var compiler; | ||
var err; | ||
var url; | ||
|
||
if (Array.isArray(routePath)) { | ||
routePath = routePath[0]; | ||
|
@@ -125,7 +141,11 @@ Route.prototype.makePath = function (params) { | |
cachedCompilers[routePath] = compiler; | ||
|
||
try { | ||
return compiler(params); | ||
url = compiler(params); | ||
if (query) { | ||
url += '?' + this._queryLib.stringify(query); | ||
} | ||
return url; | ||
} catch (e) { | ||
err = e; | ||
} | ||
|
@@ -141,6 +161,8 @@ Route.prototype.makePath = function (params) { | |
* A Router class that provides route matching and route generation functionalities. | ||
* @class Router | ||
* @param {Object} routes Route table, which is a name to router config map. | ||
* @param {Object} [options] Options for parsing and generating the urls | ||
* @param {String} [options.queryLib] Library to use for `parse` and `stringify` methods | ||
* @constructor | ||
* @example | ||
var Router = require('routr'), | ||
|
@@ -167,17 +189,18 @@ Route.prototype.makePath = function (params) { | |
console.log('[Route found]: name=', route.name, 'url=', route.url, 'params=', route.params, 'config=', route.config); | ||
} | ||
*/ | ||
function Router(routes) { | ||
function Router(routes, options) { | ||
var self = this; | ||
self._routes = {}; | ||
self._routeOrder = []; | ||
self._options = options || {}; | ||
debug('new Router, routes = ', routes); | ||
|
||
if (!Array.isArray(routes)) { | ||
// Support handling route config object as an ordered map (legacy) | ||
self._routeOrder = Object.keys(routes); | ||
self._routeOrder.forEach(function createRoute(name) { | ||
self._routes[name] = new Route(name, routes[name]); | ||
self._routes[name] = new Route(name, routes[name], self._options); | ||
}); | ||
} else if (routes) { | ||
routes.forEach(function createRouteFromArrayValue(route) { | ||
|
@@ -189,7 +212,7 @@ function Router(routes) { | |
throw new Error('Duplicate route with name ' + route.name); | ||
} | ||
self._routeOrder.push(route.name); | ||
self._routes[route.name] = new Route(route.name, route); | ||
self._routes[route.name] = new Route(route.name, route, self._options); | ||
}); | ||
} | ||
|
||
|
@@ -227,8 +250,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 +264,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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
query-string
orqs
? These 2 modules serialize query params differently. Maybe check what express uses, I think they useqs
: https://github.com/expressjs/express/blob/master/package.json#L48Maybe it is better to use one of them as default, but allow user to customize with their own
parse
andstringify
methods, so they can switch to anything they want.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose query-string since it is what fluxible-router is using and we can replace that functionality in fluxible-router with this since it will now be returned. If they are drastically different, then it could be a BC break to fluxible-router if we update fluxible-router to use the output of routr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the ability to pass
options.queryLib
to the constructor to change out the query parsing/stringifying implementation.