Methods
# inject(categoryopt, transformeropt) → {function}
Allows for the injection of an injectable
decorated dependency into a class property.
The property must be typed for the requested dependency. Only concrete classes are supported; generics are not.
Injected properties should be described like so:
class ClassName { ...Property decorator that injects a dependency into a class property.
sequenceDiagram participant Client participant Decorator participant Injectables Client->>Decorator: @inject() Decorator->>Decorator: Get type from property Decorator->>Decorator: Define metadata Decorator->>Decorator: Define property getter Note over Decorator: When property accessed Client->>Decorator: access property Decorator->>Decorator: Check if instance exists alt Instance exists in WeakMap Decorator-->>Client: Return cached instance else No instance Decorator->>Injectables: get(name) alt Injectable found Injectables-->>Decorator: Return injectable instance opt Has transformer Decorator->>Decorator: Call transformer end Decorator->>Decorator: Store in WeakMap Decorator-->>Client: Return instance else No injectable Decorator-->>Client: Throw error end endParameters:
Name Type Attributes Description category
string
<optional>
Defaults to the class name derived from the property type. Useful when minification occurs and names are changed, or when you want to upcast the object to a different type.
transformer
InstanceTransformer
<optional>
Optional function to transform the injectable instance before it's injected.
A property decorator function that sets up the dependency injection.
function
# injectable(categoryopt, forceopt, instanceCallbackopt) → {function}
Defines a class as an injectable singleton that can be retrieved from the registry. When applied to a class, replaces its constructor with one that returns a singleton instance.
Decorator that marks a class as available for dependency injection.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
category |
string
|
<optional> |
Defaults to the class name. Useful when minification occurs and names are changed, or when you want to upcast the object to a different type. |
force |
boolean
|
<optional> |
Defines if the injectable should override an already existing instance (if any). Only meant for extending decorators. |
instanceCallback |
function
|
<optional> |
Optional callback function that will be called with the instance after creation. |
A decorator function that transforms the class into an injectable.
function