@ayanaware/bento
Version:
Modular runtime framework designed to solve complex tasks
35 lines • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Inject = exports.getInjections = void 0;
const errors_1 = require("@ayanaware/errors");
const INJECT_KEY = '@ayanaware/bento:Inject';
// eslint-disable-next-line @typescript-eslint/ban-types
function getInjections(target) {
const injections = Reflect.getMetadata(INJECT_KEY, target);
if (!Array.isArray(injections))
return [];
return injections;
}
exports.getInjections = getInjections;
function Inject(reference) {
return (target, propertyKey, parameterIndex) => {
if (typeof parameterIndex === 'number' && propertyKey != null) {
throw new errors_1.IllegalAccessError('Inject(): cannot be used on method parameters outside of constructor');
}
// If no reference, attempt infer from Typescript
if (!reference && propertyKey) {
reference = Reflect.getMetadata('design:type', target, propertyKey);
// TODO: verify valid
}
if (!reference)
throw new errors_1.IllegalArgumentError('Inject(): Reference not provided and cannot be inferred from Typescript');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
if (target.prototype === undefined)
target = target.constructor;
const injections = Reflect.getMetadata(INJECT_KEY, target) || [];
injections.push({ key: propertyKey, reference });
Reflect.defineMetadata(INJECT_KEY, injections, target);
};
}
exports.Inject = Inject;
//# sourceMappingURL=Inject.js.map