This guide describes how to migrate from fastify-decorators
v3 to v4.
Note: migration guide from v2.x to v3.x is here.
v3 | v4 |
---|---|
CJS + ESM | only ESM |
In general there are only few steps to be done:
- In
package.json
: set"type"
field value to"module"
- In
tsconfig.json
: set"module"
toESNext
orES2020
- In
tsconfig.json
: set"target"
toES2015
or newer
Q: How can I adjust tests to work properly with newer version? A: See Testing documentation
Q: I'm using __dirname
or __filename
in my project
A: There's "polyfill" for them, just add this lines to file which uses these variables:
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
v3 | v4 |
---|---|
Built-in DI | DI moved to @fastify-decorators/simple-di |
In order to keep previous behavior there are 3 steps:
-
Add
@fastify-decorators/simple-di
to your application with your favorite package manager -
Remove
reflect-metadata
and explicit import of this library, simple-di package will import it itself -
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 } }