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
Set of Typescript decorators to build Fastify server with controllers, services and hooks
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.
via npm:
npm install fastify-decorators --save
via yarn:
yarn add fastify-decorators
src/sample.controller.ts:
import { Controller, GET } from 'fastify-decorators';
@Controller('/sample')
export default class SampleController {
@GET('/')
async handle() {
return 'It works!';
}
}
src/sample.handler.ts:
import { GET, RequestHandler } from 'fastify-decorators';
@GET('/sample')
export default class SampleHandler extends RequestHandler {
async handle() {
return 'It works!';
}
}
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
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 |
---|---|---|---|
directory | string | yes | Specify directory where controllers/handlers are located |
mask | 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)
export default class SimpleHandler extends RequestHandler {
async handle() {return ''}
}
Also fastify-decorators provides decorator for Controllers implementation:
Controller
decorator uses on classhook
decorator to uses on methods to define Fastify HookController accepts string
as route parameter.
It also possible to passthroughs configuration object in case if complex configuration needed:
name | 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 |
Handler decorators accept srting
as URL parameter.
It also possible to passthroughs configuration object in case if complex configuration needed:
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.