Skip to content

[RFC] URL-encode path parameters for Router.url - #116

Merged
titanism merged 1 commit into
koajs:masterfrom
ide:@ide/encode-component-default
Jul 19, 2022
Merged

[RFC] URL-encode path parameters for Router.url#116
titanism merged 1 commit into
koajs:masterfrom
ide:@ide/encode-component-default

Conversation

@ide

@ide ide commented Mar 4, 2021

Copy link
Copy Markdown
Contributor

This is a proposal for a public API change.

The main breaking change from v8.x to v9.x was an upgrade to path-to-regex. That PR 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.

This addition to the changelog shows the change by example:

  • [Breaking] URL-encode parameters passed to Router.url with encodeURIComponent.
    • Previously, the parameters to insert in a URL were not encoded:
      • Router.url('/:x', { x: 'hello world' }) produced "/hello world"
      • Router.url('/:x', { x: 'a/test?param#' }) would throw an error
    • Now, all parameters are safely URL-encoded by default:
      • Router.url('/:x', { x: 'hello world' }) produces "/hello%20world"
      • Router.url('/:x', { x: 'a/test?param#' }) produces "/a%2Ftest%3Fparam%23"

Updated tests, docs, and the changelog.

@3imed-jaberi 3imed-jaberi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

The main breaking change from v8.x to v9.x was an upgrade to `path-to-regex`. [That PR](koajs#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.
@ide
ide force-pushed the @ide/encode-component-default branch from ca7bee0 to f754976 Compare July 18, 2022 22:49
@ide

ide commented Jul 18, 2022

Copy link
Copy Markdown
Contributor Author

@titanism would you be interested in taking a look at this change? In summary, it fixes query parameter escaping for Router.url.

@titanism
titanism merged commit 94039ef into koajs:master Jul 19, 2022
@titanism

titanism commented Jul 19, 2022

Copy link
Copy Markdown
Contributor

@koa/router v12.0.0 released to npm, thanks @ide

https://github.com/koajs/router/releases/tag/v12.0.0

mirrored to koa-router

@ide
ide deleted the @ide/encode-component-default branch July 19, 2022 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants