This is yet another DI container for Javascript/Typescript. The goal of this library is to stay out of the way of your code.
npm i @carlosv2/glueSimply create as many YAML files as needed. Those files will be collected and piled up to compose a single container service. Be aware that the order in which the files are piled up is very important as the latest files will override colliding ids.
Each YAML file can contain a combination of the following keys (they all are optional):
- extends
- parameters
- services
If set, this key must contain an array of relative paths pointing at other YAML files.
The loader will load those paths and apply ther contents into the file with the extends
key as if the contents had been declared on it.
If set, this key must contain a key/value object defining parameters that can be used on other parameters and/or services, or can be pulled directly from the container.
Each parameter value can be declared with any valid value.
If set, this key must contain a key/value object defining services that can be used on other parameters and/or services, or can be pulled directly from the container.
Each service value must be either an string or an object.
When a service is defined as an string, it is considered to be an alias. This is, the service points at the service pointed in the string.
When a service is defined as an object, the following keys are expected:
- symbol: Mandatory. A value resulting in an object
- args: Optional. A list of values in the same order as the expected arguments.
- factory: Optional. A value resulting in the name of a method in the symbol's object.
- property: Optional. A value resulting in the name of a property in the symbol's object.
- calls: Optional. A list of calls to be executed on the resulting instance.
- tags: Optional. A list of strings with the tags associated to the service.
- scope: Optional. A free-form string with the name of the context this service belongs to.
Please, bear in mind that ehe factory and property keys are mutually exclusive.
A value is any data of any type (including objects of any depth and array). They can also be evaluated dynamically on runtime. In order to evaluate values on runtime, they must be defined as an string with one of the following leading characters:
<: This returns the associated service. For example, the following YAML file:Will inject theservices: my.dependency: symbol: '(my/dependency.ts)~' my.service: symbol: '(my/service)~' args: ['<my.dependency']
my.dependencyservice as argument to themy.serviceservice.$: This returns the associated parameter. For example, the following YAML file:Will inject theparameters: name: glue services: my.service: symbol: '(my/service)~' args: ['$name']
nameparameter as argument to themy.serviceservice.>: This returns an array of services containing the given tag. For example, the following YAML file:Will be interpreted asservices: my.child1: symbol: '(my/child1.ts)~' tag: ['children'] my.child2: symbol: '(my/child2.ts)~' tag: ['children'] my.parent: symbol: '(my/parent)~' args: ['>children']
['<my.child1', '<my.child2']as the argument to themy.parentservice.}: This returns an object of services where the keys are the ids and the values are the instances.Will be interpreted asservices: my.child1: symbol: '(my/child1.ts)~' tag: ['children'] my.child2: symbol: '(my/child2.ts)~' tag: ['children'] my.parent: symbol: '(my/parent)~' args: ['}children']
{'my.child1': '<my.child1', 'my.child2': '<my.child2'}as the argument to themy.parentservice.%: This returns the environment variable. It has the format%<name>[/<type>[/<fallback>]]where:name: Name of the environment variabletype: Type the environemnt variable will be interepreted on. It must be one of:s: stringb: booleann: number
fallback: Fallback value in case the environment variable is not found For example, given the environment variableMY_VAR=4, the following YAML file:
Will create the parameterparameters: var1: '%MY_VAR' var2: '%MY_VAR/n' var3: '%MY_VAR/n/5' var4: '%MY_OTHER_VAR' var5: '%MY_OTHER_VAR/n/5'
var1with value'4'(as string), thevar2with value4(as number), thevar3with value4(as number), thevar5with value5(as number). Note that the definition forvar4will produce an exception because the environment variableMY_OTHER_VARis not found.!: This returns the evaluation of the code. For example, the following YAML file:Will create the parameterparameters: now: '!Date.now()'
nowwith the current timestamp.(: This imports an object from the given relative path according to the termination.[: This imports an array of objects from the given relative pattern according to the termination. The*character is allowed as a placeholder for a single directory while the**characters are allowed as a placeholder for any amount of nested directories.
Service definitions are also valid values thus allowing service definitions to have other service definitions nested.
When using the ( and [ leading characters on a value, the termination will determine
what will be imported:
- No termination: Will import the entire module. For example, the following YAML file:
Will be interpreted as
parameters: obj: '(my/path.ts'
import * as obj from 'my/path'; ): Will import the default object from the module. For example, the following YAML file:Will be interpreted asparameters: obj: '(my/path.ts)'
import obj from 'my/path';)<name>: Will import the named object from the module. For example, the following YAML file:Will be interpreted asparameters: obj: '(my/path.ts)MyObj'
import { MyObj } from 'my/path';)~: Will import the only exported object in the file. If more than 1 object is exported, an error will be raised. For example, given the followingmy/path.tsfile:Then, the following YAML file:function addition(a: number, b: number): number { return a + b; } export const value = addition(4, 5);
Will be interpreted asparameters: obj: '(my/path.ts)~'
import { value } from 'my/path';)~<type>: Will import the only exported object of the maching type in the file. If more than 1 object of the requested type is exported, an error will be raised. Currently only the following types are accepted:class: For classesfunc: For functions For example, given the followingmy/path.tsfile:
Then, the following YAML file:export function addition(a: number, b: number): number { return a + b; } export const value = addition(4, 5);
Will be interpreted asparameters: obj: '(my/path.ts)~func'
import { addition } from 'my/path';
The container object implements the following methods:
get<T = unknown>(id: string): Promise<T>: It returns the service pointed byid.getParameter<T = unknown>(id: string): Promise<T>: It returns the parameter pointed byid.findServiceIdsByTag(tag: string): string[]: It returns a list of service ids containing thetag.
The following is a non-exhaustive list of tasks to be done:
- Improve the documentation (add documentation for loading contianers)
- Improve the non-compiled support