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

Return a literal string from route() when generating a URL #336

Merged
merged 4 commits into from
Oct 23, 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Breaking changes are marked with ⚠️.
- Use [microbundle](https://github.com/developit/microbundle) instead of Webpack to build and distribute Ziggy ([#312](https://github.com/tighten/ziggy/pull/312))
- ⚠️ Default Ziggy's `baseUrl` to the value of the `APP_URL` environment variable instead of `url('/')` ([#334](https://github.com/tighten/ziggy/pull/334))
- ⚠️ Allow getting the route name with `current()` when the current URL has a query string ([#330](https://github.com/tighten/ziggy/pull/330))
- ⚠️ Return a literal string from the `route()` function when any arguments are passed to it ([#336](https://github.com/tighten/ziggy/pull/336))

**Deprecated**

Expand All @@ -39,7 +40,8 @@ Breaking changes are marked with ⚠️.
- ⚠️ Remove the following undocumented public properties and methods from the `Router` class returned by the `route()` function ([#330](https://github.com/tighten/ziggy/pull/330)):
- `name`, `absolute`, `ziggy`, `urlBuilder`, `template`, `urlParams`, `queryParams`, and `hydrated`
- `normalizeParams()`, `hydrateUrl()`, `matchUrl()`, `constructQuery()`, `extractParams()`, `parse()`, and `trimParam()`
- ⚠️ Remove the `UrlBuilder` class ([#330](https://github.com/tighten/ziggy/pull/330)):
- ⚠️ Remove the `UrlBuilder` class ([#330](https://github.com/tighten/ziggy/pull/330))
- ⚠️ Remove the `url()` method now that `route(...)` returns a string ([#336](https://github.com/tighten/ziggy/pull/336)):

**Fixed**

Expand Down
41 changes: 11 additions & 30 deletions src/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,18 @@ class Router extends String {
*
* @example
* // with 'posts.show' route 'posts/{post}'
* route('posts.show', 1).url(); // 'https://ziggy.dev/posts/1'
* (new Router('posts.show', 1)).toString(); // 'https://ziggy.dev/posts/1'
*
* @return {String}
*/
url() {
toString() {
// Get parameters that don't correspond to any route segments to append them to the query
const unhandled = Object.keys(this._params)
.filter((key) => !this._route.parameterSegments.some(({ name }) => name === key))
.filter((key) => key !== '_query')
.reduce((result, current) => ({ ...result, [current]: this._params[current] }), {});

return this._route.compile(this._params) + stringify({ ...unhandled, ...this._queryParams }, {
return this._route.compile(this._params) + stringify({ ...unhandled, ...this._params['_query'] }, {
addQueryPrefix: true,
arrayFormat: 'indices',
encodeValuesOnly: true,
Expand Down Expand Up @@ -201,17 +202,6 @@ class Router extends String {
return Object.keys(this._config.namedRoutes).includes(name);
}

/**
* Add query parameters to be appended to the compiled URL.
*
* @param {Object} params
* @return {this}
*/
withQuery(params) {
this._queryParams = params;
return this;
}

/**
* Parse Laravel-style route parameters of any type into a normalized object.
*
Expand Down Expand Up @@ -282,8 +272,9 @@ class Router extends String {
*/
_substituteBindings(params, bindings = {}) {
return Object.entries(params).reduce((result, [key, value]) => {
// If the value isn't an object there's nothing to substitute, so we return it as-is
if (!value || typeof value !== 'object' || Array.isArray(value)) {
// If the value isn't an object, or if it's an object of explicity query
// parameters, there's nothing to substitute so we return it as-is
if (!value || typeof value !== 'object' || Array.isArray(value) || key === '_query') {
return { ...result, [key]: value };
}

Expand Down Expand Up @@ -338,20 +329,8 @@ class Router extends String {
};
}

toString() {
return this.url();
}

valueOf() {
return this.url();
}

/**
* @deprecated since v1.0, pass parameters as the second argument to `route()` instead.
*/
with(params) {
this._params = this._parse(params);
return this;
return this.toString();
}

/**
Expand All @@ -363,5 +342,7 @@ class Router extends String {
}

export default function route(name, params, absolute, config) {
return new Router(name, params, absolute, config);
const router = new Router(name, params, absolute, config);

return name ? router.toString() : router;
}
Loading