@athenna/ioc
Version:
Global Ioc helper for Athenna ecosystem. Built on top of awilix.
53 lines (52 loc) • 1.49 kB
JavaScript
/**
* @athenna/ioc
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Options } from '@athenna/common';
export class Annotation {
/**
* Verify if provider is decorated or not.
*/
static isAnnotated(target) {
return Reflect.hasMetadata('ioc:registered', target);
}
/**
* Define all metadata for a service.
*/
static defineMeta(target, options) {
Object.keys(options).forEach(key => {
const value = options[key];
Options.whenDefined(value, value => {
Reflect.defineMetadata(`ioc:${key}`, value, target);
});
});
Reflect.defineMetadata('ioc:registered', false, target);
}
/**
* Define the service as registered.
*/
static defineAsRegistered(target) {
Reflect.defineMetadata('ioc:registered', true, target);
}
/**
* Get all the metadata from the service.
*/
static getMeta(target) {
const meta = {};
Reflect.getMetadataKeys(target).forEach(key => {
const value = Reflect.getMetadata(key, target);
meta[key.replace('ioc:', '')] = value;
});
if (!meta.type) {
meta.type = 'transient';
}
if (!meta.alias) {
meta.alias = `App/Services/${target.name}`;
}
return meta;
}
}