From c1c852edd91d6b5e2d909c90e564cee6feefab14 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Tue, 27 Oct 2020 21:09:32 -0400 Subject: [PATCH 1/5] Basic implementation and test --- src/js/route.js | 5 +++++ tests/js/route.test.js | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/js/route.js b/src/js/route.js index 4f889334..4442f6cf 100644 --- a/src/js/route.js +++ b/src/js/route.js @@ -134,6 +134,11 @@ class Router extends String { arrayFormat: 'indices', encodeValuesOnly: true, skipNulls: true, + encoder: (value, encoder) => { + return typeof value === 'boolean' + ? String(Number(value)) + : encoder(value); + }, }); } diff --git a/tests/js/route.test.js b/tests/js/route.test.js index 9aa12523..5e1c9ce3 100644 --- a/tests/js/route.test.js +++ b/tests/js/route.test.js @@ -316,6 +316,10 @@ describe('route()', () => { same(route('posts.index', { filled: 'filling', empty: null }), 'https://ziggy.dev/posts?filled=filling'); }); + test('can cast boolean query parameters to integers', () => { + same(route('posts.show', { post: 1, preview: true }), 'https://ziggy.dev/posts/1?preview=1'); + }); + test('can explicitly append query parameters using _query parameter', () => { same( route('events.venues.show', { From f1dbe91c3bb7f3d222d596b8a284266121b32aab Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 30 Oct 2020 10:14:30 -0400 Subject: [PATCH 2/5] Formatting --- src/js/route.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/js/route.js b/src/js/route.js index 4442f6cf..b7f6a5b5 100644 --- a/src/js/route.js +++ b/src/js/route.js @@ -134,11 +134,7 @@ class Router extends String { arrayFormat: 'indices', encodeValuesOnly: true, skipNulls: true, - encoder: (value, encoder) => { - return typeof value === 'boolean' - ? String(Number(value)) - : encoder(value); - }, + encoder: (value, encoder) => typeof value === 'boolean' ? Number(value) : encoder(value), }); } From deba3b99c88949b6edc7ca8341805818f588d845 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 30 Oct 2020 10:17:04 -0400 Subject: [PATCH 3/5] Update Readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0db27793..ec4caafc 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,8 @@ With query parameters: route('events.venues.show', { event: 1, venue: 2, page: 5, count: 10 }); // Returns '/events/1/venues/2?page=5&count=10' ``` +> Note: like Laravel's `route()` helper, Ziggy will encode boolean query parameters as integers. + If whole objects are passed, Ziggy will automatically look for an `id` primary key: ```js From 57c707397c15d9eff56afb52e563c9a335a1dfdc Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 30 Oct 2020 10:20:32 -0400 Subject: [PATCH 4/5] Update test and Readme to cover (not) decoding integers back into booleans --- README.md | 2 +- tests/js/route.test.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ec4caafc..6af9abbf 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ With query parameters: route('events.venues.show', { event: 1, venue: 2, page: 5, count: 10 }); // Returns '/events/1/venues/2?page=5&count=10' ``` -> Note: like Laravel's `route()` helper, Ziggy will encode boolean query parameters as integers. +> Note: like Laravel's `route()` helper, Ziggy will encode boolean query parameters as integers. Due to their ambiguity, Ziggy **cannot** decode these query parameters back into booleans, so if you're parsing them out with `route().params` you will have to decode them yourself. If whole objects are passed, Ziggy will automatically look for an `id` primary key: diff --git a/tests/js/route.test.js b/tests/js/route.test.js index 5e1c9ce3..0c75d4fa 100644 --- a/tests/js/route.test.js +++ b/tests/js/route.test.js @@ -459,11 +459,11 @@ describe('route()', () => { deepEqual(route().params, { post: '1', guest: { name: 'Taylor' } }); - global.window.location.href = 'https://ziggy.dev/events/1/venues/2?id=5&vip=true'; + global.window.location.href = 'https://ziggy.dev/events/1/venues/2?id=5&vip=0'; global.window.location.pathname = '/events/1/venues/2'; - global.window.location.search = '?id=5&vip=true'; + global.window.location.search = '?id=5&vip=0'; - deepEqual(route().params, { event: '1', venue: '2', id: '5', vip: 'true' }); + deepEqual(route().params, { event: '1', venue: '2', id: '5', vip: '0' }); }); }); From 37f0dd9a49cdc9a2cddab5592933a9e56b125368 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Fri, 30 Oct 2020 10:44:07 -0400 Subject: [PATCH 5/5] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b42346fe..00548b2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Breaking changes are marked with ⚠️. - ⚠️ Rename `namedRoutes` → `routes`, `defaultParameters` → `defaults`, `baseUrl` → `url`, and `basePort` → `port` ([#338](https://github.com/tighten/ziggy/pull/338)) - ⚠️ Make the `filter()` method on the `Ziggy` class return an instance of that class instead of a collection of routes ([#341](https://github.com/tighten/ziggy/pull/341)) - ⚠️ Make the `nameKeyedRoutes()`, `resolveBindings()`, `applyFilters()`, and `group()` methods on the `Ziggy` class, and the `generate()` method on the `CommandRouteGenerator` class, private ([#341](https://github.com/tighten/ziggy/pull/341)) +- ⚠️ Encode boolean query parameters as integers ([#345](https://github.com/tighten/ziggy/pull/345)) **Deprecated**