0.5.0 • Published 3 months ago

@armscye/container v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@armscye/container

A collection of shared standard TypeScript definitions (@container).

Installation

Using npm:

npm install --save-dev @armscye/container

or using yarn:

yarn add @armscye/container --dev

Reference

ClassProvider Interface

Configures the Container to return an instance of useClass for a token.

interface ClassProvider {
  provide: ProviderToken;

  useClass: NoArgument<any>;

  shared?: boolean;
}

Properties

PropertyDescription
provide: ProviderTokenA provider token.
useClass: NoArgument<any>A class to instantiate for the token.
shared?: booleanWhen true, the created instance is cached.

Container Interface

Describes the interface of a container that exposes methods to read its entries.

interface Container {
  get<T>(token: ProviderToken): T;

  has(token: ProviderToken): boolean;
}

Methods

get<T>(token: ProviderToken): T

Retrieves an entry from the container based on its provider token.

Parameters

  • token: The unique identifier of the entry to retrieve.

Returns

The entry associated with the provided token, if found.

Throws

Error if the entry doesn't exist or an error occurs during retrieval.

has(token: ProviderToken): boolean

Checks whether an entry for a specific provider token exists in the container.

Parameters

  • token: The unique identifier of the entry to check for.

Returns

true if an entry for the provided token exists, false otherwise.

ExistingProvider Interface

Configures the Container to return a value of another useExisting token.

interface ExistingProvider {
  provide: ProviderToken;

  useExisting: ProviderToken;

  shared?: boolean;
}

Properties

PropertyDescription
provide: ProviderTokenA provider token.
useExisting: ProviderTokenExisting token to return.
shared?: booleanWhen true, the created instance is cached.

FactoryProvider Interface

Configures the Container to return a value by invoking a useFactory function.

interface FactoryProvider {
  provide: ProviderToken;

  useFactory: Factory<any>;

  shared?: boolean;
}

Properties

PropertyDescription
provide: ProviderTokenA provider token.
useFactory: Factory<any>A factory function to invoke to create an object for the token.
shared?: booleanWhen true, the created instance is cached.

Factory Type

A function to invoke to create an object. The function is invoked with an instance of the container in order to access required dependencies.

type Factory<T = any> = (container: Container) => T;

ProviderToken Type

Token that can be used to retrieve an instance from a container.

type ProviderToken = string | symbol;

Provider Type

Describes how the Container should be configured.

type Provider =
  | ValueProvider
  | ClassProvider
  | FactoryProvider
  | ExistingProvider;

ValueProvider Interface

Configures the Container to return a value for a token.

interface ValueProvider {
  provide: ProviderToken;

  useValue: any;
}

Properties

PropertyDescription
provide: ProviderTokenA provider token.
useValue: anyThe actual value that will be provided for the given token.

License

This project is licensed under the MIT license.

See LICENSE for more information.

0.5.0

3 months ago

0.4.0

3 months ago

0.3.0

4 months ago

0.2.0

6 months ago

0.1.0

10 months ago