URI.js icon indicating copy to clipboard operation
URI.js copied to clipboard

support for deep object hierarchies

Open rweng opened this issue 11 years ago • 17 comments

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!

rweng avatar May 16 '13 10:05 rweng

what's the resulting query string you expect?

rodneyrehm avatar May 16 '13 10:05 rodneyrehm

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]"})

rweng avatar May 16 '13 10:05 rweng

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 avatar May 16 '13 10:05 rodneyrehm

bump! any comments on that branch, @rweng?

rodneyrehm avatar Aug 03 '13 16:08 rodneyrehm

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.

rweng avatar Aug 08 '13 09:08 rweng

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

ioncreature avatar Jun 26 '14 19:06 ioncreature

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

rodneyrehm avatar Jun 26 '14 21:06 rodneyrehm

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.

ioncreature avatar Jun 27 '14 10:06 ioncreature

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.

dettier avatar Feb 12 '15 09:02 dettier

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

nitsujri avatar Apr 02 '15 06:04 nitsujri

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

lamchau avatar Jan 14 '16 00:01 lamchau

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 avatar Jan 14 '16 09:01 rodneyrehm

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

lamchau avatar Jan 14 '16 18:01 lamchau

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

rodneyrehm avatar Jan 14 '16 22:01 rodneyrehm

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.

sanjeevkpandit avatar Feb 25 '16 10:02 sanjeevkpandit

+1 need it

iklyaus avatar Mar 11 '16 11:03 iklyaus

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.

alexweissman avatar Jan 29 '17 19:01 alexweissman