node-muneem / anumargak

Simple but Fastest Web Router in nodejs
Other
39 stars 5 forks source link

Print the list of registered routes #12

Open amitguptagwl opened 6 years ago

amitguptagwl commented 6 years ago

It can be a good user experience if the library can return the list of registered routes. They can be useful for debugging purpose as well.

lucasheim commented 6 years ago

Can I try to tackle this issue? Do you have more details about the method name for this and what should I return? An object with all routes, each formatted like the ones on find?

amitguptagwl commented 6 years ago

Yes please. If you check the code, you'll find there are 2 objects to hold static and dynamic routes. You'll have to iterate them to get the list. You can even create 2 function: one to return a list of routes another for formatted list like a tree.

You can also check recently closed PR.

On Thu 25 Oct, 2018, 6:13 AM Lucas Heim, notifications@github.com wrote:

Can I try to tackle this issue? Do you have more details about the method name for this and what should I return? An object with all routes, each formatted like the ones on find?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/node-muneem/anumargak/issues/12#issuecomment-432874789, or mute the thread https://github.com/notifications/unsubscribe-auth/AHVgKBEKpAWaY1sOsWu0NZ32tgNR6c3Pks5uoQlBgaJpZM4XvurV .

amitguptagwl commented 5 years ago

@lucasheim are you working on this issue?

lucasheim commented 5 years ago

I'm not @amitguptagwl. It's been hard for me to find some time recently.

amitguptagwl commented 5 years ago

no problem

jadach1 commented 5 years ago

Hi, can I please be assigned this issue, I would like to try to tackle it.

jadach1 commented 5 years ago

Hi I have a question regarding a closed PR from October 2018 which is relevant to this issue

https://github.com/node-muneem/anumargak/pull/13

It seems that someone was trying to work on the issue with the router lists and tried to merge their PR but the PR was closed because of lack of features or functionality ? I am not quite sure but I would like to not make the same mistakes going further, so any clarification would be great ! Thank you.

amitguptagwl commented 5 years ago
  1. The PR you mentioned was based on the assumption that someone may use the added code in the PR to print the list of routes.
  2. It was not handling the case for versioned routes.

Expected output

{
  "GET" : [
         "/this/is/static",
         { "/this/is/static/with/versions" : [ '1.2.3', '*' ] } 
         "/this/is/{dynamic}"
  ]
}

The goal is to print the routes without revealing the internal complexity of the application. Though we can prepare the list at the time of adding/deleting the route, printing routes is generally one-time operation hence I was avoiding to store it in a variable. However, this option can be considered to reduce complexity.

jadach1 commented 5 years ago

Thanks for your reply, a few new questions, I see inside of the letsRoute.js file there is a function commented out as shown below on line 541:

/* Anumargak.prototype.print = function(){ var urlTree = {

}

for(var i=0; i < httpMethods.length; i++){
    this.staticRoutes [ httpMethods[i] ]
}

} */

Is this what you are trying to request to be completed, or is that something different ?


Second questions, there are 34 different OBJECTS inside of the staticRoutes and dynamicRoutes objects as shown below: ACL,BIND,CHECKOUT,CONNECT,COPY,DELETE,GET,HEAD,LINK,LOCK,M-SEARCH,MERGE,MKACTIVITY,MKCALENDAR,MKCOL,MOVE,NOTIFY,OPTIONS,PATCH,POST,PROPFIND,PROPPATCH,PURGE,PUT,REBIND,REPORT,SEARCH,SOURCE,SUBSCRIBE,TRACE,UNBIND,UNLINK,UNLOCK,UNSUBSCRIBE

As of right now I am under the assumption that you only want to print the properties from the GET objects as per your example above. However, there are also objects within those objects which brings up the question which data from those sub objects would you like to have displayed for the user ? It is my understanding that you want it to be as clean and user friendly as possible.

As an example if I do a crude console.log[this.staticRoute.GET] it will print

{ '/': { data: { handler: [Function], store: undefined }, verMap: undefined, params: undefined }, '/a': { data: { handler: [Function], store: undefined }, verMap: undefined, params: undefined }, '/login/as/admin': { data: { handler: undefined, store: undefined }, verMap: undefined, params: { role: 'admin' } }, '/login/as/user': { data: { handler: undefined, store: undefined }, verMap: undefined, params: { role: 'user' } }, '/login/as/staff': { data: { handler: undefined, store: undefined }, verMap: undefined, params: { role: 'staff' } }, '/some/route': { data: undefined, verMap: SemVerStore { tree: [Node] }, params: undefined } }

I am guessing the verMap is where the version will be stored, my question is, is there any other data from those sub objects you want to be displayed for the user ?

The above example you provided will give me a starting point but I would like to know exactly what you are looking for.

Thank you.

amitguptagwl commented 5 years ago

I believe most of the things are self-explainable. However, there is nothing wrong to verify them.

  1. The commented print method in the code can be used for this purpose.
  2. If a user registers routes only with GET method then it should print only GET> But you should iterate through all to know which method has the routes.
  3. In the log, you must have noticed that the URLs(routes) are the key of the object that we want to print. But in case if you've gone through the rest code, you must have noticed that the route used to register and the route actually being saved is a little bit different. So I'll suggest you collect the data at the time of registering the route. This will be a simple approach.

Further, we can think to print a route as follow,

  "GET" : [
         "this: : {
               "is" : {
                    "static",
                    "with" : {
                          "versions" : [ '1.2.3', '*' ] 
                     },
                     "{dynamic}"
               }
          }
  ]
}
jadach1 commented 5 years ago

Hi, I submitted a PR but it is failing on a test

1) Anumargak events should emit not-found and request event when the route is not registered Message: TypeError: Cannot read property 'version' of undefined Stack: at at Anumargak.on (C:\Users\Jacob\OSD600\anumargak\src\letsRoute.js:51:18) at UserContext. (C:\Users\Jacob\OSD600\anumargak\tests\event_test.js:53:16) at at runCallback (timers.js:705:18) at tryOnImmediate (timers.js:676:5)

mainly because I am trying to access the version property of objects which don't have a version on them and regardless of what I do ( check to see if there is a version before saving ) it will always fail that test case. I took your advice on collecting the data of the routes when they are being registered in the prorotype.on method but I do not know how to get around this particular problem, if you have any idea of how to approach this that would be great. My alternative would be to print from the staticRoutes and dynamicRoutes objects.

amitguptagwl commented 5 years ago

Sure.. I'll check it tomorrow. Thanks