@armscye/container
v0.5.0
Published
A collection of shared standard TypeScript definitions (@container)
Downloads
165
Maintainers
Readme
@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
| Property | Description |
| --------------------------- | ------------------------------------------ |
| provide: ProviderToken
| A provider token. |
| useClass: NoArgument<any>
| A class to instantiate for the token
. |
| shared?: boolean | When 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
| Property | Description |
| ---------------------------- | ------------------------------------------ |
| provide: ProviderToken
| A provider token. |
| useExisting: ProviderToken
| Existing token
to return. |
| shared?: boolean | When 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
| Property | Description |
| -------------------------- | ----------------------------------------------------------------- |
| provide: ProviderToken
| A provider token. |
| useFactory: Factory<any>
| A factory function to invoke to create an object for the token
. |
| shared?: boolean | When 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
| Property | Description |
| ------------------------ | ----------------------------------------------------------- |
| provide: ProviderToken
| A provider token. |
| useValue: any | The actual value that will be provided for the given token. |
License
This project is licensed under the MIT license.
See LICENSE for more information.