Skip to content

Instantly share code, notes, and snippets.

@qwtel
Created November 5, 2019 19:13
Show Gist options
  • Save qwtel/84be84255e335b7706841a2332af0a97 to your computer and use it in GitHub Desktop.
Save qwtel/84be84255e335b7706841a2332af0a97 to your computer and use it in GitHub Desktop.
export const urlWithParams = (url, params) => {
const u = new URL(url, global.location.origin)
if (params) {
u.search = new URLSearchParams([...u.searchParams, ...Object.entries(params)])
}
return u.href
}
@qwtel
Copy link
Author

qwtel commented Nov 5, 2019

Example: urlWithParams('/api/post', { foo: 3, bar: 4 }) gives http://thisdomain.tld/api/post?foo=3&bar=4

@qwtel
Copy link
Author

qwtel commented Nov 5, 2019

Note: You might have to replace global with window, self, or globalThis depending on use context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment