Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@drewjbartlett/tiny-ioc

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@drewjbartlett/tiny-ioc

`tiny-ioc` is a lightweight (less than 1kB) dependency injection / IOC container for TypeScript.

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-80%
Maintainers
1
Weekly downloads
 
Created
Source

Tiny IOC

tiny-ioc is a lightweight (less than 1kB) dependency injection / IOC container for TypeScript.

Core CI

Features

  • ✅ 100% TypeScript
  • ✅ lightweight - < 1kB

Installation

npm i @drewjbartlett/tiny-ioc --save

Usage

Create a container.ts file that creates, registers, and then exports the container.

// container.ts
import { createContainer, Scope } from '@drewjbartlett/tiny-ioc';

const container = createContainer();

container.bind(MyClass, () => new MyClass(), Scope.Singleton);

container.registerFactory(DataSource, () => new DataSource(container.get(HttpClient)));
container.registerSingleton(HttpClient, () => new HttpClient());

export { container }
// some-other-file.ts

import { container } from 'path/to/container';
import { DataSource } from 'path/to/data-source';

export async function makeRequest() {
  try {
    const dataSource = container.get(DataSource);

    return await dataSource.get('/foo/bar');
  } catch (e) {
    //
  }
}

API

  • bind - Bind a dependency with a given scope.
  • bindSingleton - Bind a dependency to the container as a singleton.
  • bindFactory - Bind a dependency to the container as a factory. Each time the dependency is resolved the container will call the factory function.
  • bindOnce - Only bind the given value if there is not already a binding.
  • get - Attempt to resolve a given binding. Will throw if there is no binding found.
  • resetSingleton - Reset a singleton value. If a value has been previously resolved and is registered as a singleton, this will keep the binding but reset the singleton value until the next resolve.
  • bound - Determine if a binding exists or not.
  • unbind - Remove the given binding from the container entirely.
  • swap - Swap the old binding's value with the new value. This is useful when testing.

Using in tests

// some-other-file.ts

import { container } from 'path/to/container';
import { DataSource } from 'path/to/data-source';

export async function makeRequest() {
  try {
    const dataSource = container.get(DataSource);

    return await dataSource.get('/foo/bar');
  } catch (e) {
    //
  }
}
// unit-test.test.ts

import { container } from 'path/to/container';
import { HttpClient } from 'path/to/http-client';
import { makeRequest } from 'path/top/make-request';

class DummyHttpClient {
  get(url: string) {
    return dummyData;
  }
}

it('should make the request', () => {
  container.swap(HttpClient, () => new DummyHttpClient());

  await myRequest(); // this calls .get() on DummyHttpClient
})

Keywords

FAQs

Package last updated on 26 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc