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

Make combining a URL (string) with a query easier #413

Closed
annevk opened this issue Aug 23, 2018 · 2 comments
Closed

Make combining a URL (string) with a query easier #413

annevk opened this issue Aug 23, 2018 · 2 comments

Comments

@annevk
Copy link
Member

annevk commented Aug 23, 2018

In particular if you have a URL, either as a string (inc. a relative URL) or an object, and some kind of structure that could be passed to URLSearchParams's constructor, it should be easier to get a new URL out of that (either a string or object).

What you can currently do is something like (substitution):

const url = new URL(input, location);
url.query = new URLSearchParams(params);

or (additive):

const url = new URL(input, location);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))

Both are quite verbose. Perhaps something like

URL.withQuery(input, params)

though note you'd lose the base argument (we'd grab that from the global) and flexibility on substitution or additive (gotta pick).

@phistuck
Copy link

What about making URLSearchParams.prototype.append take an object? Or maybe add URLSearchParams.prototype.appendAll(objectOrArrayOfPairs)?

@annevk
Copy link
Member Author

annevk commented Apr 26, 2020

If libraries identify a popular pattern here I'm sure it'll be suggested here long term (or using libraries is deemed adequate, which is also acceptable).

@annevk annevk closed this as completed Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants