Skip to content

Latest commit

 

History

History
56 lines (40 loc) · 1.33 KB

README.md

File metadata and controls

56 lines (40 loc) · 1.33 KB

HerbsDI

Herbs Simple Container Services

Purpose

The purpose of this tool is to make it more scalable and make service dependency management easier in your applications.

container
    .addAsFunction(userRepository)
    .addAsClass(UserService)
    .addAsValue('cache', cache)
    .addAsFunction(getUsersUseCase);

const useCase = container.factoryContainer().getUsersUseCase;
var output = await useCase.run({ name: 'Kenedy' });

Running Examples Locally

Make sure you have Node.js installed.

git clone https://github.com/maikmb/herbs-di.git # or clone your own fork
cd herbs-di
npm install

Express middleware

To use HerbsJS with express serve you can use useHerbsDi middleware to configure container like bellow:

const app = express()
const port = 3000

app.use(useHerbsDi(injection))

app.get('/', async (req, res) => {
    const { getUsersUseCase } = req.injection;
    const ret = await getUsersUseCase.run({ name: 'Jhon' });
    if (ret.isOk) {
        res.send({ users: ret.value });
        return;
    }
    res.send({ error: ret.err })
})

Scripts

  • dev:console to run an example di container console
  • dev:express to run an API with express middleware for the di container
  • dev:gql to run an api with graphql middleware for the di container (under construction)