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;
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
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 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 classhook
decorator to uses on methods to define Fastify Hookname | type | required | description |
---|---|---|---|
route | string | yes | Controller base route |
type | ControllerType enum | no | Define controller behaviour. Default SINGLETON |
name | type | required | description |
---|---|---|---|
name | string | yes | Hook name |
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 |
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.