1.0.6 • Published 3 years ago

@hoovee.party/tools v1.0.6

Weekly downloads
15
License
MIT
Repository
github
Last release
3 years ago

@hoovee.party/tools

Simple strongly-typed IOC container

Usage

Create a container in a file available from all other. This container may not require any of your dependencies. It will made dependencies available wherever needed in the codebase.

// example/container.ts

import createTools, { Container } from "../src/index";
export interface Services {}
export type ServicesContainer = Container<Services>;
const tools: ServicesContainer = createTools<Services>();
export default tools;

Register services. Make sure to start services provided asynchronously.

// example/index.ts

import container from "./container";
import ConfigurationProvider from "./services/Configuration/provider";
import RemoteConfigurationProvider from "./services/RemoteConfiguration/provider";
import ServerProvider from "./services/Server/provider";

export default async function start() {
  ConfigurationProvider(container);
  RemoteConfigurationProvider(container);
  ServerProvider(container);

  await container.start("remoteConfiguration");

  container.server.start();
}

Use declaration merging to make TS aware of injected services

// example/services/Configuration/provider.ts

import createConfiguration, { Configuration } from ".";
import { ServicesContainer } from "../../container";

declare module "../../container" {
  interface Services {
    configuration: Configuration;
  }
}

export default (container: ServicesContainer) => {
  container.service("configuration", () => createConfiguration());
};

Examples

Full example in example folder

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago