URI.js
URI.js copied to clipboard
support for deep object hierarchies
Hi,
here is a feature request: It would be nice if one could write:
url.query({user: {name: "Kimba", email: "[email protected]"}})
instead of manually creating the nesting. Thanks for URI.js!
what's the resulting query string you expect?
something like this (only encoded):
/?user[name]=Kimba&user[email][email protected]
I think that's a quite common way to group params. If the syntax described above is not occupied with something else, I definitely favor it to
url.query({"user[name]": "Kimba", "user[email]": "[email protected]"})
Please have a look at #28 .query() Array-Parameters won't fill PHP's $_GET as intended and check out the URI.hooks branch. See if this is the direction you wanted to go. Please provide feedback on that branch - I haven't gotten any from the original request, which is why I just left it there…
bump! any comments on that branch, @rweng?
nope, it isn't that high on my list so I never looked into it. I still think it would be a nifty feature, but I won't have time to implement it.
I'm interested in such feature too. I need it to store state object in URI in my app.
Something line this https://github.com/visionmedia/node-querystring
Are there any reasons not to use the suggested node-querystring library directly? It supports parsing and serializing. I mean, we could easily make uri.query(true)
translate to qs.parse(uri._parts.query)
for convenience, if that's what you're asking for…
(I don't see a reason implementing this again, if it's already been solved…)
Maybe you are right, but in this case it's more convenient for me to use only node-querystring instead if mixin two libraries.
Thanks.
I would really love this to be implemented, as it is really hard to work with nested parameters now. You have to juggle two libs for both parsing and building urls.
+1 I need to be able to parse: ?utf8=✓&d=&lesson_filters[time]=0,20&lesson_filters[neighborhoods][]=City Center&lesson_filters[neighborhoods][]=West Side&lesson_filters[neighborhoods][]=&lesson_filters[activities][]=&lesson_filters[studios][]=
From a rails form, grouped checkboxes.
:+1: @rodneyrehm, what was the reasoning behind not using JSON.stringify
-- cross-browser compatibility? perhaps a custom build (compatible and modern) would be handy?
const uri = URI("/path/to/api");
const expected = {
query: {
$or: {
$regex: ["hello", "world"]
}
}
};
uri.addQuery(expected); // [Object object]
what about a custom handler?
const uri = URI("/path/to/api");
const expected = {
query: {
$or: {
$regex: ["hello", "world"]
}
}
};
// defers until toString is called
uri.addQuery(options, JSON.stringify);
// should also allow for strings
// uri.addQuery(JSON.stringify(options));
const request = URI(uri);
const actual = request.query(JSON.parse); // would call decodeURIComponent() first
assert.deepEquals(actual , expected); // true
i can help out, just need to understand the decisions
what was the reasoning behind not using JSON.stringify -- cross-browser compatibility?
I'm not sure I understand what you want achieve with JSON.stringify
here?
@rodneyrehm: just providing a custom serializer, we have deeply nested objects but from the previous comments some users are expected ?user[name]=john&user[age]=20
where we are expected query='{"user":{"name":"john","age":20}}'
I see. if you need to cater to JSON, you're likely better off overwriting URI.parseQuery
and URI.buildQuery
accordingly.
I think this is a very important improvement, because nested parameters/arrays are used quite often. I came across this problem recently. I thought it was a bug before, but later I noticed that this feature isn't supported as of now.
+1 need it
Mottie's tablesorter plugin has a "sort-to-hash" feature, which stores the current state of a sorted/filtered table in the URL hash. It requires custom decodeHash
and encodeHash
methods to translate the state between a JSON object (which contains a deep hierarchy) and a URL fragment.
It would be cool if URI.js could be used to automatically do this.