-
Notifications
You must be signed in to change notification settings - Fork 30
Append query params to url if provided #28
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
Conversation
This is neat! |
Not sure if this is would be considered an edge case or a simple matter of "just don't do it this way", but what of the scenario where you have a URL that already has query params and you want to add on to it? Something like this wouldn't work as expected... const preset_url_from_some_other_process = "/addresses/states?enabled_only=true"
const response = await get(preset_url_from_some_other_process, {
query: { country: event.target.selectedOptions[0].value },
responseKind: "turbo-stream"
}) I suppose the "solution" would be "Parse out and remove existing query params from the URL and merge them with the object passed into the |
We can use URLSearchParams to parse an existing query string and merge them easily. Probably makes sense to add that. It can parse a string:
Then we could just merge in the query options. I'll add that later today. |
And added! |
} | ||
|
||
get url () { | ||
return this.originalUrl.split('?')[0] + this.query |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably wouldn't matter for ajax calls, but a side effect of this is if the url has an anchor in it (for whatever reason), it would be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I didn't see any reason to keep the anchor for AJAX requests. I don't know of any use case for it server-side actually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
If a user wants to request a url with query params, they have to build the URL with query params on their own with
URLSearchParams
or something similar.For example, a dynamic country / state select you might do the following to update the state select field after choosing a country:
This PR adds a
query
option that will automatically be appended to the URL if provided. The above example can then become the following:Both examples will make requests to
/addresses/states?country=us
.I also updated the README to document all the request options a bit clearer (and documented
query
). Originally, I named the optionparams
but thoughtquery
was clearer.