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

Encode boolean query parameters as integers #345

Merged
merged 6 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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