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

Set of Typescript decorators to build Fastify server with controllers, services and hooks

  • 3.2.3
  • Source
  • npm
  • Socket score

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

Fastify decorators

npm version npm License: MIT

Node.js CI Build example codecov

This package developed to provide useful typescript decorators to implement RequestHandler pattern with Fastify.

NOTE: Fastify-decorators was developed with fastify ^3.0.0 and may not work with other versions.

Install

via npm:

npm install fastify-decorators --save

via yarn:

yarn add fastify-decorators

IDE support

Documentation

Basic usage

Controller

src/sample.controller.ts:

import { Controller, GET } from 'fastify-decorators';

@Controller('/sample')
export default class SampleController {
    @GET('/')
    async handle() {
        return 'It works!';
    }
}

Request Handler

src/sample.handler.ts:

import { GET, RequestHandler } from 'fastify-decorators';

@GET('/sample')
export default class SampleHandler extends RequestHandler {
    async handle() {
        return 'It works!';
    }
}

Bootstrapping

index.ts:

import { bootstrap } from 'fastify-decorators';
import fastify = require('fastify');
import { resolve } from 'path';

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

// Register handlers auto-bootstrap
instance.register(bootstrap, {
    directory: resolve(__dirname, `src`),
    mask: /\.(controller|handler)\./
});

instance.listen(3000);

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
directorystringyesSpecify directory where controllers/handlers are located
maskstring, 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 Sep 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