Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-decorators

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-decorators

Fastify decorators collection with bootstraper

  • 2.0.0-4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.5K
increased by13.43%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 15 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc