Created
November 11, 2022 14:40
-
-
Save stefanocudini/7682b4d8d53cede80a6cf1a9f01143b8 to your computer and use it in GitHub Desktop.
fastify print routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRouteConfig(r) { | |
return r.config ?? {} | |
} | |
module.exports = function printRoutes(routes) { | |
if (routes.length === 0) { | |
return | |
} | |
// Sort routes | |
routes = routes.filter(r => getRouteConfig(r).hide !== true).sort((a, b) => a.url.localeCompare(b.url)) | |
const hasDescription = routes.some(r => 'description' in getRouteConfig(r)) | |
const rows = [] | |
for (const route of routes) { | |
const methods = Array.isArray(route.method) ? route.method : [route.method] | |
const row = [ | |
methods | |
.sort((a, b) => methodsOrder.indexOf(a) - methodsOrder.indexOf(b)) | |
.join(' | ') | |
, | |
`${route.url.replace(/:\w+|\[:\w+]/g,'$&')}` | |
].join(' ') | |
if (hasDescription) { | |
row.push(`${getRouteConfig(route).description ?? ''}`) | |
} | |
rows.push(row); | |
} | |
console.log(`listen routes:\n`,rows); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment