Created
November 5, 2019 19:13
-
-
Save qwtel/84be84255e335b7706841a2332af0a97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
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
Example:
urlWithParams('/api/post', { foo: 3, bar: 4 })
giveshttp://thisdomain.tld/api/post?foo=3&bar=4