Skip to content

Commit

Permalink
0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gendronb committed Apr 18, 2019
1 parent 385a8ef commit 60ca2d7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@ class NamedRoutes {
}

const namedRoutesPlugin = function (fastify, opts, next) {
let instance = new NamedRoutes({
let plugin = new NamedRoutes({
baseUrl: opts.baseUrl
})

fastify.decorate('namedRoutes', plugin)

fastify.addHook('onRoute', (routeOptions) => {
if (routeOptions.config && routeOptions.config.routeName) {
let { routeName } = routeOptions.config

if (instance.routesMap.has(routeName)) {
fastify.log.warn(`Named route ${routeName} already present, skipping...`)
if (plugin.routesMap.has(routeName)) {
// let existingRoute = plugin.routesMap.get(routeName)
// onRoute hook is called twice for normal route prefix or when prefixTrailingSlash = 'both',
// so we should complain ONLY if this is not the case
// if (!existingRoute.config || !existingRoute.config.url) {
fastify.log.warn(`Named route '${routeName}' already present, skipping...`)
// }
} else {
fastify.log.info(`Adding named route => ${routeName}`)
instance.routesMap.set(routeName, routeOptions)
fastify.log.info(`Adding named route '${routeName}'`)
console.debug(routeOptions)
plugin.routesMap.set(routeName, routeOptions)
console.debug(plugin.routesMap)
}
}
})

fastify.decorate('namedRoutes', instance)
next()
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@gendronb/fastify-named-routes",
"version": "0.6.0",
"version": "0.8.0",
"description": "Named routes support for Fastify",
"main": "index.js",
"scripts": {
"test:lint": "standard",
"test:unit": "tap -J test/**/*.test.js",
"test:unit": "tap -J test/**/*.test.js --100",
"test": "npm run test:lint && npm run test:unit"
},
"precommit": "test",
Expand Down
37 changes: 37 additions & 0 deletions test/prefixed-routes.test.js.skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const test = require('tap').test

const plugin = require('..')

const Fastify = require('fastify')

test('does support prefixed routes', t => {
t.plan(1)

const fastify = Fastify({
logger: {
level: 'info'
}
})

fastify.addHook('onRoute', async function (routeOptions) {
// console.debug(!routeOptions.prefixTrailingSlash || routeOptions.prefixTrailingSlash == 'both')
console.debug('onRoute', routeOptions)
})

fastify.register(plugin)

const route = async function (fastify, opts) {
fastify.get('/', { config: { routeName: 'prefixed' } }, async () => 'Test')
}

fastify.register(route, { prefix: '/prefix' })

fastify.listen(0, function () {
console.debug(fastify.namedRoutes.get('prefixed'))
t.equal(fastify.namedRoutes.get('prefixed').path, '/prefix')
// t.equal('toto', 'titi')
fastify.server.unref()
})
})

0 comments on commit 60ca2d7

Please sign in to comment.