Skip to content

Commit

Permalink
Treat all parameters not matching a named route segment as query para…
Browse files Browse the repository at this point in the history
…ms (#451)

* Add failing tests

* Always treat parameters that don't match any route segments as query params

* Wip
  • Loading branch information
bakerkretzmar authored Aug 25, 2021
1 parent 05411b5 commit 25712ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/js/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default class Router extends String {

return {
...this._defaults(route),
...this._substituteBindings(params, route.bindings),
...this._substituteBindings(params, route),
};
}

Expand All @@ -201,17 +201,17 @@ export default class Router extends String {
* Substitute Laravel route model bindings in the given parameters.
*
* @example
* _substituteBindings({ post: { id: 4, slug: 'hello-world', title: 'Hello, world!' } }, { post: 'slug' }); // { post: 'hello-world' }
* _substituteBindings({ post: { id: 4, slug: 'hello-world', title: 'Hello, world!' } }, { bindings: { post: 'slug' } }); // { post: 'hello-world' }
*
* @param {Object} params - Route parameters.
* @param {Object} bindings - Route model bindings.
* @param {Object} route - Route definition.
* @return {Object} Normalized route parameters.
*/
_substituteBindings(params, bindings = {}) {
_substituteBindings(params, { bindings, parameterSegments }) {
return Object.entries(params).reduce((result, [key, value]) => {
// If the value isn't an object, or if it's an object of explicit query
// parameters, there's nothing to substitute so we return it as-is
if (!value || typeof value !== 'object' || Array.isArray(value) || key === '_query') {
// If the value isn't an object, or if the key isn't a named route parameter,
// there's nothing to substitute so we return it as-is
if (!value || typeof value !== 'object' || Array.isArray(value) || !parameterSegments.some(({ name }) => name === key)) {
return { ...result, [key]: value };
}

Expand Down
9 changes: 9 additions & 0 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,15 @@ describe('route()', () => {
same(route('posts.show', [1, 2]), 'https://ziggy.dev/posts/1?2=');
same(route('posts.show', ['my-first-post', 'foo', 'bar']), 'https://ziggy.dev/posts/my-first-post?foo=&bar=');
});

test("can automatically append object with only 'extra' parameters to query", () => {
// Route has no parameters, the entire parameters object is 'extra' and should be used as the query string
same(route('hosting-contacts.index', { filter: { name: 'Dwyer' } }), 'https://ziggy.dev/hosting-contacts?filter[name]=Dwyer');
});

test("can append 'extra' object parameter to query", () => {
same(route('posts.show', { post: 2, filter: { name: 'Dwyer' } }), 'https://ziggy.dev/posts/2?filter[name]=Dwyer');
});
});

describe('has()', () => {
Expand Down

0 comments on commit 25712ad

Please sign in to comment.