2

According to some JSON API specifciations, such as http://jsonapi.org/format/#urls-individual-resources, the server should interpret a GET request to /myResources/1,2,3,4,5.

Right now the Sails.js router sends this to the findOne action.

What is the best way to get this to be handled properly?

I think ideally the router should interpret /1,2,3,4,5 in the same way it would interpret ?ids[1,2,3,4,5], but I realize this may not be trivial.

Another option is to add a policy to all findOne requests to check if req.params('id') has commas and then reformat the list of ids to a query string and then redirect to find (would this work?). Kind of annoying to add the policy to every controller.

Thoughts?

laser
  • 1,388
  • 13
  • 14
leejt489
  • 615
  • 6
  • 13

1 Answers1

0

I think this was your suggestion, but its simply a matter placing this in the top of the findone.js blueprint. (or adding this as a policy). Just check to make sure its a string (incase your already passing an array.

if(typeof req.params.id === "string"){
    req.params.id = req.params.id.split(',')
}

EDIT:

For anyone implementing, keep in mind that your findOne policies will be applied to this request (see comments). For example, suppose you have a policy for find that applies some parameters to req.options.where to filter records based on the user's permissions. This won't be applied in this case!

EDIT Unless you just make your routes direct these to the index/find route. You could use blueprints routes for everything else, but those with commas in the "key" portion of the url.

Meeker
  • 5,979
  • 2
  • 20
  • 38
  • The problem with this is that then `findOne` has to be able to work as `find` does. It's not a huge amount of code, but it feels like poor practice. – leejt489 Nov 23 '14 at 02:31
  • A I see what you mean. Then your issues is not so much what find or findOne does, but routing a comma list to a the find blueprint? – Meeker Nov 23 '14 at 21:10
  • Exactly. I'm thinking that a policy to redirect might be the best (and it ought to send the request through `find`'s policies as well), and I would also want to fully mask the redirect from the client. – leejt489 Nov 24 '14 at 02:40
  • Instead of a redirect you could just copy the find blueprint into findone custom blueprint folder (with the code above) is the simplest solution and you would get no redirects. – Meeker Nov 24 '14 at 15:32
  • Thanks for your answer. It will certainly work, but it feels like kind of a hack. No offense to you; I think it is the best solution without sails core providing something more elegant. Another wrinkle that I thought of with this approach is that it undermines policies being assigned correctly; i.e. this request should be picked up by `find` policies, but it will be picked up by `findOne` policies. For the most part that shouldn't be a big deal, but something to watch out for! – leejt489 Nov 25 '14 at 04:12
  • Actually, now that I think about it, that could actually be a serious issue. See my edit. – leejt489 Nov 25 '14 at 04:20
  • If the solution feels hacky, then its because you need to hack a bit to achieve your hacky urls. You could easily keep everything as is by using the api as intended with a url such as /myResources?id=1,2,3,4,5,6 or a the following encoded /myResources?where={id : {in:'1,2,3,4,5'}} – Meeker Nov 25 '14 at 14:30
  • I suppose i should not call your URL hacky as they are spec, but you get what I mean. Their are methods available so as to not hack the code, but you need to change your urls. – Meeker Nov 25 '14 at 14:35
  • That is ultimately what I did. FYI, they're not my urls, but from the JSON API spec http://jsonapi.org/format/#urls-individual-resources and I ran into this problem using a client side library that expects this by default – leejt489 Nov 25 '14 at 23:06
  • Use `req.params.id.constructor === String` instead. It is the fastest and will accurately give you the type. `typeof` will call an Array an Object. `.constructor` will call an Array an Array. Check out this http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript/26633883#26633883 as it has a explanation and link to jsperf.com so you can do the test. – jemiloii Dec 05 '14 at 20:49