tsyringe
- Version 4.10.0
- Published
- 149 kB
- 1 dependency
- MIT license
Install
npm i tsyringe
yarn add tsyringe
pnpm add tsyringe
Overview
Lightweight dependency injection container for JavaScript/TypeScript
Index
Variables
Functions
- autoInjectable()
- delay()
- inject()
- injectable()
- injectAll()
- injectAllWithTransform()
- injectWithTransform()
- instanceCachingFactory()
- instancePerContainerCachingFactory()
- isClassProvider()
- isFactoryProvider()
- isNormalToken()
- isTokenProvider()
- isValueProvider()
- predicateAwareClassFactory()
- registry()
- scoped()
- singleton()
Interfaces
Enums
Type Aliases
Variables
variable container
const container: DependencyContainer;
Functions
function autoInjectable
autoInjectable: () => (target: constructor<any>) => any;
Class decorator factory that replaces the decorated class' constructor with a parameterless constructor that has dependencies auto-resolved
Note: Resolution is performed using the global container
{Function} The class decorator
function delay
delay: <T>(wrappedConstructor: () => constructor<T>) => DelayedConstructor<T>;
function inject
inject: ( token: InjectionToken<any>, options?: { isOptional?: boolean }) => ( target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => any;
Parameter decorator factory that allows for interface information to be stored in the constructor's metadata
{Function} The parameter decorator
function injectable
injectable: <T>(options?: { token?: InjectionToken<T> | InjectionToken<T>[];}) => (target: constructor<T>) => void;
Class decorator factory that allows the class' dependencies to be injected at runtime.
{Function} The class decorator
function injectAll
injectAll: ( token: InjectionToken<any>, options?: { isOptional?: boolean }) => ( target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => any;
Parameter decorator factory that allows for interface information to be stored in the constructor's metadata
{Function} The parameter decorator
function injectAllWithTransform
injectAllWithTransform: ( token: InjectionToken<any>, transformer: InjectionToken<Transform<[any], any>>, ...args: any[]) => ( target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => any;
Parameter decorator factory that allows for interface information to be stored in the constructor's metadata
{Function} The parameter decorator
function injectWithTransform
injectWithTransform: ( token: InjectionToken<any>, transformer: InjectionToken<Transform<any, any>>, ...args: any[]) => ( target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => any;
Parameter decorator factory that allows for interface information to be stored in the constructor's metadata with a transform token
Parameter token
The token of the object to be resolved
Parameter transformer
The token of the transform object
Parameter args
Arguments to be passed to the transform method on the transformer
Returns
The parameter decorator
function instanceCachingFactory
instanceCachingFactory: <T>( factoryFunc: FactoryFunction<T>) => FactoryFunction<T>;
function instancePerContainerCachingFactory
instancePerContainerCachingFactory: <T>( factoryFunc: FactoryFunction<T>) => FactoryFunction<T>;
function isClassProvider
isClassProvider: <T>(provider: Provider<T>) => provider is ClassProvider<any>;
function isFactoryProvider
isFactoryProvider: <T>( provider: Provider<T>) => provider is FactoryProvider<any>;
function isNormalToken
isNormalToken: (token?: InjectionToken<any>) => token is string | symbol;
function isTokenProvider
isTokenProvider: <T>(provider: Provider<T>) => provider is TokenProvider<any>;
function isValueProvider
isValueProvider: <T>(provider: Provider<T>) => provider is ValueProvider<T>;
function predicateAwareClassFactory
predicateAwareClassFactory: <T>( predicate: (dependencyContainer: DependencyContainer) => boolean, trueConstructor: constructor<T>, falseConstructor: constructor<T>, useCaching?: boolean) => FactoryFunction<T>;
function registry
registry: ( registrations?: ({ token: InjectionToken; options?: RegistrationOptions; } & Provider<any>)[]) => (target: any) => any;
Class decorator factory that allows constructor dependencies to be registered at runtime.
{Function} The class decorator
function scoped
scoped: <T>( lifecycle: Lifecycle.ContainerScoped | Lifecycle.ResolutionScoped, token?: InjectionToken<T>) => (target: constructor<T>) => void;
Class decorator factory that registers the class as a scoped dependency within the global container.
The class decorator
function singleton
singleton: <T>() => (target: constructor<T>) => void;
Class decorator factory that registers the class as a singleton within the global container.
{Function} The class decorator
Interfaces
interface ClassProvider
interface ClassProvider<T> {}
property useClass
useClass: constructor<T> | DelayedConstructor<T>;
interface DependencyContainer
interface DependencyContainer extends Disposable {}
method afterResolution
afterResolution: <T>( token: InjectionToken<T>, callback: PostResolutionInterceptorCallback<T>, options?: InterceptorOptions) => void;
Registers a callback that is called after a successful resolution of the token
Parameter token
The token to intercept
Parameter callback
The callback that is called after the token is resolved
Parameter options
Options for under what circumstances the callback will be called
method beforeResolution
beforeResolution: <T>( token: InjectionToken<T>, callback: PreResolutionInterceptorCallback<T>, options?: InterceptorOptions) => void;
Registers a callback that is called when a specific injection token is resolved
Parameter token
The token to intercept
Parameter callback
The callback that is called before the token is resolved
Parameter options
Options for under what circumstances the callback will be called
method clearInstances
clearInstances: () => void;
method createChildContainer
createChildContainer: () => DependencyContainer;
method dispose
dispose: () => Promise<void> | void;
Calls
.dispose()
on all disposable instances created by the container. After calling this, the container may no longer be used.
method isRegistered
isRegistered: <T>(token: InjectionToken<T>, recursive?: boolean) => boolean;
Check if the given dependency is registered
Parameter token
The token to check
Parameter recursive
Should parent containers be checked? Whether or not the token is registered
method register
register: { <T>( token: InjectionToken<T>, provider: ValueProvider<T> ): DependencyContainer; <T>( token: InjectionToken<T>, provider: FactoryProvider<T> ): DependencyContainer; <T>( token: InjectionToken<T>, provider: TokenProvider<T>, options?: RegistrationOptions ): DependencyContainer; <T>( token: InjectionToken<T>, provider: ClassProvider<T>, options?: RegistrationOptions ): DependencyContainer; <T>( token: InjectionToken<T>, provider: constructor<T>, options?: RegistrationOptions ): DependencyContainer;};
method registerInstance
registerInstance: <T>( token: InjectionToken<T>, instance: T) => DependencyContainer;
method registerSingleton
registerSingleton: { <T>(from: InjectionToken<T>, to: InjectionToken<T>): DependencyContainer; <T>(token: constructor<T>): DependencyContainer;};
method registerType
registerType: <T>( from: InjectionToken<T>, to: InjectionToken<T>) => DependencyContainer;
method reset
reset: () => void;
Clears all registered tokens
method resolve
resolve: <T>(token: InjectionToken<T>) => T;
Resolve a token into an instance
Parameter token
The dependency token An instance of the dependency
method resolveAll
resolveAll: <T>(token: InjectionToken<T>) => T[];
interface Disposable
interface Disposable {}
method dispose
dispose: () => Promise<void> | void;
interface FactoryProvider
interface FactoryProvider<T> {}
Provide a dependency using a factory. Unlike the other providers, this does not support instance caching. If you need instance caching, your factory method must implement it.
property useFactory
useFactory: (dependencyContainer: DependencyContainer) => T;
interface TokenProvider
interface TokenProvider<T> {}
property useToken
useToken: InjectionToken<T>;
interface ValueProvider
interface ValueProvider<T> {}
property useValue
useValue: T;
Enums
enum Lifecycle
enum Lifecycle { Transient = 0, Singleton = 1, ResolutionScoped = 2, ContainerScoped = 3,}
member ContainerScoped
ContainerScoped = 3
member ResolutionScoped
ResolutionScoped = 2
member Singleton
Singleton = 1
member Transient
Transient = 0
Type Aliases
type FactoryFunction
type FactoryFunction<T> = (dependencyContainer: DependencyContainer) => T;
type Frequency
type Frequency = 'Always' | 'Once';
type InjectionToken
type InjectionToken<T = any> = | constructor<T> | string | symbol | DelayedConstructor<T>;
type Provider
type Provider<T = any> = | ClassProvider<T> | ValueProvider<T> | TokenProvider<T> | FactoryProvider<T>;
type RegistrationOptions
type RegistrationOptions = { /** * Customize the lifecycle of the registration * See https://github.com/microsoft/tsyringe#available-scopes for more information */ lifecycle: Lifecycle;};
Package Files (27)
- dist/typings/decorators/auto-injectable.d.ts
- dist/typings/decorators/inject-all-with-transform.d.ts
- dist/typings/decorators/inject-all.d.ts
- dist/typings/decorators/inject-with-transform.d.ts
- dist/typings/decorators/inject.d.ts
- dist/typings/decorators/injectable.d.ts
- dist/typings/decorators/registry.d.ts
- dist/typings/decorators/scoped.d.ts
- dist/typings/decorators/singleton.d.ts
- dist/typings/dependency-container.d.ts
- dist/typings/factories/factory-function.d.ts
- dist/typings/factories/instance-caching-factory.d.ts
- dist/typings/factories/instance-per-container-caching-factory.d.ts
- dist/typings/factories/predicate-aware-class-factory.d.ts
- dist/typings/index.d.ts
- dist/typings/lazy-helpers.d.ts
- dist/typings/providers/class-provider.d.ts
- dist/typings/providers/factory-provider.d.ts
- dist/typings/providers/injection-token.d.ts
- dist/typings/providers/provider.d.ts
- dist/typings/providers/token-provider.d.ts
- dist/typings/providers/value-provider.d.ts
- dist/typings/types/dependency-container.d.ts
- dist/typings/types/disposable.d.ts
- dist/typings/types/frequency.d.ts
- dist/typings/types/lifecycle.d.ts
- dist/typings/types/registration-options.d.ts
Dependencies (1)
Dev Dependencies (14)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/tsyringe
.
- Markdown[](https://www.jsdocs.io/package/tsyringe)
- HTML<a href="https://www.jsdocs.io/package/tsyringe"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3588 ms. - Missing or incorrect documentation? Open an issue for this package.