@nova-ts/context
Version:
A TypeScript library for Dependency Injection
37 lines (35 loc) • 1.31 kB
JavaScript
import {
ApplicationContext
} from "./chunk-WIUZQ3FQ.js";
// src/Decorators/Bean.ts
function Bean(options) {
return function(target, propertyKey, descriptor) {
const lazy = options?.lazy || false;
const method = descriptor.value;
if (!lazy) {
const beanInstance = method.apply(null);
if (!beanInstance || typeof beanInstance !== "object") {
throw new Error(`@Bean: Method '${String(propertyKey)}' did not return an object`);
}
const clazzType = options?.key || beanInstance.constructor.name;
if (ApplicationContext.isBound(clazzType)) {
ApplicationContext.unbind(clazzType);
}
ApplicationContext.bind(clazzType).toConstantValue(beanInstance);
} else {
const returnType = Reflect.getMetadata("design:returntype", target, propertyKey);
const clazzType = options?.key || String(returnType.name);
ApplicationContext.bind(clazzType).toDynamicValue((context) => {
const beanInstance = method.apply(null);
if (!beanInstance || typeof beanInstance !== "object") {
throw new Error(`@Bean: Method '${String(propertyKey)}' did not return an object`);
}
return beanInstance;
}).inSingletonScope();
}
};
}
export {
Bean
};
//# sourceMappingURL=chunk-DDRZXGD6.js.map