Skip to content

Commit

Permalink
Merge pull request #345 from tighten/jbk/cast-query-bools
Browse files Browse the repository at this point in the history
Encode boolean query parameters as integers
  • Loading branch information
bakerkretzmar committed Nov 6, 2020
2 parents 96ecb84 + 1dd348f commit ae2fd97
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/js/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
}

Expand Down
10 changes: 7 additions & 3 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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' });
});
});

Expand Down

0 comments on commit ae2fd97

Please sign in to comment.