Skip to content

Commit

Permalink
feat: Added filter option.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Feb 6, 2024
1 parent 07b569d commit 3460eda
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Register as a plugin as early as possible, optional providing any of the followi

- `useColors`: If to use colors to highlight routes.
- `compact`: If to show all routes of the same path in a single line even if they are defined using different handlers. Descripion of unified routes will not be printed.
- `filter`: If provided, a function that receives a route and should return a boolean to determine if the route should be print or not.

Routes can be omitted by the list by setting `hide` option to `true` inside their `config`.
Routes can also be omitted by the list by setting `hide` option to `true` inside their `config`.

Once the server is started, it will print on the console all available routes and methods.

Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { type FastifyError, type FastifyInstance, type FastifyPluginOptions, typ
import fastifyPlugin from 'fastify-plugin'
import { table } from 'table'

type RouteConfig = Record<string, any>

type RouteFilter = (route: RouteOptions) => boolean

interface Schema {
properties: Record<string, unknown>
required: string[]
}

type RouteConfig = Record<string, any>

const methodsOrder = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS']

function getRouteConfig(r: RouteOptions): RouteConfig {
Expand Down Expand Up @@ -52,7 +54,7 @@ function unifyRoutes(routes: RouteOptions[]): RouteOptions[] {
return [...routesMap.values()].sort(sortRoutes)
}

function printRoutes(routes: RouteOptions[], useColors: boolean, compact: boolean): void {
function printRoutes(routes: RouteOptions[], useColors: boolean, compact: boolean, filter: RouteFilter): void {
if (routes.length === 0) {
return
}
Expand All @@ -61,7 +63,7 @@ function printRoutes(routes: RouteOptions[], useColors: boolean, compact: boolea

// Sort and eventually unify routes
/* c8 ignore start */
routes = routes.filter(r => getRouteConfig(r).hide !== true).sort(sortRoutes)
routes = routes.filter(r => getRouteConfig(r).hide !== true && filter(r)).sort(sortRoutes)
/* c8 ignore end */

if (compact) {
Expand Down Expand Up @@ -144,6 +146,7 @@ export const plugin = fastifyPlugin(
function (instance: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void {
const useColors: boolean = options.useColors ?? true
const compact: boolean = options.compact ?? false
const filter: RouteFilter = options.filter ?? (() => true)

const routes: RouteOptions[] = []

Expand All @@ -153,7 +156,7 @@ export const plugin = fastifyPlugin(
})

instance.addHook('onReady', done => {
printRoutes(routes, useColors, compact)
printRoutes(routes, useColors, compact, filter)
done()
})

Expand Down
52 changes: 51 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import fastify from 'fastify'
import fastify, { type RouteOptions } from 'fastify'
import { table } from 'table'
import t from 'tap'
import { plugin as fastifyPrintRoutes } from '../src/index.js'
Expand Down Expand Up @@ -226,6 +226,56 @@ t.test('Plugin', t => {
)
})

t.test('should correctly list filtered routes without colors', async t => {
const logCalls = t.capture(console, 'log')
const server = fastify()

await server.register(fastifyPrintRoutes, {
useColors: false,
filter(route: RouteOptions): boolean {
return route.url === '/abc'
}
})

server.get('/abc', {
handler,
config: {
description: 'Title'
}
})

server.options('/abc', { handler })

server.route({
url: '/another/:params',
method: ['POST', 'GET'],
handler,
config: {
description: 'Title'
}
})

server.route({
url: '/path3',
method: ['POST', 'GET'],
handler,
config: {
hide: true
}
})
await server.listen({ port: 0 })
await server.close()

t.equal(
logCalls()[0].args[0],
generateOutput([
['GET', '/abc', 'Title'],
['HEAD', '/abc', 'Title'],
['OPTIONS', '/abc', '']
])
)
})

t.test('should correctly compact routes', async t => {
const logCalls = t.capture(console, 'log')
const server = fastify()
Expand Down

0 comments on commit 3460eda

Please sign in to comment.