Skip to content

Latest commit

 

History

History
112 lines (81 loc) · 2.59 KB

Migration to v4.md

File metadata and controls

112 lines (81 loc) · 2.59 KB

Fastify decorators

Migration from v3.x to v4.x

This guide describes how to migrate from fastify-decorators v3 to v4.

Note: migration guide from v2.x to v3.x is here.

Minimal Fastify/TypeScript/Node.js version changed

Update dependencies:

Dependency Minimal supported version
Node.js 20.12.0
TypeScript 5.0.0
Fastify 4.0.0

ControllerType was renamed

Replace usages of ControllerType with Scope:

before:

import { Controller, ControllerType } from 'fastify-decorators';

@Controller({
  route: '/',
  type: ControllerType.SINGLETON,
})

after:

import { Controller, Scope } from 'fastify-decorators';

@Controller({
  route: '/',
  scope: Scope.SINGLETON,
})

Built-in DI was moved to plugin

What's changed

v3 v4
Built-in DI DI moved to @fastify-decorators/simple-di

How to reflect

In order to keep previous behavior there are 3 steps:

  1. Add @fastify-decorators/simple-di to your application with your favorite package manager

  2. Remove reflect-metadata and explicit import of this library, simple-di package will import it itself

  3. Update imports in your application, for example:

    Before:

    import { Controller, GET, Initializer, Inject, Service } from 'fastify-decorators';
    
    @Service()
    class SampleService {
      @Initializer()
      async initializeService() {
        // some async stuff here
      }
    }
    
    @Controller('/')
    export class SampleController {
      @Inject(SampleService)
      private service!: SampleService;
    
      @GET()
      async handleRequest() {
        // some stuff with service
      }
    }

    After:

    import { Controller, GET } from 'fastify-decorators';
    import { Initializer, Inject, Service } from '@fastify-decorators/simple-di';
    
    @Service()
    class SampleService {
      @Initializer()
      async initializeService() {
        // some async stuff here
      }
    }
    
    @Controller('/')
    export class SampleController {
      @Inject(SampleService)
      private service!: SampleService;
    
      @GET()
      async handleRequest() {
        // some stuff with service
      }
    }