diff --git a/CHANGELOG.md b/CHANGELOG.md index 81fba40a..0e48cd98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ Breaking changes are marked with ⚠️. - ⚠️ 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)) - ⚠️ Export from `index.js` instead of `route.js` ([#344](https://github.com/tighten/ziggy/pull/344)) +- ⚠️ Encode boolean query parameters as integers ([#345](https://github.com/tighten/ziggy/pull/345)) **Deprecated** diff --git a/README.md b/README.md index 5f1d5bc7..b3008172 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. 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: ```js diff --git a/src/js/Router.js b/src/js/Router.js index b5a3cbdb..521baebc 100644 --- a/src/js/Router.js +++ b/src/js/Router.js @@ -47,6 +47,7 @@ export default class Router extends String { arrayFormat: 'indices', encodeValuesOnly: true, skipNulls: true, + encoder: (value, encoder) => typeof value === 'boolean' ? Number(value) : encoder(value), }); } diff --git a/tests/js/route.test.js b/tests/js/route.test.js index bf58ee19..c5aad933 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', { @@ -455,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' }); }); });