@expressots/core
Version:
Expressots - modern, fast, lightweight nodejs web framework (@core)
99 lines (98 loc) • 4.01 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.scope = exports.CreateModule = exports.BaseModule = exports.BINDING_TYPE_METADATA_KEY = void 0;
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-explicit-any */
const inversify_1 = require("../di/inversify");
const decorator_1 = require("../decorator");
const binding_decorator_1 = require("../di/binding-decorator");
/**
* Key to be used for storing and retrieving binding type metadata.
*/
exports.BINDING_TYPE_METADATA_KEY = "binding-type";
/**
* The scope decorator is a higher order function that can be used to decorate a class with a binding type.
* @param binding An instance of interfaces.BindingScope which represents the binding type.
* @returns A decorator function that can be used to decorate a class with a binding type.
* @public API
*/
const scope = (binding) => {
return function (target) {
if (!Reflect.hasMetadata(exports.BINDING_TYPE_METADATA_KEY, target)) {
Reflect.defineMetadata(exports.BINDING_TYPE_METADATA_KEY, binding, target);
switch (binding) {
case inversify_1.BindingScopeEnum.Singleton:
(0, decorator_1.provideSingleton)(target);
break;
case inversify_1.BindingScopeEnum.Transient:
(0, decorator_1.provideTransient)(target);
break;
default:
(0, binding_decorator_1.provide)(target);
break;
}
}
};
};
exports.scope = scope;
/**
* The BaseModule class provides methods for creating InversifyJS container modules.
* @provide BaseModule
*/
class BaseModule {
/**
* Create a map of symbols for the provided controllers.
* @param controllers - An array of controller classes.
* @returns A map of symbols mapped to controller constructor functions.
*/
static createSymbols(controllers) {
const symbols = new Map();
for (const controller of controllers) {
const target = controller;
const symbol = Symbol.for(target.name);
symbols.set(symbol, target);
}
return symbols;
}
static bindToScope(symbol, target, bindingType, bind) {
switch (bindingType) {
case inversify_1.BindingScopeEnum.Singleton:
bind(symbol).to(target).inSingletonScope();
break;
case inversify_1.BindingScopeEnum.Transient:
bind(symbol).to(target).inTransientScope();
(0, decorator_1.provideTransient)(target);
break;
case inversify_1.BindingScopeEnum.Request:
bind(symbol).to(target).inRequestScope();
break;
default:
bind(symbol).to(target).inRequestScope();
break;
}
}
/**
* Create an InversifyJS ContainerModule for the provided controllers.
* @param controllers - An array of controller classes.
* @param scope - An optional binding scope to be used for all controllers.
* @returns A ContainerModule with the controller bindings.
* @public API
*/
static createContainerModule(controllers, scope) {
const symbols = BaseModule.createSymbols(controllers);
return new inversify_1.ContainerModule((bind) => {
for (const [symbol, target] of symbols) {
if (scope) {
BaseModule.bindToScope(symbol, target, scope, bind);
}
else {
const bindingType = Reflect.getMetadata(exports.BINDING_TYPE_METADATA_KEY, target);
BaseModule.bindToScope(symbol, target, bindingType, bind);
}
}
});
}
}
exports.BaseModule = BaseModule;
const CreateModule = BaseModule.createContainerModule;
exports.CreateModule = CreateModule;
;