UNPKG

@malagu/core

Version:
58 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bindValue = exports.createValueProperty = exports.applyValueDecorator = exports.parseValueOption = exports.Value = exports.VALUE = void 0; const inversify_1 = require("inversify"); const config_util_1 = require("../config/config-util"); const utils_1 = require("../utils"); const config_protocol_1 = require("../config/config-protocol"); exports.VALUE = Symbol('Value'); const Value = function (elOrOption) { return (target, targetKey, index) => { const option = parseValueOption(target, targetKey, index, elOrOption); applyValueDecorator(option, target, targetKey, index); }; }; exports.Value = Value; const defaultValueOption = { detached: false }; function parseValueOption(target, targetKey, index, elOrOption) { const option = utils_1.AnnotationUtil.getValueOrOption(elOrOption, 'el'); option.el = option.el || targetKey; return Object.assign(Object.assign({}, defaultValueOption), option); } exports.parseValueOption = parseValueOption; function applyValueDecorator(option, target, targetKey, index) { 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.`); } createValueProperty(option, target, targetKey); return; } const el = option.el; (0, inversify_1.inject)(exports.VALUE)(target, targetKey, index); (0, inversify_1.named)(el)(target, targetKey, index); return option; } exports.applyValueDecorator = applyValueDecorator; function createValueProperty(option, target, property) { Object.defineProperty(target, property, { enumerable: true, get() { const el = option.el; return config_util_1.ConfigUtil.get(el); } }); } exports.createValueProperty = createValueProperty; function bindValue(bind) { bind(exports.VALUE).toDynamicValue(ctx => { const namedMetadata = ctx.currentRequest.target.getNamedTag(); const el = namedMetadata.value.toString(); const configProvider = ctx.container.get(config_protocol_1.ConfigProvider); return configProvider.get(el); }); } exports.bindValue = bindValue; //# sourceMappingURL=value.js.map