-
-
Notifications
You must be signed in to change notification settings - Fork 124
feat: add getRoutes method to list registered routes #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b21c685
73fae83
f491e42
9b19b4d
440a46e
faac557
16b6c77
856dff1
02c672d
2b19160
45f5da5
4f611f4
376f83d
62f14bb
306eefd
e75bcb7
6c071e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -143,6 +143,60 @@ router.param('user_id', function (req, res, next, id) { | |||||
| }) | ||||||
| ``` | ||||||
|
|
||||||
| ### route.getRoutes() | ||||||
|
|
||||||
| Returns an array of all the routes registered on this route, including | ||||||
| all the methods, key, and the options of instance of router. | ||||||
|
|
||||||
| ```js | ||||||
| const router = new Router({ strict: true, caseSensitive: true }) | ||||||
| const admin = new Router({ strict: true, caseSensitive: false }) | ||||||
|
|
||||||
| admin.use((req, res, next) => { | ||||||
| // some middleware for admin routes | ||||||
| next() | ||||||
| }) | ||||||
|
|
||||||
| admin.get('/', (req, res, next) => { | ||||||
| res.end('Hello') | ||||||
| }) | ||||||
|
|
||||||
| router.use("/admin", admin) | ||||||
|
|
||||||
| router.all('/:id', function (req, res) { | ||||||
| res.end('Hello') | ||||||
| }) | ||||||
|
|
||||||
| console.log(router.getRoutes()) | ||||||
| // [ | ||||||
| // { | ||||||
| // name: 'router', | ||||||
| // path: '/admin', | ||||||
| // methods: undefined, | ||||||
| // keys: undefined, | ||||||
| // router: [ | ||||||
| // { | ||||||
| // name: 'handle', | ||||||
| // path: '/', | ||||||
| // methods: ['GET'], | ||||||
| // keys: undefined, | ||||||
| // router: undefined, | ||||||
| // options: { strict: true, caseSensitive: false, end: true }, | ||||||
| // } | ||||||
| // ], | ||||||
| // options: { strict: true, caseSensitive: true, end: false } | ||||||
| // }, | ||||||
| // { | ||||||
| // name: 'handle', | ||||||
| // path: '/:id', | ||||||
| // methods: ['_ALL'], | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed on the call taking an alternative approach to
Suggested change
|
||||||
| // keys: [{ name: 'id', type: "param" }], | ||||||
| // router: undefined, | ||||||
| // options: { strict: true, caseSensitive: true, end: true } | ||||||
| // } | ||||||
| // ] | ||||||
| ``` | ||||||
|
|
||||||
| ### router.route(path) | ||||||
|
|
||||||
| Creates an instance of a single `Route` for the given `path`. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,9 +14,11 @@ | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const isPromise = require('is-promise') | ||||||||||||||||||||||||||||||||||
| const Layer = require('./lib/layer') | ||||||||||||||||||||||||||||||||||
| const { MATCHING_GROUP_REGEXP } = require('./lib/layer') | ||||||||||||||||||||||||||||||||||
| const { METHODS } = require('node:http') | ||||||||||||||||||||||||||||||||||
| const parseUrl = require('parseurl') | ||||||||||||||||||||||||||||||||||
| const Route = require('./lib/route') | ||||||||||||||||||||||||||||||||||
| const pathRegexp = require('path-to-regexp') | ||||||||||||||||||||||||||||||||||
| const debug = require('debug')('router') | ||||||||||||||||||||||||||||||||||
| const deprecate = require('depd')('router') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -441,6 +443,23 @@ Router.prototype.route = function route (path) { | |||||||||||||||||||||||||||||||||
| return route | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * List all registered routes. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @return {Array} An array of route paths | ||||||||||||||||||||||||||||||||||
| * @public | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| Router.prototype.getRoutes = function getRoutes () { | ||||||||||||||||||||||||||||||||||
| const stack = this.stack | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const options = { | ||||||||||||||||||||||||||||||||||
| strict: this.strict, | ||||||||||||||||||||||||||||||||||
| caseSensitive: this.caseSensitive | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return collectRoutes(stack, options) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+452
to
+461
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small formatting nitpick (not required, just personal preference):
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // create Router#VERB functions | ||||||||||||||||||||||||||||||||||
| methods.concat('all').forEach(function (method) { | ||||||||||||||||||||||||||||||||||
| Router.prototype[method] = function (path) { | ||||||||||||||||||||||||||||||||||
|
|
@@ -450,6 +469,118 @@ methods.concat('all').forEach(function (method) { | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Collect routes from a router stack recursively. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @param {Array} stack - The router stack to collect routes from | ||||||||||||||||||||||||||||||||||
| * @param {object} options - The router options | ||||||||||||||||||||||||||||||||||
| * @private | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| function collectRoutes (stack, options) { | ||||||||||||||||||||||||||||||||||
| const routes = [] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| for (const layer of stack) { | ||||||||||||||||||||||||||||||||||
| // route layer (has methods) | ||||||||||||||||||||||||||||||||||
| if (layer.pathPatterns && layer.route) { | ||||||||||||||||||||||||||||||||||
| const methods = Object.keys(layer.route.methods).map((method) => method.toUpperCase()) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (Array.isArray(layer.pathPatterns)) { | ||||||||||||||||||||||||||||||||||
| for (const pathPattern of layer.pathPatterns) { | ||||||||||||||||||||||||||||||||||
| const keys = extractPatternKeys(pathPattern) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| routes.push({ | ||||||||||||||||||||||||||||||||||
| name: layer.name, | ||||||||||||||||||||||||||||||||||
| path: pathPattern, | ||||||||||||||||||||||||||||||||||
| keys, | ||||||||||||||||||||||||||||||||||
| methods, | ||||||||||||||||||||||||||||||||||
| router: undefined, | ||||||||||||||||||||||||||||||||||
| options: { ...options, end: layer.end } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| const keys = extractPatternKeys(layer.pathPatterns) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| routes.push({ | ||||||||||||||||||||||||||||||||||
| name: layer.name, | ||||||||||||||||||||||||||||||||||
| path: layer.pathPatterns, | ||||||||||||||||||||||||||||||||||
| keys, | ||||||||||||||||||||||||||||||||||
| methods, | ||||||||||||||||||||||||||||||||||
| router: undefined, | ||||||||||||||||||||||||||||||||||
| options: { ...options, end: layer.end } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // mounted router (use) | ||||||||||||||||||||||||||||||||||
| if (layer.pathPatterns && layer.handle && layer.handle.stack && !layer.route) { | ||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path will never be undefined .use always sets the path to '/' when no path is explicitly provided in the arguments.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the kind of thing that should be nailed down for the API, but understood. It probably is reasonable to keep it as
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E.g. why
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed here, lets just simplify this down into the bare minimum api before merging this PR. Then we can take on nesting routers and deeply iterating the stack more directly later if we deem it worth it. We want to unstuck this progress, and I think these details is what I got hung up on last time leading to my original blocking review. Better to land what we can for sure agree on adds value and move this forward. |
||||||||||||||||||||||||||||||||||
| if (Array.isArray(layer.pathPatterns)) { | ||||||||||||||||||||||||||||||||||
| for (const pathPattern of layer.pathPatterns) { | ||||||||||||||||||||||||||||||||||
| const inner = collectRoutes( | ||||||||||||||||||||||||||||||||||
| layer.handle.stack, | ||||||||||||||||||||||||||||||||||
| { strict: layer.handle.strict, caseSensitive: layer.handle.caseSensitive } | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| const keys = extractPatternKeys(pathPattern) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| routes.push({ | ||||||||||||||||||||||||||||||||||
| name: layer.name, | ||||||||||||||||||||||||||||||||||
| path: pathPattern, | ||||||||||||||||||||||||||||||||||
| keys, | ||||||||||||||||||||||||||||||||||
| methods: undefined, | ||||||||||||||||||||||||||||||||||
| router: inner.length ? inner : undefined, | ||||||||||||||||||||||||||||||||||
| options: { ...options, end: layer.end } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| const inner = collectRoutes( | ||||||||||||||||||||||||||||||||||
| layer.handle.stack, | ||||||||||||||||||||||||||||||||||
| { strict: layer.handle.strict, caseSensitive: layer.handle.caseSensitive } | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| const keys = extractPatternKeys(layer.pathPatterns) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| routes.push({ | ||||||||||||||||||||||||||||||||||
| name: layer.name, | ||||||||||||||||||||||||||||||||||
| path: layer.pathPatterns, | ||||||||||||||||||||||||||||||||||
| keys, | ||||||||||||||||||||||||||||||||||
| methods: undefined, | ||||||||||||||||||||||||||||||||||
| router: inner.length ? inner : undefined, | ||||||||||||||||||||||||||||||||||
| options: { ...options, end: layer.end } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return routes | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Extracts parameter/key descriptors from a route pattern. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @param {string|RegExp} pattern - Route pattern to analyze (path string or RegExp). | ||||||||||||||||||||||||||||||||||
| * @returns {Array<Object>|undefined} Array of key descriptor objects (each with at least a `name` property), or `undefined` if none found. | ||||||||||||||||||||||||||||||||||
| * @private | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| function extractPatternKeys (pattern) { | ||||||||||||||||||||||||||||||||||
| if (pattern instanceof RegExp) { | ||||||||||||||||||||||||||||||||||
| const keys = [] | ||||||||||||||||||||||||||||||||||
| let name = 0 | ||||||||||||||||||||||||||||||||||
| let m | ||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line no-cond-assign | ||||||||||||||||||||||||||||||||||
| while (m = MATCHING_GROUP_REGEXP.exec(pattern.source)) { | ||||||||||||||||||||||||||||||||||
| keys.push({ name: m[1] || name++ }) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return keys.length > 0 ? keys : undefined | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const pathKeys = pathRegexp.pathToRegexp(String(pattern)).keys | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (pathKeys && pathKeys.length > 0) { | ||||||||||||||||||||||||||||||||||
| return pathKeys | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return undefined | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Generate a callback that will make an OPTIONS response. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ const MATCHING_GROUP_REGEXP = /\((?:\?<(.*?)>)?(?!\?)/g | |
| */ | ||
|
|
||
| module.exports = Layer | ||
| module.exports.MATCHING_GROUP_REGEXP = MATCHING_GROUP_REGEXP | ||
|
|
||
| function Layer (path, options, fn) { | ||
| if (!(this instanceof Layer)) { | ||
|
|
@@ -43,7 +44,11 @@ function Layer (path, options, fn) { | |
| this.keys = [] | ||
| this.name = fn.name || '<anonymous>' | ||
| this.params = undefined | ||
| // path is determinate in runtime execution | ||
| this.path = undefined | ||
| this.end = opts.end | ||
|
|
||
| this.pathPatterns = path | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming of this implies to me two things:
Maybe a nitpick the naming would be we could call it |
||
| this.slash = path === '/' && opts.end === false | ||
|
|
||
| function matcher (_path) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wont block on this but Id prefer to see
route.routes()orroute.listRoutes()to avoid intellisense/tab complete conflict when folks typeapp/router.gin an attempt to type.getThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
listRoutes()is reasonable.