Injecti
Injecti is a simple dependency injection tool for typescript with the following features:
- First class dependency injection without container
- Fully written in TypeScript
- Clean code compatible
Table of Contents:
Installation
To get started, install injecti using npm or yarn:
npm install injecti
# or
yarn add injecti
Usage
import { inject } from 'injecti';
const insertUserInDB = (name: string) => {
return db.insert('user', { name });
};
const [createUser, createUserFactory] = inject(
{ insertUserInDB },
(deps) => (name: string) => {
return deps.insertUserInDB(name);
}
);
// usage
await createUser('John');
// test
const fakeInsertUserInDB = () => {};
const createUser = createUserFactory({ insertUserInDB: fakeInsertUserInDB });
const result = createUser('John');
// assert fakeInsertUserInDB called once
// assert result is {name: 'John'}
That's it! You can now use injecti to create unit testable functions.