@fleur/di
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

🌼 fleur-di 💉 npm version travis

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'))

Readme

Keywords

none

Package Sidebar

Install

npm i @fleur/di

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

2.38 kB

Total Files

7

Last publish

Collaborators

  • hanakla
  • ragg
  • geta6
  • fsubal1102