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
Dependency Injectionis just a design pattern mostly used inOOP. You have a class, you have some dependencies on other classes that you general "inject" during the construction of the object. The dependencies could be passed in the constructor, using a builder pattern, a decorator, etc. In functional programming, you don't have exactly dependencies. You have a function that receives parameters (could be values or other functions) and you return a value or a function. Pass a function as a parameter could be similar to dependency injectionitinpmjs.com/package/iti Disclaimer: author here :)