npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-decorators

v3.16.1

Published

Framework aimed to provide useful TypeScript decorators to implement controllers, services and request handlers, built with Fastify.

Downloads

25,225

Readme

Fastify decorators

npm version Jetbrains plugin version npm License: MIT

Node.js CI codecov Language grade: JavaScript

Framework aimed to provide useful TypeScript decorators to implement controllers, services and request handlers, built with Fastify.

Benefits

  • Fastify compatible - Built with Fastify and supports all its features and plugins
    • JSON Schema validation - Build JSON Schemas to validate and speedup your requests and replies
    • High performance - Framework adds as less overhead to Fastify as it can
  • Highly customizable - Create your controllers, services and their methods as you wish
  • 100% TypeScript - Written in TypeScript and comes with all the required typings
  • Built-in DI - Provides simple Dependency Injection interface to bind your entries

Documentation

Fastify versions support

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

IDE Support

Alternatives

  • NestJS - A progressive Node.js framework for building efficient, reliable and scalable server-side applications.
  • Fastify Resty - Modern and declarative REST API framework for superfast and oversimplification backend development, build on top of Fastify and TypeScript.

Getting started

Hello! Thank you for checking out fastify-decorators!

This documents aims to be gentle introduction to the fastify-decorators and its usages.

Prerequisites

  • Typescript
  • Fastify
  • typings for NodeJS (@types/node package installed)

Install

Install with npm

npm i fastify-decorators --save

Install with yarn

yarn add fastify-decorators

Additional TypeScript configuration

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

Your first server

Request handler way

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!';
  }
}

Controllers way

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
  }
}

Build and run server

After all our files done we have to build server before we can run it:

  1. Add to our package.json script to build server:

    "scripts": {
      "build": "tsc"
    }
  2. Run build script With npm:

    npm run build

    with yarn:

    yarn build
  3. Start server

    node index.ts

Awesome, that was easy.

License

This project licensed under MIT License