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
Comments
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. |
PR welcome. Should include docs and tests :) |
+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:
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 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 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. |
I think this issue was addressed with #167 |
Hi,
I think that would be good to add another type of
arrayFormat
in bothstringify
andparse
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
andexplode
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:
But I think is much more clear to do it this way:
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!!
The text was updated successfully, but these errors were encountered: