Skip to content

Latest commit

 

History

History

typedi

@fastify-decorators/typedi

npm version npm License: MIT

Dependency injection

Dependency injection (DI) is widely used mechanism to autowire controller/service dependency. In fastify-decorators DI only available for controllers.

This plugin provides support for integration with TypeDI

Getting started

  1. Install @fastify-decorators/typedi and typedi

  2. Use TypeDI container in the app

    import { useContainer } from '@fastify-decorators/typedi';
    import { fastify } from 'fastify';
    import { bootstrap } from 'fastify-decorators';
    import { Container } from 'typedi';
    
    useContainer(Container);
    
    export const app = fastify();
    
    app.register(bootstrap, {
      directory: import.meta.url,
    });
  3. Write services, annotate them with @Service and inject into controllers using @Inject from TypeDI

Examples