diff --git a/packages/polka/index.js b/packages/polka/index.js index 19f23b0..43c3b45 100644 --- a/packages/polka/index.js +++ b/packages/polka/index.js @@ -61,10 +61,13 @@ class Polka extends Router { } handler(req, res, next) { - let info = this.parse(req, true); + let info = this.parse(req); let obj = this.find(req.method, req.path=info.pathname); - req.params = obj.params; + req.params = {}; + for (let key in obj.params) { + req.params[key] = decodeURIComponent(obj.params[key]); + } req.originalUrl = req.originalUrl || req.url; req.url = info.pathname + info.search; req.query = info.query || {}; diff --git a/packages/polka/test/index.js b/packages/polka/test/index.js index 006afd3..f985477 100644 --- a/packages/polka/test/index.js +++ b/packages/polka/test/index.js @@ -1425,16 +1425,16 @@ test('HEAD', async () => { }); -test('decode url', async () => { +test('encoded url', async () => { // t.plan(8); let sub = ( polka() .get('/:foo', (req, res) => { assert.ok('~> inside "GET /sub/:foo" handler') - assert.ok(req._decoded, '~> marked as decoded'); - assert.is(req.path, '/føøß∂r', '~> decoded "path" value'); - assert.is(req.url, '/føøß∂r?phone=%2b8675309', '~> decoded "url" value partially'); + assert.ok(!req._decoded, '~> not marked as decoded'); + assert.is(req.path, '/f%C3%B8%C3%B8%C3%9F%E2%88%82r', '~> "path" value'); + assert.is(req.url, '/f%C3%B8%C3%B8%C3%9F%E2%88%82r?phone=%2b8675309', '~> "url" value'); assert.is(req.params.foo, 'føøß∂r', '~> decoded "params.foo" segment'); assert.is(req.query.phone, '+8675309', '~~> does NOT decode "req.query" keys twice'); res.end('done'); diff --git a/readme.md b/readme.md index 62f2380..b93cf10 100644 --- a/readme.md +++ b/readme.md @@ -126,22 +126,12 @@ You may also pass a sub-application, which _must_ be accompanied by a `base` pat Please see [`Middleware`](#middleware) and [Express' middleware examples](http://expressjs.com/en/4x/api.html#middleware-callback-function-examples) for more info. -### parse(req, toDecode=true) +### parse(req, toDecode=false) Returns: `Object` or `undefined` As of `v0.5.0`, this is an alias of the [`@polka/url`](/packages/url) module. For nearly all cases, you'll notice no changes. -One important difference is that URL decoding is enabled by default in polka but it is disabled by default in the `@polka/url` module. You can disable the URL decoding if required, - -```js -const app = polka(); -const { parse } = require('@polka/url'); -app.parse = (req, toDecode) => { - return parse(req, false); // or simply, return parse(req); -} -``` - But, for whatever reason, you can quickly swap in [`parseurl`](https://github.com/pillarjs/parseurl) again: ```js