Skip to content

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

Merged
merged 6 commits into from
Aug 1, 2021
Merged

Conversation

excid3
Copy link
Contributor

@excid3 excid3 commented Jul 31, 2021

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:

async updateStateSelect(event) {
  const value = event.target.selectedOptions[0].value
  const query = new URLSearchParams({ country: value })
  const response = await  get(`/addresses/states?${query}`, {
    responseKind: "turbo-stream"
  })
}

This PR adds a query option that will automatically be appended to the URL if provided. The above example can then become the following:

async updateStateSelect(event) {
  const response = await  get("/addresses/states", {
    query: { country: event.target.selectedOptions[0].value },
    responseKind: "turbo-stream"
  })
}

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 option params but thought query was clearer.

@tabishiqbal
Copy link

This is neat!

@t27duck
Copy link
Contributor

t27duck commented Jul 31, 2021

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 query option", but I at least wanted to bring this use case up.

@excid3
Copy link
Contributor Author

excid3 commented Jul 31, 2021

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:

let params = new URLSearchParams('abc=foo&def=%5Basf%5D&xyz=5');
params.get("abc"); // "foo"

Then we could just merge in the query options.

I'll add that later today.

@excid3
Copy link
Contributor Author

excid3 commented Jul 31, 2021

And added!

}

get url () {
return this.originalUrl.split('?')[0] + this.query
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

@marcelolx marcelolx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@marcelolx marcelolx merged commit 928a5fd into rails:main Aug 1, 2021
@excid3 excid3 deleted the query-params branch August 2, 2021 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants