3.0.2 • Published 1 year ago

fastify-autoroutes v3.0.2

Weekly downloads
14
License
MIT
Repository
github
Last release
1 year ago

fastify-autoroutes

Banner

JavaScript   TypeScript

NPM version NPM downloads Known Vulnerabilities GitHub license

CI Coverage Status

:star: Thanks to everyone who has starred the project, it means a lot!

Plugin to handle routes in fastify automatically based on directory structure.

:newspaper: Full Documentation

fastify-autoroutes

:rocket: Install

npm install --save fastify-autoroutes

:blue_book: Usage

Register plugin

const fastify = require('fastify')
const server = fastify()

server.register(require('fastify-autoroutes'), {
  dir: './<autoroutes-directory>', // relative to your cwd
})

Create file in autoroutes directory

//file: `<autoroutes-directory>/some/route.js`
//url:  `http://your-host/some/route`

export default (fastifyInstance) => ({
  get: {
    handler: async (request, reply) => 'Hello, Route'
  },
})

Using typescript support for module

//file: `<autoroutes-directory>/some/route.ts`
//url:  `http://your-host/some/route`

import { FastifyInstance } from 'fastify'
import { Resource } from 'fastify-autoroutes'

export default (fastify: FastifyInstance) => <Resource> {
  get: {
    handler: async (request: FastifyRequest, reply: FastifyReply) => 'Hello, Route!'
  }
}

Accepts params in autoroutes

:information_source: file/directory name must follow syntax :paramName or {paramName}

//file: `<autoroutes-directory>/users/{userId}/photos.js`
//mapped to: `<your host>/users/:userId/photos`

export default (fastifyInstance) => ({
  get: {
    handler: (request, reply) => {
      reply.send(`photos of user ${request.params.userId}`)
    }
  },
})

:arrow_forward: Route module definition

Method specification for attributes is available here: Method specification

:information_source: attributes url and method are dynamically provided

Allowed attributes mapped to Http methods in module:

  • delete
  • get
  • head
  • patch
  • post
  • put
  • options

:arrow_forward: Skipping files

to skip file in routes directory, prepend the . or _ character to filename

examples:

routes
├── .ignored-directory
├── _ignored-directory
├── .ignored-js-file.js
├── _ignored-js-file.js
├── .ignored-ts-file.ts
├── _ignored-ts-file.ts
├── ignored-js-test.test.js
└── ignored-ts-test.test.ts

:warning: also any *.test.js and *.test.ts are skipped!

this is useful if you want to have a lib file which contains functions that don't have to be a route, so just create the file with _ prepending character

:page_facing_up: License

Licensed under MIT

:sparkles: Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification.

Contributions of any kind welcome!

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

2 years ago

2.1.4

2 years ago

2.1.5

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.0.24

3 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.19

4 years ago

1.0.20

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.9

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.2

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago