injecti
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Injecti

Typing SVG

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.

Package Sidebar

Install

npm i injecti

Weekly Downloads

52

Version

1.0.5

License

MIT

Unpacked Size

6.97 kB

Total Files

8

Last publish

Collaborators

  • flodlc