Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created November 11, 2022 14:40
Show Gist options
  • Save stefanocudini/7682b4d8d53cede80a6cf1a9f01143b8 to your computer and use it in GitHub Desktop.
Save stefanocudini/7682b4d8d53cede80a6cf1a9f01143b8 to your computer and use it in GitHub Desktop.
fastify print routes
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