@inversifyjs/core
Version:
InversifyJs core package
46 lines • 2.51 kB
JavaScript
import { isPromise } from '@inversifyjs/common';
import { InversifyCoreError } from '../../error/models/InversifyCoreError.js';
import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind.js';
export function resolvePostConstruct(instance, binding, postConstructMethodName) {
const postConstructResult = invokePostConstruct(instance, binding, postConstructMethodName);
if (isPromise(postConstructResult)) {
return postConstructResult.then(() => instance);
}
return instance;
}
function invokePostConstruct(instance, binding, postConstructMethodName) {
if (postConstructMethodName in instance) {
if (typeof instance[postConstructMethodName] === 'function') {
let postConstructResult;
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
postConstructResult = instance[postConstructMethodName]();
}
catch (error) {
throw new InversifyCoreError(InversifyCoreErrorKind.resolution, `Unexpected error found when calling "${postConstructMethodName.toString()}" @postConstruct decorated method on class "${binding.implementationType.name}"`, {
cause: error,
});
}
if (isPromise(postConstructResult)) {
return invokePostConstructAsync(binding, postConstructMethodName, postConstructResult);
}
}
else {
throw new InversifyCoreError(InversifyCoreErrorKind.resolution, `Expecting a "${postConstructMethodName.toString()}" method when resolving "${binding.implementationType.name}" class @postConstruct decorated method, a non function property was found instead.`);
}
}
else {
throw new InversifyCoreError(InversifyCoreErrorKind.resolution, `Expecting a "${postConstructMethodName.toString()}" property when resolving "${binding.implementationType.name}" class @postConstruct decorated method, none found.`);
}
}
async function invokePostConstructAsync(binding, postConstructMethodName, postConstructResult) {
try {
await postConstructResult;
}
catch (error) {
throw new InversifyCoreError(InversifyCoreErrorKind.resolution, `Unexpected error found when calling "${postConstructMethodName.toString()}" @postConstruct decorated method on class "${binding.implementationType.name}"`, {
cause: error,
});
}
}
//# sourceMappingURL=resolvePostConstruct.js.map