Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
fastify-decorators
Advanced tools
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.
via npm:
npm install fastify-decorators --save
via yarn:
yarn add fastify-decorators
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);
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)
name | type | required | description |
---|---|---|---|
handlersDirectory | string | yes | Specify directory where handlers are located |
handlersMask | string , RegExp | no | Specify mask for files filter |
prefix | string | no | Specify prefix for routes |
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;
name | type | required | description |
---|---|---|---|
url | string | yes | Route url which will be passed to Fastify |
options | RouteConfig | no | Config for route which will be passed to Fastify |
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;
This project licensed under MIT License
FAQs
Framework aimed to provide useful TypeScript decorators to implement controllers, services and request handlers, built with Fastify.
The npm package fastify-decorators receives a total of 4,559 weekly downloads. As such, fastify-decorators popularity was classified as popular.
We found that fastify-decorators demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.