GithubHelp home page GithubHelp logo

node-append-query's Introduction

Build Status

append-query

Append querystring params to a URL.

Installation

npm install append-query

Usage

appendQuery(url, query[, options])

  • url - a string URL to append to.
  • query - a string or object containing query params to append.
  • options (optional)
    • encodeComponents - whether or not to encode appended passed params using encodeURIComponent. Default: true.
    • removeNull - whether or not to remove params for null properties in the query object. Default: false (properties will be preserved with no value).

Example

var appendQuery = require('append-query')

appendQuery('http://example.com/foo', 'bar=baz&beep=boop')
// http://example.com/foo?bar=baz&beep=boop
appendQuery('http://example.com/?foo=bar', 'hello=world')
// http://example.com/?foo=bar&hello=world
appendQuery('http://example.com/', { beep: 'boop' })
// http://example.com/?beep=boop
appendQuery('http://example.com/', { nothing: null })
// http://example.com/?nothing

// using pre-encoded values
appendQuery('http://example.com/', { preEncoded: '%22hello%2C%20world!%22' }, { encodeComponents: false })
// http://example.com/?preEncoded=%22hello%2C%20world!%22

// remove existing values
appendQuery('http://example.com/?test=1', { test: null }, { removeNull: true })
// http://example.com/

Running Tests

npm test

Change Log

  • 2.1.0
    • #9 upgrade extend dependency to ^2.0.0
  • 2.0.1
    • fix typo
  • 2.0.0
    • fix #5
    • add options: encodeComponents and removeNull
  • 1.1.0
    • add support for recursive serialization of nested objects
    • add support for arrays as properties

License

(The MIT License)

Copyright 2014 Cameron Lakenen

node-append-query's People

Contributors

andyjdavis avatar ceastman-ibm avatar gabceb avatar lakenen avatar ojhaujjwal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-append-query's Issues

append parameter encoded by encodeURIComponent

appendQuery('http://localhost', { redirectUrl: encodeURIComponent(location.href)});
Result: "http://localhost/?redirectUrl=http%253A%252F%252Flocal.cdp.biz.aliyun.test%253A8088%252F%2523%252F%253F_k%253Djdb1a1"
encodeURIComponent(location.href)
Result: "http%3A%2F%2Flocal.cdp.biz.aliyun.test%3A8088%2F%23%2F%3F_k%3Djdb1a1"

The query, 'redirectUrl' is incorrect produced by appendQuery.

Null values in query objects cause lib to throw

var appendQuery = require("append-query");
var foo = appendQuery('http://bar.com/', { baz: null });

I would expect foo to equal: http://bar.com/?baz
Instead this error is thrown: TypeError: Cannot convert undefined or null to object

Recursive Params?

Any chance of supporting converting {user: {name: 'john'}} to ?user[name]=john?

Great package; thanks!

encodes incorrectly when query string has a `+` symbol

This is probably because we're passing in an un-encoded string as an input to querystring.parse() function in line 13

Looks like querystring.parse() function takes an encoded string, and in line 55 of the library, + symbol is replaced with %20. This makes sense because when encoding a space, it is sometimes encoded as + or %20.

However, in our case + is the raw value and we wan't it to be treated as a + and not a space

Expected behavior
No matter how you pass the query parameters (object or string), it should always produce the same result

Actual behavior
Query parameters which includes + symbol if passed in as string, gets encoded as %20 instead of %2B

test('should encode a url if encodeComponents is truthy when passed query as either object or string', function (t) {
  t.plan(2)

  var token = '0_h5E0i+IAmI8aZXOPEbgAWMiTA='
    , expected = 'http://example.com/?token=' + encodeURIComponent(token)

  // query object includes `+` symbol
  var result1 = appendQuery('http://example.com/', { token: token } , { encodeComponents: true })
  t.equal(result1, expected, 'should be equal')

  // query string includes `+` symbol
  var result2 = appendQuery('http://example.com/', 'token=' + token, { encodeComponents: true })
  t.equal(result2, expected, 'should be equal') 
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.