@fleur/di
v1.0.2
Published
Simply DI container without any dependency for TypeScript
Downloads
1
Readme
🌼 fleur-di 💉
Simply DI container without any dependency for TypeScript
Example
import { inject } from '@fleur/di'
import { getUser } from './api'
const fetchUser = inject({ getUser })(
(injects) => async (userId: string) => {
await injects.getUser(userId)
},
)
// Fetch user data
await fetchUser('1')
// Inject mock
const getUserMock = async (userId: string) => ({ id: userId })
await fetchUser.inject({ getUser: getUserMock }).exec('1')
// with redux-thunk
export const fetchUserAction = inject({ getUser })(
(injects) => (userId: string) => async (dispatch, getState) => {
const user = await injects.getUser(userId)
dispatch({ type: 'FETCH_USER_SUCCESS', payload: user })
},
)
// in tests
dispatch(fetchUserAction.inject({ getUser: getUserMock }).exec('1'))