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

Add simple query/search parameters option? #54

Closed
mrtnbroder opened this issue Oct 17, 2018 · 3 comments
Closed

Add simple query/search parameters option? #54

mrtnbroder opened this issue Oct 17, 2018 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@mrtnbroder
Copy link

mrtnbroder commented Oct 17, 2018

I've added a very simple wrapper around fetch myself and added the ability to passing in a qs object, which adds some parameters to the url:

const uri = new URL(url)
if (qs) {
	Object.keys(qs).forEach((key) => uri.searchParams.append(key, qs[key]))
}

it's a nicer api then just adding these to the url by hand.
I could make a PR if interested.

@sindresorhus
Copy link
Owner

Good idea! PR welcome. Should probably call the option searchParams to match the naming of the URL property.

I'm not sure whether it's a good idea to just append though. I would rather expect it to replace whatever's on the URL.

The searchParams option could be either an object, string, or URLSearchParams instance. Same as what new URLSearchParams() supports.

Thoughts?

@mrtnbroder
Copy link
Author

mrtnbroder commented Oct 18, 2018

I'm not sure whether it's a good idea to just append though. I would rather expect it to replace whatever's on the URL.

I guess it depends on the API. Some expect array values to be defined as a=foo&a=bar, where others expect a=foo,bar (or even more abstract). But I'm fine with using .set as the default and go for the a=foo,bar approach (shorter = nicer!).

The searchParams option could be either an object, string, or URLSearchParams instance. Same as what new URLSearchParams() supports.

Sounds good! maybe also add an alias for it like sp (searchParams) ? or qs? (which is more widely known I'd say). Opinions?

@sindresorhus
Copy link
Owner

Sounds good! maybe also add an alias for it like sp (searchParams) ? or qs? (which is more widely known I'd say). Opinions?

Nah, I don't like aliases. Better to make the code clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants