@inaiat/fastify-di-plugin
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

fastify-di-plugin

A dependency injection plugin for fastify framework, using awilix

Motivation: I really wanted use fastify-awilix but this plugin make things statics. So, basically, this plugin can be used without problem with parallel tests and so on.

Getting started

yarn add @inaiat/fastify-di-plugin awilix

Next, set up the plugin:

import { fastifyAwilixPlugin } from '@inaiat/fastify-di-plugin'

Next, set up the plugin:

declare module '@inaiat/fastify-di-plugin' {
  interface Cradle {
    dateService: Date
    printDate: string
  }
}

const dateService = () => new Date();
const printService = ({dateService: Date}) => dateService().toDateString()

fastify.register(fastifyDiPlugin, {
      module: {
        dateService: asFunction(dateService).singleton(),
        printDate: asFunction(printService).singleton()
}})

server.get(
      '/status',
      async (request) => {
        const cradle = request.diScope.cradle
        return cradle.printDate
      }
    )
    

Package Sidebar

Install

npm i @inaiat/fastify-di-plugin

Weekly Downloads

194

Version

1.1.1

License

MIT

Unpacked Size

15.8 kB

Total Files

21

Last publish

Collaborators

  • inaiat