🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

add-query-params-to-url

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

add-query-params-to-url

lightweight utility for adding formatted query parameters to get request url.

1.1.0
latest
Source
npm
Version published
Weekly downloads
13
-58.06%
Maintainers
1
Weekly downloads
 
Created
Source

add-query-params-to-url

yarn add add-query-params-to-url

npm i add-query-params-to-url -S

Lightweight ( < 1kb) helper to add query parameters to a url GET request.

usage:

const addParams = require('add-query-params-to-url');
// or
import addParams from 'add-query-params-to-url'

const params = {
    auth: 'bearer',
    token: '12345',
    username: 'ynautwobbuffet',
    password: '42069'
};

const url = "https://nextstrain.org/ncov";

fetch(addParams(params, url), {
    method: 'GET'
})

// url argument is optional, it just prepends the formatted parameters to the end of the url.

fetch(`${url}${addParams({
    auth: 'bearer',
    token: '12345',
    username: 'ynautwobbuffet',
    password: '42069'
})}`, {
    method: 'GET'
})

If you'd prefer to just copy the code instead of installing this it's just this:

exports.addParams = function (params, url) {
    return url + "?" + Object.entries(params).map(function (x) { return (x[0] + "=" + encodeURIComponent(x[1])); }).join("&");
};

FAQs

Package last updated on 19 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts