@faizaanceg/syringe
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

@faizaanceg/syringe

A tiny dependency injection solution written in JS

Installation

npm install @faizaanceg/syringe
yarn add @faizaanceg/syringe

Usage

import { Syringe } from "@faizaanceg/syringe";
const injections = [
  { name: "host", uses: [], injectFn: () => "https://example.com" },
  {
    name: "endpoints",
    uses: [{ name: "host" }],
    injectFn: ({ host }) => ({ url: host + "/" }),
  },
];
Syringe.fill(injections);

console.log(Syringe.inject("endpoints")); // { url: "https://example.com/" }
console.log(Syringe.inject("host")); // "https://example.com"

API

fill

fill method is used to setup the injections that'll be later requested by your app.

Signature

interface InjectionRef {
  name: string;
}

interface Injection<T> {
  name: string;
  uses: InjectionRef[];
  injectFn(): T;
}

fill(injections: Injections[]): void

inject

inject method is used to inject the dependency wherever requested.

Signature

inject<T>(name: string): T

Package Sidebar

Install

npm i @faizaanceg/syringe

Weekly Downloads

7

Version

0.0.1

License

MIT

Unpacked Size

5.47 kB

Total Files

6

Last publish

Collaborators

  • faizaanceg