All Projects → israeleriston → fastify-register-routes

israeleriston / fastify-register-routes

Licence: MIT license
Plugin to automatically load routes from a specified path and optionally limit loaded file names by a regular expression.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to fastify-register-routes

fastify-loader
The route loader for the cool kids!
Stars: ✭ 17 (+13.33%)
Mutual labels:  routes, fastify
ertuo
Ertuo: quick routing for PHP
Stars: ✭ 29 (+93.33%)
Mutual labels:  routes
intelliroutes
Support for Play Routes in IntelliJ IDEA
Stars: ✭ 21 (+40%)
Mutual labels:  routes
mrapi
A framework for rapid development of API or DAL applications.
Stars: ✭ 20 (+33.33%)
Mutual labels:  fastify
svelte-ssr-example
Svelte SSR example
Stars: ✭ 43 (+186.67%)
Mutual labels:  fastify
fastify-env
Fastify plugin to check environment variables
Stars: ✭ 129 (+760%)
Mutual labels:  fastify
fastify-accepts
Add accepts parser to fastify
Stars: ✭ 51 (+240%)
Mutual labels:  fastify
fastify-axios
Add axios http client to your fastify instance
Stars: ✭ 28 (+86.67%)
Mutual labels:  fastify
fastify-hasura
A Fastify plugin to have fun with Hasura.
Stars: ✭ 30 (+100%)
Mutual labels:  fastify
fastify-awilix
Dependency injection support for fastify
Stars: ✭ 52 (+246.67%)
Mutual labels:  fastify
fastify-postgres
Fastify PostgreSQL connection plugin
Stars: ✭ 144 (+860%)
Mutual labels:  fastify
regbits
C++ templates for type-safe bit manipulation
Stars: ✭ 53 (+253.33%)
Mutual labels:  registers
type-arango
🥑 TypeArango manages ArangoDB collections, documents, relations and routes by taking advantage of TypeScript typings.
Stars: ✭ 55 (+266.67%)
Mutual labels:  routes
fastify-metrics
📊 Fastify plugin that integrates metrics collection and dispatch to statsd
Stars: ✭ 62 (+313.33%)
Mutual labels:  fastify
zuul-route-jdbc-spring-cloud-starter
No description or website provided.
Stars: ✭ 23 (+53.33%)
Mutual labels:  routes
nest-rest-typeorm-boilerplate
🍱 backend with nest (typescript), typeorm, and authentication
Stars: ✭ 142 (+846.67%)
Mutual labels:  fastify
radlkarte
Hand-crafted routing information for cyclists
Stars: ✭ 14 (-6.67%)
Mutual labels:  routes
fastify-vite
This plugin lets you load a Vite client application and set it up for Server-Side Rendering (SSR) with Fastify.
Stars: ✭ 497 (+3213.33%)
Mutual labels:  fastify
railways
A plugin for RubyMine and IntelliJ IDEA Ultimate that adds a panel with routes of Ruby on Rails application.
Stars: ✭ 44 (+193.33%)
Mutual labels:  routes
fastify-graphql-nexus-prisma
GraphQL Server with Fastify, Mercurius, Prisma, and Nexus
Stars: ✭ 125 (+733.33%)
Mutual labels:  fastify

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 :)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].