add-query-params-to-url
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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("&");
};

Readme

Keywords

none

Package Sidebar

Install

npm i add-query-params-to-url

Weekly Downloads

3

Version

1.1.0

License

MIT

Unpacked Size

3.32 kB

Total Files

4

Last publish

Collaborators

  • bigbizze