npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-register-routes

v1.1.2

Published

fastify register routes is a simple plugin that contains an scheme the auto registration the routes to fastify.

Downloads

60

Readme

Fastify Register Routes

GitHub issues GitHub forks GitHub stars GitHub license js-standard-style NPM downloads Build Status Coverage Status

fastify register routes (fastify-register-routes) Plugin to automatically load routes from a specified path and optionally limit loaded file names by a regular expression.

Install

npm i fastify-register-routes

Usage

Options

  • regex: You regex to test file name the router Ex.: user-router.js if nothing is informed I'll use the regex standard /((Route)|(Routes)|(route)|(routes))\.js|.mjs$/.

  • showTable: After loaded all routes, will showind one table with all routes registred's by default value is false.

  • path: Path is used to reference the directory for reading files, therefore, is required.

  • useService: allowed injecting methods of services inside the fastify Request object. Accepts as an argument a list of functions, exemple below.

  • schema: In your routes, you can define the schema, according to the documentation of fastify, this parameter is optional, you just need to inform schema: you-schema consult the documentation of fastify, see other examples here

const path = require('path')
const Fastify = require('fastify')
const registerRoutes = require('fastify-register-routes')

const fastify = Fastify()

// path with your's routes files
const defaultPath = path.join(__dirname, './routes')

fastify.register(registerRoutes, {
  regex: /((Route)|(Routes))\.js|.mjs$/,
  showTable: true,
  path: defaultPath
})

// case need confering routes registred's
// fastify provide an log with the routes loaded
// this function ready é executed after the all is completed loading
fastify.ready().then(() => console.log(fastify.printRoutes()))

fastify.listen(3000, '127.0.0.1', err => {
  if (err) {
    throw  err
  }
  console.log(`Listen ${fastify.address().port}`)
})

Options for Routes

  • useWrap: use a flag useWrap with value true, is an envelope of handler methods, i see below
module.exports = {
  path: '/some-route',
  handler: wrapMiddleware((req, res) => {
    const usersRepo = req.$repositories.users
    const data = req.body
    return usersRepo.create(data)
      .then(user => ({ data: user }))
  })
}

Options for method's services Injected and Scheme at routes

// by default, is used ajv for validation schemes.
const schema = {
  querystring: {
    name: { type: 'string' },
    excitement: { type: 'integer' }
  },
  response: {
    200: {
      type: 'object',
      properties: {
        hello: { type: 'string' }
      }
    }
  }
}

const action01 = () => {
  // same code here
  return 'action01'
}

const action02 = () => {
  // your logic here!
  return 'action02'
}

const get = {
  name: 'user-get',
  version: '1.0.0',
  path: '/get-route',
  // your scheme here, any questions, consult the documentation of fastify.
  schema: schema,
  method: 'get',
  service: [ action01, action02 ],
  handler: (req, reply) => {
    const action = req.$service.action01()
    return reply.send({ payload: action })
  }
}

Other Examples using services injections.

 // middleware.js file
 // don't you import much services methods within you logic,
 // you will only need inject on http-route, its file the routes.

const createUser = (req, reply) => {
  const userNews = req.$service.createUser(req.body)
  // Wow! it's simple!
}


// service.js
// example of service
const createUser = (user) => User.create(user)



// route.js
// example of uses


const userRoute = {
  name: 'user-create',
  version: '1.2.1',
  path: '/user-create',
  method: 'post',
  service: [ createUser ],
  handler: middleware.createUser
}

disclaimer

any error can be reported, as issue and I am accepting PR's :)