restify / restify/node-restify
Registering a route returns route.name not route
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 975
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 5
Description
- Used appropriate template for the issue type
- Searched both open and closed issues for duplicates of this issue
- Title adequately and concisely reflects the feature or the bug
Bug Report
Restify Version
8.5.1
Node.js Version
any
Expected behaviour
Registering a route should return the {Route} object not the {Route.name} so it meets the documentation. There is no other way to get the routes before middleware it would seem which is not ideal if you want to add some context to each route for use within middleware at a later time.
Actual behaviour
According to the documentation both internal and external (@see https://github.com/restify/node-restify/blob/master/lib/server.js) when registering a route it should return {Route} but the router currently seems to return {Route.name} instead.
Repro case
const route = server.get('*', (...args) => {})
console.log(route); // 'get'
Cause
Here it returns route.name rather than route
You can see that v6 and earlier it did return the actual route
https://github.com/restify/node-restify/blob/6.x/lib/server.js#L1604-L1606
It would look like the only work around is quite hacky and probably wouldn't be safe through new versions (not to mention far more inefficient) which would be something like:
const routeName = server.get('*', (...args) => {})
const { route } = server.router._registry.lookup('GET', routeName)
route.securityLevel = 'none'
or to mutate the server object for each register and apply to a request manually using a middleware
Are you willing and able to fix this?
Yes
Feature Request
Use Case
It is how documented, how the typescript types are built, and what seems to be the expected result so you can add context to routes.
Example API
const route = server.get('*', (...args) => {})
console.log(route); // Route object
route.securityLevel = 'none'
Are you willing and able to implement this?
Yes
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/server.js around lines 1662-1690, then compare the earlier behavior at lib/server.js lines 1604-1606 in the 6.x link. Confirm the route-registration return value against the documented API and the provided reproduction; done means server.get returns the Route object rather than its name.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100