Skip to content
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

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

Merged
merged 1 commit into from
Jul 19, 2022

Conversation

ide
Copy link
Contributor

@ide ide commented Mar 4, 2021

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.

Copy link
Member

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

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
Copy link
Contributor Author

ide commented Jul 18, 2022

@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
Copy link
Contributor

titanism commented Jul 19, 2022

@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