Fastify decorators
This package developed to provide useful typescript decorators to implement RequestHandler pattern with Fastify.
NOTE: Fastify-decorators was developed with fastify ^2.0.0
and may not work with other versions.
Install
via npm:
npm install fastify-decorators --save
via yarn:
yarn add fastify-decorators
Documentation
Basic usage
Request Handler
index.ts:
import { bootstrap } from 'fastify-decorators';
import fastify = require('fastify');
import { join } from 'path';
const instance = fastify();
instance.register(bootstrap, {
handlersDirectory: join(__dirname, `handlers`),
handlersMask: /\.handler\./
});
instance.listen(3000);
handlers/sample.handler.ts:
import { GET, RequestHandler } from 'fastify-decorators';
@GET('/sample')
export default class SampleHandler extends RequestHandler {
async handle() {
return 'It works!';
}
}
Controller
index.ts:
import { bootstrap } from 'fastify-decorators';
import fastify = require('fastify');
import { join } from 'path';
const instance = fastify();
instance.register(bootstrap, {
controllersDirectory: join(__dirname, `controllers`),
controllersMask: /\.controller\./
});
instance.listen(3000);
handlers/sample.controller.ts:
import { Controller, GET } from 'fastify-decorators';
@Controller('/sample')
export default class SampleController {
@GET('/')
async handle() {
return 'It works!';
}
}
NOTE: Using decorators require experimentalDecorators
to be enabled in tsconfig.json
API
bootstrap
bootstrap
is Fastify plugin to autoload all decorated modules
example:
import fastify = require('fastify');
import {bootstrap} from 'fastify-decorators';
const instance = fastify();
instance.register(bootstrap, options)
Bootstrap options
name | type | required | description |
---|
handlersDirectory | string | yes | Specify directory where handlers are located |
handlersMask | string , RegExp | no | Specify mask for files filter |
prefix | string | no | Specify prefix for routes |
Decorators
List of available decorators for handlers:
GET
POST
PUT
DELETE
HEAD
OPTIONS
ALL
example:
import { POST, RequestHandler } from 'fastify-decorators';
@POST(options)
export default class SimpleHandler extends RequestHandler {
async handle() {return ''}
}
Also fastify-decorators provides decorator for Controllers implementation:
Controller
decorator uses on classhook
decorator to uses on methods to define Fastify Hook- Same decorators as for handlers use on methods to define Fastify Route
Controller decorator options:
Controller accepts string
as route parameter.
It also possible to passthroughs configuration object in case if complex configuration needed:
name | type | required | description |
---|
route | string | yes | Controller base route |
type | ControllerType enum | no | Define controller behaviour. Default SINGLETON |
Hook decorator options:
name | type | required | description |
---|
name | string | yes | Hook name |
Handler decorators options (for controllers and handlers both)
Handler decorators accept srting
as URL parameter.
It also possible to passthroughs configuration object in case if complex configuration needed:
name | type | required | description |
---|
url | string | yes | Route url which will be passed to Fastify |
options | RouteConfig | no | Config for route which will be passed to Fastify |
License
This project licensed under MIT License