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.0.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

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;

NOTE: Using decorators require experimentalDecorators to be enabled in tsconfig.json

otherwise decorators won't work but you still can use it without them:

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

class SampleHandler extends RequestHandler {
    async handle() {
        return 'It works!';
    }
}

// BTW decorators is just a functions :)
export = GET({ url: '/sample' })(SampleHandler);

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:

  • 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;
Decorators options
nametyperequireddescription
urlstringyesRoute url which will be passed to Fastify
optionsRouteConfignoConfig for route which will be passed to Fastify

How it works

Under the hood decorators create static method register in your class and then bootstraper use it to register it.

It means that this code:

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

@PUT({
    url: '/sample'
})
class SimplePutHandler extends RequestHandler {
    async handle() {
        return this.request.body.message;
    }
}

export = SimplePutHandler;

becomes:

import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { REGISTER } from 'fastify-decorators';
import { IncomingMessage, ServerResponse } from 'http';

class SimplePutHandler {
    constructor(protected request: FastifyRequest<IncomingMessage>,
                protected reply: FastifyReply<ServerResponse>) {
    }

    async handle() {
        return this.request.body.message;
    }

    static [REGISTER] = (instance: FastifyInstance) => instance.put(`/sample`, {}, (req, res) => new SimplePutHandler(req, res).handle());
}

export = SimplePutHandler;

License

This project licensed under MIT License

Keywords

FAQs

Package last updated on 14 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