Skip to content

Commit 94039ef

Browse files
authored
URL-encode path parameters for Router.url (#116)
The main breaking change from v8.x to v9.x was an upgrade to `path-to-regex`. [That PR](#71) alluded to a breaking change in encoding. Namely, parameters were not URL-encoded: parameters with safe special characters (like spaces) were not percent-encoded, and parameters with special characters that mean something in a URL, such as slashes (path separators) and question marks (query string delimiter). The motivation for this PR is to make URL-encoding be the default since typically the parameters provided to `Router.url` are plain, unencoded values. Should someone need an escape hatch, they could pass in `{ encode: null }` (I think) to disable the automatic encoding. Updated tests and docs.
1 parent 8fe1d54 commit 94039ef

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ router
424424

425425
### Router.url(path, params) ⇒ <code>String</code>
426426

427-
Generate URL from url pattern and given `params`.
427+
Generate URL from url pattern and given `params`. This method URL-encodes the parameters before including them in the URL.
428428

429429
**Kind**: static method of <code>[Router](#exp_module_koa-router--Router)</code>
430430

lib/layer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Layer.prototype.url = function (params, options) {
119119
}
120120
}
121121

122-
const toPath = compile(url, options);
122+
const toPath = compile(url, Object.assign({ encode: encodeURIComponent }, options));
123123
let replaced;
124124

125125
const tokens = parse(url);

test/lib/layer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ describe('Layer', function () {
303303
url.should.equal('/programming/how-to-node');
304304
});
305305

306+
it('escapes using encodeURIComponent()', function() {
307+
const route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'});
308+
const url = route.url({ category: 'programming', title: 'how to node & js/ts' });
309+
url.should.equal('/programming/how%20to%20node%20%26%20js%2Fts');
310+
});
311+
306312
it('setPrefix method checks Layer for path', function () {
307313
const route = new Layer('/category', ['get'], [function () {}], {
308314
name: 'books'

test/lib/router.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,14 @@ describe('Router', function () {
16741674
router.url('Picard').should.be.Error();
16751675
router.url(Symbol('books')).should.be.Error();
16761676
});
1677+
1678+
it('escapes using encodeURIComponent()', function() {
1679+
const url = Router.url(
1680+
'/:category/:title',
1681+
{ category: 'programming', title: 'how to node & js/ts' }
1682+
);
1683+
url.should.equal('/programming/how%20to%20node%20%26%20js%2Fts');
1684+
});
16771685
});
16781686

16791687
describe('Router#param()', function () {

0 commit comments

Comments
 (0)