Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

cmdo-inverse

1.0.1 • Public • Published

Inverse

A simple dependency service.

Services

Services is where you create your container tokens.

import { token } from "cmdo-inverse";
 
type Constructor = {
  new (): Country;
}
 
export type Country = {
  greet(): string;
}
 
export type CountryToken = Token<Constructor, Country>;

Providers

Providers are implementation details for the injectable services and are registered with the container.

import { Country } from "country";
 
export class USA implements Country {
  greet(): string {
    return "Howdy";
  }
}
 
export class UK implements Country {
  greet(): string {
    return "Hello";
  }
}

Tokens

import { CountryToken } from "services/country";
 
export type Countries = {
  usa: CountryToken;
  uk: CountryToken;
}

Container

Inverse container consists of a list of injectable transient and singleton tokens. A token consists of a constructor and type definition.

import { Container } from "cmdo-inverse";
import { Tokens } from "tokens";
 
export const container = new Container<Tokens>();

Register Dependency Providers

import { container } from "container";
import { USA, UK } from "countries";
 
container
  .register("usa", USA)
  .register("uk", UK);

Sample Usage

import { container } from "container";
 
export class Sample {
  constructor(public country = container.resolve("usa")) {};
}
 
const sample = new Sample();
 
// typeof sample.country === Country

Install

npm i cmdo-inverse

DownloadsWeekly Downloads

9

Version

1.0.1

License

MIT

Unpacked Size

13.2 kB

Total Files

28

Last publish

Collaborators

  • avatar