Is there any dependency injection container like InversifyJs but for functional programming in typescript? What I want to achieve is to inject a fake function from my tests end to end in the same way that i do with the OOP like the following example
Test end to end binding fake user repository
import magicContainer from 'magic-library'
import { UserRepositoryInterface } from '..'
import { FakeUserRepository } from '..'
describe(‘My test describe’, () => {
test('My test', async () => {
magicContainer.bind(UserRepositoryInterface, FakeUserRepository)
await supertest(app)
.post('/my-endpoint')
.send({payload})
.expect(HttpStatus.OK)
})
})
Express Router:
import {UserRepositoryInterface} from '...'
import {UserRepository} from '...'
import magicContainer from ‘magic-library’
const router = Router()
router.post('/my-endpoint', magicContainer.bind(UserRepositoryInterface, UserRepository)
)
User Interface and implementations:
export interface UserRepositoryInterface {
myFunction: () => Promise<User[]>
}
export const UserRepository: UserRepositoryInterface = {
myFunction: async () => {
…
}
}
export const FakeUserRepository: UserRepositoryInterface = {
myFunction: async () => {
…
}
}
I have tried with libraries like InversifyJS, tsyringe and node-dependency-injection but they are purely object oriented, not for functional programming
I faced the same problem before, some people argue that dependency injection is a design pattern that should be implemented in OOP only however this is not right, dependency injection and dependency inversion are principles can be applied on both functional programming and OOP The idea is very simple but first we should know the main components for dependency injection:
Example:
1- create a dependency:
2- Create a Dependency Consumer
3- bind the dependencies into the container