3.15.1 • Published 6 months ago

fastify-decorators v3.15.1

Weekly downloads
310
License
MIT
Repository
github
Last release
6 months ago

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';

// Create Fastify instance
const instance = fastify();

// Register handlers auto-bootstrap
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';

// Create Fastify instance
const instance = fastify();

// Register handlers auto-bootstrap
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

nametyperequireddescription
handlersDirectorystringyesSpecify directory where handlers are located
handlersMaskstring, RegExpnoSpecify mask for files filter
prefixstringnoSpecify 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 class
  • hook 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:

nametyperequireddescription
routestringyesController base route
typeControllerType enumnoDefine controller behaviour. Default SINGLETON

Hook decorator options:

nametyperequireddescription
namestringyesHook 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:

nametyperequireddescription
urlstringyesRoute url which will be passed to Fastify
optionsRouteConfignoConfig for route which will be passed to Fastify

License

This project licensed under MIT License

4.0.0-next.5

6 months ago

3.15.1

10 months ago

3.15.0

1 year ago

4.0.0-next.4

2 years ago

4.0.0-next.3

2 years ago

3.14.1

2 years ago

3.14.0

2 years ago

3.13.1

2 years ago

3.13.0

2 years ago

3.12.0

2 years ago

4.0.0-next.2

2 years ago

3.11.0

2 years ago

4.0.0-next.1

2 years ago

4.0.0-next.0

3 years ago

3.9.1

3 years ago

3.10.0

3 years ago

3.9.0

3 years ago

3.8.0

3 years ago

3.7.1

3 years ago

3.7.0

3 years ago

3.6.0

3 years ago

3.5.0

3 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.3.1

3 years ago

3.3.0

4 years ago

3.2.4

4 years ago

3.2.3

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

3.0.0-1

4 years ago

2.0.1

4 years ago

3.0.0-0

4 years ago

2.0.0

4 years ago

2.0.0-7

4 years ago

2.0.0-6

4 years ago

2.0.0-5

4 years ago

2.0.0-4

4 years ago

2.0.0-3

4 years ago

2.0.0-2

4 years ago

2.0.0-1

4 years ago

2.0.0-0

4 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago