Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Another type of arrayFormat #116

Closed
d-asensio opened this issue Dec 1, 2017 · 4 comments
Closed

Proposal: Another type of arrayFormat #116

d-asensio opened this issue Dec 1, 2017 · 4 comments

Comments

@d-asensio
Copy link

Hi,

I think that would be good to add another type of arrayFormat in both stringify and parse methods for joining array values with a comma.

The reason of my proposal is that with the OpenAPI 3 specification they allow to do that (see: OpenAPI spec > fixed fields style and explode fields).

I can understand that you want to keep the functionality of this package as standard as possible (based in the HTTP spec) but I think that would be a nice feature for those who use this package to build API-related url query strings.

Of course this would only provide a code style improvement, since you can reach the same result with the current functionality by doing something like this:

queryString.stringify({
    foo: [1,2,3].join(`,`),
    bar: ['a','b','c'].join(`,`)
});

But I think is much more clear to do it this way:

queryString.stringify({
    foo: [1,2,3],
    bar: ['a','b','c']
}, { arrayFormat: "noExplode"});

I would like to submit a PR if you accept my proposal.

PD:
Another option could be to allow the user build its own arrayFormatter by passing a callback instead of a string... Maybe?

Looking forward your feedback!!

@chacestew
Copy link

I second the need for this.

Currently when handling dynamic requests we need to iterate over the payload and manually join any arrays before passing them to query-string. This defeats the purpose of using a library to abstract away the URL parsing.

@sindresorhus
Copy link
Owner

PR welcome. Should include docs and tests :)

@yadielar
Copy link
Contributor

yadielar commented Feb 9, 2018

+1 from me as well. I actually submitted issue #122 and PR #124 trying to solve a problem related to this issue.

In my scenario, I'm working with an API url that uses the following array format for tags:

tags=postal%20office,burger%2C%20fries%20and%20coke

And I was resorting to something similar to what @d-asensio describes:

let tags = ['postal office', 'burger, fries and coke'];

let params = {
  tags: tags.map(t => encodeURIComponent(t)).join(',') // encode manually
}

let qs = queryString.stringify(params, { encode: false }); // disable encoding

This worked just fine for creating the query string, but I stumbled into limitations when parsing it. Since the API url allows commas inside tags, and those are decoded automatically by parse, I was getting the following:

let params = queryString.parse(qs)
// => { tags: 'postal office,burger,fries and coke' }

let tags = params.tags.split(',');
// => ['postal office', 'burger', 'fries and coke'

Therefore my issue suggesting adding the decode option to parse as well, which lets me do:

let params = queryString.parse(qs, { decode: false })
// => { tags: 'postal%20office,burger%2C%20fries%20and%20coke' }

let tags = params.tags.split(',').map(t => decodeURIComponent(t));
// => ['postal office', 'burger, fries and coke'

I share my specific scenario here in case it might bring more light to the usefulness of this issue and #122.

@dalgleish
Copy link

I think this issue was addressed with #167

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants