fix: Try another api

This commit is contained in:
2024-08-28 15:46:39 +02:00
parent e0ab54d38b
commit b3de04e3f9

View File

@@ -49,7 +49,10 @@ export function Inject<TargetType, DependencyType, PropertyType>(
return function ( return function (
constructor: undefined, constructor: undefined,
context: ClassFieldDecoratorContext<TargetType>, context: ClassFieldDecoratorContext<TargetType>,
): void { ): (
this: TargetType,
initialValue: PropertyType | undefined,
) => PropertyType | undefined {
const _identifier = identifier ?? context.name; const _identifier = identifier ?? context.name;
if (_identifier == null && necessary === true) if (_identifier == null && necessary === true)
@@ -67,9 +70,10 @@ export function Inject<TargetType, DependencyType, PropertyType>(
); );
}; };
context.addInitializer(function (this: TargetType) { return function (
Object.defineProperty(this, context.name, { this: TargetType,
get() { initialValue: PropertyType | undefined,
): PropertyType | undefined {
let instance: DependencyType | PropertyType | undefined; let instance: DependencyType | PropertyType | undefined;
const dependency: DependencyType | undefined = tryAndCatch( const dependency: DependencyType | undefined = tryAndCatch(
@@ -84,8 +88,7 @@ export function Inject<TargetType, DependencyType, PropertyType>(
typeof init === 'function' && dependency != null typeof init === 'function' && dependency != null
? (): PropertyType => init(dependency) ? (): PropertyType => init(dependency)
: init === true && hasConstructor(dependency) : init === true && hasConstructor(dependency)
? (): PropertyType => ? (): PropertyType => new dependency() as PropertyType
new dependency() as PropertyType
: undefined; : undefined;
if (init == null) instance = dependency; if (init == null) instance = dependency;
@@ -112,14 +115,9 @@ export function Inject<TargetType, DependencyType, PropertyType>(
configurable: false, configurable: false,
}); });
return instance; // eslint-disable-next-line @typescript-eslint/no-explicit-any
}, return instance as any;
/** };
* Make the property configurable to allow replacing it
*/
configurable: true,
});
});
}; };
} }