
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
fastify-decorators
Advanced tools
Framework aimed to provide useful TypeScript decorators to implement controllers, services and request handlers, built with Fastify.
Framework aimed to provide useful TypeScript decorators to implement controllers, services and request handlers, built with Fastify.
Fastify Decorators | Fastify |
---|---|
1.x | 2.x |
2.x | 2.x |
< 3.12.x | 3.x |
>= 3.12.x | 3.x & 4.x |
4.x | 4.x |
Hello! Thank you for checking out fastify-decorators!
This documents aims to be gentle introduction to the fastify-decorators and its usages.
@types/node
package installed)Install with npm
npm i fastify-decorators --save
Install with yarn
yarn add fastify-decorators
Fastify-decorators requires experimentalDecorators
feature to be enabled. For this you need to update your TypeScript config:
tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true
}
}
Note: if you struggle which target
please refer to table below:
Node version | target |
---|---|
10.x | es2018 |
12.x | es2019 |
14.x | es2020 |
fastify-decorators
itself use "target": "es2018"
to support NodeJS 10+ (see Node.js ES2018 Support).
Let's write your first server with request handler:
Project structure:
├── index.ts
├── handlers
│ └── first.handler.ts
└── tsconfig.json
index.ts:
import { bootstrap } from 'fastify-decorators';
import { resolve } from 'path';
// Require the framework and instantiate it
const instance = require('fastify')();
// Register handlers auto-bootstrap
instance.register(bootstrap, {
// Specify directory with our handler
directory: resolve(__dirname, `handlers`),
// Specify mask to match only our handler
mask: /\.handler\./,
});
// Run the server!
instance.listen(3000);
handlers/first.handler.ts:
import { GET, RequestHandler } from 'fastify-decorators';
@GET({
url: '/hello',
})
export default class FirstHandler extends RequestHandler {
async handle() {
return 'Hello world!';
}
}
fastify-decorators also provides way to build controllers with multiple handlers:
Project structure:
├── index.ts
├── controllers
│ └── first.controller.ts
└── tsconfig.json
index.ts:
import { bootstrap } from 'fastify-decorators';
import { resolve } from 'path';
// Require the framework and instantiate it
const instance = require('fastify')();
// Register handlers auto-bootstrap
instance.register(bootstrap, {
// Specify directory with our controllers
directory: resolve(__dirname, `controllers`),
// Specify mask to match only our controllers
mask: /\.controller\./,
});
// Run the server!
instance.listen(3000);
controllers/first.controller.ts:
import { Controller, GET } from 'fastify-decorators';
@Controller({ route: '/' })
export default class FirstController {
@GET({ url: '/hello' })
async helloHandler() {
return 'Hello world!';
}
@GET({ url: '/goodbye' })
async goodbyeHandler() {
return 'Bye-bye!';
}
}
Also, we need to enable experimentalDecorators
feature in our TypeScript config
tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true
}
}
After all our files done we have to build server before we can run it:
Add to our package.json script to build server:
"scripts": {
"build": "tsc"
}
Run build script With npm:
npm run build
with yarn:
yarn build
Start server
node index.ts
Awesome, that was easy.
This project licensed under MIT License
FAQs
Framework aimed to provide useful TypeScript decorators to implement controllers, services and request handlers, built with Fastify.
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 1 open source maintainer 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.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.