Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ await app.ready()

For more advanced use cases, check the official [awilix documentation](https://github.com/jeffijoe/awilix).

## Defining awilix dependencies in plugins

Relying on dependency injection in plugins is problematic, because plugins cannot know upfront how they can resolve dependencies from a fastify application.

For this purpose there is a helper class available in `@fastify/awilix`:

```ts
import { FastifyDependencyProvider } from '@fastify/awilix'
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import statement references FastifyDependencyProvider but the actual exported function is resolveDependencyFromApp. The import should be import { resolveDependencyFromApp } from '@fastify/awilix'.

Suggested change
import { FastifyDependencyProvider } from '@fastify/awilix'
import { resolveDependencyFromApp } from '@fastify/awilix'

Copilot uses AI. Check for mistakes.

app = fastify({ logger: true })
await app.register(fastifyAwilixPlugin, {
container: diContainer,
})
app.diContainer.register({
maxUserName: asValue('username'),
})

// ...

function plugin (fastify, opts, next) {
const maxUserName = resolveDependencyFromApp<string>(fastify, 'maxUserName')
// 'username'
}
```

## License

Licensed under [MIT](./LICENSE).
5 changes: 5 additions & 0 deletions lib/fastifyAwilixPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ const fastifyAwilixPlugin = fp(plugin, {
name: '@fastify/awilix'
})

function resolveDependencyFromApp (app, dependencyKey) {
return app.diContainer.resolve(dependencyKey)
}

module.exports = {
diContainer: diContainerProxy,
resolveDependencyFromApp,
diContainerProxy,
diContainerClassic,
fastifyAwilixPlugin
Expand Down
7 changes: 6 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AwilixContainer } from 'awilix'
import { FastifyPluginCallback } from 'fastify'
import { FastifyInstance, FastifyPluginCallback } from 'fastify'
import { AwilixManager } from 'awilix-manager'

export interface Cradle {}
Expand All @@ -17,6 +17,11 @@ declare module 'fastify' {
}
}

/**
* Tries to resolve the dependency and throws an error if it fails
*/
export function resolveDependencyFromApp<T> (app: FastifyInstance<any, any, any, any, any>, dependencyKey: string): T

export type FastifyAwilixOptions = {
disposeOnResponse?: boolean
disposeOnClose?: boolean
Expand Down
18 changes: 18 additions & 0 deletions test/fastifyAwilixPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { describe, it, afterEach } = require('node:test')
const assert = require('node:assert')

const { diContainer, diContainerClassic, fastifyAwilixPlugin } = require('../lib')
const { resolveDependencyFromApp } = require('../lib/fastifyAwilixPlugin')

class UserServiceClassic {
constructor (userRepository, maxUserName, maxEmail) {
Expand Down Expand Up @@ -169,4 +170,21 @@ describe('fastifyAwilixPlugin', () => {
await assert.rejects(app.ready(), /failed to init/)
})
})

describe('FastifyDependencyProvider', () => {
it('resolves dependencies', async () => {
app = fastify({ logger: true })
await app.register(fastifyAwilixPlugin, {
container: diContainer,
})
app.diContainer.register({
maxUserName: asValue('username'),
})

const maxUserName = resolveDependencyFromApp(app, 'maxUserName')
assert.equal(maxUserName, 'username')

await app.close()
})
})
})
Loading