UNPKG

@malagu/core

Version:
55 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAutowiredProperty = exports.applyAutowiredDecorator = exports.parseAutowiredOption = exports.Autowired = void 0; const inversify_1 = require("inversify"); const container_1 = require("../container"); const utils_1 = require("../utils"); const Autowired = function (idOrOption) { return (target, targetKey, index) => { const option = parseAutowiredOption(target, targetKey, index, idOrOption); applyAutowiredDecorator(option, target, targetKey, index); }; }; exports.Autowired = Autowired; const defaultAutowiredOption = { multi: false, detached: false }; function parseAutowiredOption(target, targetKey, index, idOrOption) { const option = utils_1.AnnotationUtil.getValueOrOption(idOrOption); const type = utils_1.AnnotationUtil.getType(target, targetKey, index); if (type === Array) { option.multi = true; } option.id = option.id || type; return Object.assign(Object.assign({}, defaultAutowiredOption), option); } exports.parseAutowiredOption = parseAutowiredOption; function applyAutowiredDecorator(option, target, targetKey, index, doInject = ({ id, multi }, t, k, i) => multi ? (0, inversify_1.multiInject)(id)(t, k, i) : (0, inversify_1.inject)(id)(t, k, i), doGetValue = ({ id, multi }, t, property) => multi ? container_1.ContainerUtil.getAll(id) : container_1.ContainerUtil.get(id)) { if (option.detached) { if (index !== undefined) { throw new Error(`The ${target.constructor.name} itself is not injected into the container, so the parameter injection of the constructor is not supported.`); } createAutowiredProperty(option, doGetValue, target, targetKey); } else { doInject(option, target, targetKey, index); } return option; } exports.applyAutowiredDecorator = applyAutowiredDecorator; function createAutowiredProperty(option, doGetValue, target, property) { let value; Object.defineProperty(target, property, { enumerable: true, get() { if (value !== undefined) { return value; } value = doGetValue(option, target, property); return value; } }); } exports.createAutowiredProperty = createAutowiredProperty; //# sourceMappingURL=autowired.js.map