All Projects → L2jLiga → Fastify Decorators

L2jLiga / Fastify Decorators

Licence: mit
Set of Typescript decorators to build Fastify server with controllers, services and hooks

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Fastify Decorators

React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+56.47%)
Mutual labels:  decorators, dependency-injection
Testdeck
Object oriented testing
Stars: ✭ 206 (+142.35%)
Mutual labels:  decorators, dependency-injection
Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+2183.53%)
Mutual labels:  decorators, dependency-injection
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+2664.71%)
Mutual labels:  dependency-injection, decorators
common-injector
Heavily influenced by Angular and it's dependency injection. Inspired by Angular and Indiv.
Stars: ✭ 18 (-78.82%)
Mutual labels:  dependency-injection, decorators
Bottlejs
A powerful dependency injection micro container for JavaScript applications
Stars: ✭ 1,171 (+1277.65%)
Mutual labels:  decorators, dependency-injection
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+101.18%)
Mutual labels:  decorators, dependency-injection
resty
A Node.js framework
Stars: ✭ 20 (-76.47%)
Mutual labels:  dependency-injection, decorators
vue3-oop
使用类和依赖注入写vue组件
Stars: ✭ 90 (+5.88%)
Mutual labels:  dependency-injection, decorators
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (+402.35%)
Mutual labels:  decorators, dependency-injection
Injection Js
Dependency injection library for JavaScript and TypeScript in 5.1K. It is an extraction of the Angular's ReflectiveInjector which means that it's well designed, feature complete, fast, reliable and well tested.
Stars: ✭ 962 (+1031.76%)
Mutual labels:  decorators, dependency-injection
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+1315.29%)
Mutual labels:  dependency-injection
Android App Architecture Mvvm Databinding
A simple but complete project (in both Java & Kotlin) to demonstrate the Android application architecture with MVVM pattern, a client app for The Movie DB Web API. Dagger2 is used for dependency injection and RxJava is used for RFP (Reactive Functional Programming).
Stars: ✭ 69 (-18.82%)
Mutual labels:  dependency-injection
Di
psr/container implementation for humans
Stars: ✭ 69 (-18.82%)
Mutual labels:  dependency-injection
Pure Swift Dependency Injection
Dependency Injection in Pure Swift
Stars: ✭ 67 (-21.18%)
Mutual labels:  dependency-injection
Fluent Symfony
Fluent configuration for Symfony
Stars: ✭ 80 (-5.88%)
Mutual labels:  dependency-injection
Dikit
Dependency Injection Framework for Swift, inspired by KOIN.
Stars: ✭ 77 (-9.41%)
Mutual labels:  dependency-injection
Graphql Typescript
Define and build GraphQL Schemas using typed classes
Stars: ✭ 67 (-21.18%)
Mutual labels:  decorators
App
Reusable framework for micro services & command line tools
Stars: ✭ 66 (-22.35%)
Mutual labels:  dependency-injection
Daggraph
Dagger dependency graph generator for Android Developers
Stars: ✭ 1,140 (+1241.18%)
Mutual labels:  dependency-injection

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.

NOTE: fastify-decorators was developed with fastify ^3.0.0 and may not work with other versions.

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

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].