Skip to content

Instantly share code, notes, and snippets.

@organom
Last active June 27, 2022 20:37
Show Gist options
  • Save organom/88ee2dbf9c7070cab732c35d6597c254 to your computer and use it in GitHub Desktop.
Save organom/88ee2dbf9c7070cab732c35d6597c254 to your computer and use it in GitHub Desktop.
util code for nodejs
"use strict";
/**
* adds an object as query to the given url
* @param url
* @param {object} query
* @returns {string} url with the extra query parameters
*/
function addQueryToUrl(url, query) {
if(!url) return '';
if(!query) return url;
const parsedUrl = require('url').parse(url, true);
Object.assign(parsedUrl.query, query);
// needed in order for the URL to parse the query field instead of the search field
// Documentation => on url.format: query (object; see querystring) will only be used if search is absent.
delete parsedUrl.search;
return parsedUrl.format();
}
module.exports = {
addQueryToUrl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment