npm.io
4.0.0-next.5 • Published 2 years ago

@fastify-decorators/typedi

Licence
MIT
Version
4.0.0-next.5
Deps
3
Size
12 kB
Vulns
0
Weekly
0
Stars
306

@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