@softnetics/what-the-dep
TypeScript icon, indicating that this package has built-in type declarations

2.2.1 • Public • Published

what-the-dep

compile time async dependency injection plugin

Installation

bun install @softnetics/what-the-dep

Then create a preload file in your project

// preload.ts
import { whatTheDep } from "@softnetics/what-the-dep";
import { plugin } from "bun";

plugin(whatTheDep());

and add a preload into your bunfig.toml

preload = ["./preload.ts"]

Features

  • [x] Compile time dependency injection
  • [x] Async dependency initialization
  • [x] Circular dependency detection
  • [ ] Circular dependency resolution
  • [ ] Import dependency from other modules

Usage

import { Container } from "@softnetics/what-the-dep";

const container = new Container();

class A {
  constructor(public b: B) {}
}

class B {
  constructor() {}
}

container.register<A>().register<B>();

const a = await container.get<A>();

Initialize class with a factory

import { Container } from "@softnetics/what-the-dep";

const container = new Container();

class A {
  constructor(public b: B) {}
}

class B {
  constructor() {}
}

container.register<B>().register<A>(async (get) => {
  const b = await get<B>();
  return new A(b);
});

const a = await container.get<A>();

Reference to class using an interface

import { Container } from "@softnetics/what-the-dep";

const container = new Container();

interface IA {
  b: B;
}

class A implements IA {
  constructor(public b: B) {}
}

class B {
  constructor() {}
}

container.register<B>().register<IA, A>(async (get) => {
  const b = await get<B>();
  return new A(b);
});

const a = await container.get<IA>();

Package Sidebar

Install

npm i @softnetics/what-the-dep

Weekly Downloads

1

Version

2.2.1

License

MIT

Unpacked Size

122 kB

Total Files

32

Last publish

Collaborators

  • msp5382
  • suphon-t
  • saenyakorn
  • whatthefar