Skip to content

Commit

Permalink
URL-encode path parameters for Router.url
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ide committed Jul 18, 2022
1 parent 8fe1d54 commit f754976
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ router

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Layer.prototype.url = function (params, options) {
}
}

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

const tokens = parse(url);
Expand Down
6 changes: 6 additions & 0 deletions test/lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ describe('Layer', function () {
url.should.equal('/programming/how-to-node');
});

it('escapes using encodeURIComponent()', function() {
const route = new Layer('/:category/:title', ['get'], [function () {}], {name: 'books'});
const url = route.url({ category: 'programming', title: 'how to node & js/ts' });
url.should.equal('/programming/how%20to%20node%20%26%20js%2Fts');
});

it('setPrefix method checks Layer for path', function () {
const route = new Layer('/category', ['get'], [function () {}], {
name: 'books'
Expand Down
8 changes: 8 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,14 @@ describe('Router', function () {
router.url('Picard').should.be.Error();
router.url(Symbol('books')).should.be.Error();
});

it('escapes using encodeURIComponent()', function() {
const url = Router.url(
'/:category/:title',
{ category: 'programming', title: 'how to node & js/ts' }
);
url.should.equal('/programming/how%20to%20node%20%26%20js%2Fts');
});
});

describe('Router#param()', function () {
Expand Down

0 comments on commit f754976

Please sign in to comment.