-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathparentDiConfig.ts
49 lines (41 loc) · 1.77 KB
/
parentDiConfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { CommonLogger } from '@lokalise/node-core'
import { globalLogger } from '@lokalise/node-core'
import type { Connection } from 'amqplib'
import type { AwilixContainer, NameAndRegistrationPair, Resolver } from 'awilix'
import { Lifetime } from 'awilix'
import type { AppInstance } from '../app.js'
import type { UsersModuleDependencies } from '../modules/users/userDiConfig.js'
import { resolveUsersConfig } from '../modules/users/userDiConfig.js'
import type { CommonDependencies } from './commonDiConfig.js'
import { resolveCommonDiConfig } from './commonDiConfig.js'
import type { DIOptions } from './diConfigUtils.js'
export type ExternalDependencies = {
app?: AppInstance
logger: CommonLogger
amqpConnection?: Connection
}
export const SINGLETON_CONFIG = { lifetime: Lifetime.SINGLETON }
export type DependencyOverrides = Partial<DiConfig>
export type Dependencies = CommonDependencies & UsersModuleDependencies
type DiConfig = NameAndRegistrationPair<Dependencies>
export function registerDependencies(
diContainer: AwilixContainer,
dependencies: ExternalDependencies = { logger: globalLogger },
dependencyOverrides: DependencyOverrides = {},
options: DIOptions = {},
): void {
const diConfig: DiConfig = {
...resolveCommonDiConfig(dependencies, options),
...resolveUsersConfig(options),
}
diContainer.register(diConfig)
for (const [dependencyKey, dependencyValue] of Object.entries(dependencyOverrides)) {
diContainer.register(dependencyKey, dependencyValue as Resolver<unknown>)
}
}
declare module '@fastify/awilix' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Cradle extends Dependencies {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface RequestCradle extends Dependencies {}
}