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

support for deep object hierarchies #86

Open
rweng opened this issue May 16, 2013 · 17 comments
Open

support for deep object hierarchies #86

rweng opened this issue May 16, 2013 · 17 comments

Comments

@rweng
Copy link

rweng commented May 16, 2013

Hi,

here is a feature request: It would be nice if one could write:

url.query({user: {name: "Kimba", email: "kimba@example.com"}})

instead of manually creating the nesting. Thanks for URI.js!

@rodneyrehm
Copy link
Member

what's the resulting query string you expect?

@rweng
Copy link
Author

rweng commented May 16, 2013

something like this (only encoded):

/?user[name]=Kimba&user[email]=kimba@example.com

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]": "kimba@example.com"})

@rodneyrehm
Copy link
Member

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…

@rodneyrehm
Copy link
Member

bump! any comments on that branch, @rweng?

@rweng
Copy link
Author

rweng commented Aug 8, 2013

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.

@ioncreature
Copy link

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

@rodneyrehm
Copy link
Member

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…)

@ioncreature
Copy link

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.

@dettier
Copy link

dettier commented Feb 12, 2015

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.

@rodneyrehm rodneyrehm added this to the v2.0.0 milestone Mar 11, 2015
@nitsujri
Copy link

nitsujri commented Apr 2, 2015

+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.

@lamchau
Copy link

lamchau commented Jan 14, 2016

👍 @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

@rodneyrehm
Copy link
Member

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?

@lamchau
Copy link

lamchau commented Jan 14, 2016

@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}}'

@rodneyrehm
Copy link
Member

I see. if you need to cater to JSON, you're likely better off overwriting URI.parseQuery and URI.buildQuery accordingly.

@sanjeevkpandit
Copy link

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.

@iklyaus
Copy link

iklyaus commented Mar 11, 2016

+1 need it

@alexweissman
Copy link

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.

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

9 participants