Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/inaiat/fastify-di-plugin
Simple, unobtrusive depency injection for fastify
https://github.com/inaiat/fastify-di-plugin
Last synced: 8 days ago
JSON representation
Simple, unobtrusive depency injection for fastify
- Host: GitHub
- URL: https://github.com/inaiat/fastify-di-plugin
- Owner: inaiat
- Created: 2022-08-14T22:36:25.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-17T18:56:22.000Z (about 1 year ago)
- Last Synced: 2024-10-11T19:06:50.243Z (about 1 month ago)
- Language: TypeScript
- Size: 299 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fastify-di-plugin
A dependency injection plugin for fastify framework, using [awilix](https://github.com/jeffijoe/awilix)
Motivation: I really wanted use [fastify-awilix](https://github.com/fastify/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
```bash
yarn add @inaiat/fastify-di-plugin awilix
```Next, set up the plugin:
```ts
import { fastifyAwilixPlugin } from '@inaiat/fastify-di-plugin'
```Next, set up the plugin:
```ts
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
}
)