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

  • 1.2.0
  • 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

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({
    url: '/sample'
})
class SampleHandler extends RequestHandler {
    async handle() {
        return 'It works!';
    }
}

// We should export class to make it accessible to bootstraper
export = SampleHandler;

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({
    route: '/sample'
})
class SampleController {
    @GET({url: '/'})
    async handle() {
        return 'It works!';
    }
}

// We should export class to make it accessible to bootstraper
export = SampleController;

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)
class SimpleHandler extends RequestHandler {
    async handle() {return ''}
}

export = SimpleHandler;

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:
nametyperequireddescription
routestringyesController base route
typeControllerType enumnoDefine controller behaviour. Default SINGLETON
Hook decorator options:
nametyperequireddescription
namestringyesHook name
Handler decorators options (for controllers and handlers both)
nametyperequireddescription
urlstringyesRoute url which will be passed to Fastify
optionsRouteConfignoConfig for route which will be passed to Fastify

Documentation

License

This project licensed under MIT License

Keywords

FAQs

Package last updated on 26 May 2019

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