koajs / route

Simple route middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Routes with optional parameters

eiriksm opened this issue · comments

This is related to #8. Did not know if you guys wanted me to open an issue for it, post in that issue, or if this might just be me, but anyway...

I just updated a project I had lying around (from 1.0.2 to latest), and ran into a problem with above mentioned new feature (always push 'next' to the route handler fn args). Obviously there are plenty of solutions to the problem, and ways I can work around it, but it had me bugged for a while. Might be my way of structuring things, but if some of you are able to respond (and possibly suggest a better method) that would be real cool. Also, if it is not just me, then this is probably the right place to report this :)

So I had these routes, they all pointed to the same function

/:resource
/:resource/:id
/:resource/:id/:subresource
/:resource/:id/:subresource/:id

These all pointed to this function:

var index = function*(resource, id, subresource, sid) {...}

Now, earlier I returned a list on GET /resource, because I could check if the id parameter was undefined. Which it no longer is (since it is next). And this also cascades to the other cases where I want to return a list of subresources (on GET /resource/123/subresource)

Of course, it's no biggie for me to work around (in many different ways), but I feel like I am patching away the functionality that was added. Suggestions? Am I just doing this really wrong?

Without having seen your code, I could imagine your `ìndex`` function as a bit messy? Why not just split it up in more functions, to match the functionality of the route. In my experience this is a better design.

Well, I guess that is a matter of taste :)

Personally I find the codebase tidy, and the index function just hands the parameters over to the resource function, where it in turn handles it to the function that determines if we are supposed to return a list of the resource or an instance of it, with an id. This way I can just keep adding resources, (or subresources) and have consistency in the route function.

Anyway, no biggie. Just a couple of lines of code to adapt to the new function signature, so fixed for me long before I opened this issue. And of course I see the benefits from having next available as a parameter as bigger than having some minor issues upgrading :)

So, just closing this. At least this might serve as something if someone googles the same thing though. If you think some documentation update would help/be needed, I'll be happy to help.

Keep up the good work, thanks!

@eiriksm alright, thank you