ioc-service-container
Version:
Lightweight ioc service container
30 lines (29 loc) • 1.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.inject = void 0;
const ServiceContainer_1 = __importDefault(require("./ServiceContainer"));
function inject(serviceIdOrTarget, propertyKey) {
if (typeof serviceIdOrTarget === 'string') {
return function (target, propertyKey) {
redefineObject(target, propertyKey, serviceIdOrTarget);
};
}
redefineObject(serviceIdOrTarget, propertyKey);
}
exports.inject = inject;
function redefineObject(target, propertyKey, serviceId) {
function get() {
var _a;
return ServiceContainer_1.default.get((_a = serviceId === null || serviceId === void 0 ? void 0 : serviceId.toLowerCase()) !== null && _a !== void 0 ? _a : propertyKey.toLowerCase());
}
function set() {
throw new Error(`Injected property [${propertyKey}] can't be reset`);
}
Object.defineProperty(target, propertyKey, {
get,
set,
});
}