diff --git a/.changeset/wild-clouds-complain.md b/.changeset/wild-clouds-complain.md new file mode 100644 index 00000000..dbcb7d84 --- /dev/null +++ b/.changeset/wild-clouds-complain.md @@ -0,0 +1,5 @@ +--- +"@inversifyjs/core": patch +--- + +Updated `getClassMetadata` to correctly fetch name and target names diff --git a/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.spec.ts b/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.spec.ts index 44e3005c..156fab2b 100644 --- a/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.spec.ts +++ b/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.spec.ts @@ -2,6 +2,8 @@ import { beforeAll, describe, expect, it } from '@jest/globals'; import { ServiceIdentifier } from '@inversifyjs/common'; +import { InversifyCoreError } from '../../error/models/InversifyCoreError'; +import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind'; import { INJECT_TAG, MULTI_INJECT_TAG, @@ -38,11 +40,12 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => { }); it('should throw an Error', () => { - const expectedErrorProperties: Partial = { + const expectedErrorProperties: Partial = { + kind: InversifyCoreErrorKind.missingInjectionDecorator, message: 'Expected @inject, @multiInject or @unmanaged metadata', }; - expect(result).toBeInstanceOf(Error); + expect(result).toBeInstanceOf(InversifyCoreError); expect(result).toStrictEqual( expect.objectContaining(expectedErrorProperties), ); @@ -121,12 +124,13 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => { }); it('should throw an Error', () => { - const expectedErrorProperties: Partial = { + const expectedErrorProperties: Partial = { + kind: InversifyCoreErrorKind.missingInjectionDecorator, message: 'Expected a single @inject, @multiInject or @unmanaged metadata', }; - expect(result).toBeInstanceOf(Error); + expect(result).toBeInstanceOf(InversifyCoreError); expect(result).toStrictEqual( expect.objectContaining(expectedErrorProperties), ); @@ -225,7 +229,7 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => { value: 'customTagValue', }; nameMetadataFixture = { - key: NAME_TAG, + key: NAMED_TAG, value: 'name-fixture', }; optionalMetadataFixture = { @@ -233,7 +237,7 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => { value: true, }; targetNameMetadataFixture = { - key: NAMED_TAG, + key: NAME_TAG, value: 'target-name-fixture', }; metadataListFixture = [ diff --git a/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.ts b/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.ts index 23de23c6..f38cc7f1 100644 --- a/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.ts +++ b/packages/container/libraries/core/src/metadata/calculations/getClassElementMetadataFromLegacyMetadata.ts @@ -1,5 +1,7 @@ import { LazyServiceIdentifier, ServiceIdentifier } from '@inversifyjs/common'; +import { InversifyCoreError } from '../../error/models/InversifyCoreError'; +import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind'; import { INJECT_TAG, MULTI_INJECT_TAG, @@ -39,11 +41,14 @@ export function getClassElementMetadataFromLegacyMetadata( } if (multiInjectMetadata === undefined && injectMetadata === undefined) { - throw new Error('Expected @inject, @multiInject or @unmanaged metadata'); + throw new InversifyCoreError( + InversifyCoreErrorKind.missingInjectionDecorator, + 'Expected @inject, @multiInject or @unmanaged metadata', + ); } const nameMetadata: LegacyMetadata | undefined = metadataList.find( - (metadata: LegacyMetadata): boolean => metadata.key === NAME_TAG, + (metadata: LegacyMetadata): boolean => metadata.key === NAMED_TAG, ); const optionalMetadata: LegacyMetadata | undefined = metadataList.find( @@ -51,7 +56,7 @@ export function getClassElementMetadataFromLegacyMetadata( ); const targetNameMetadata: LegacyMetadata | undefined = metadataList.find( - (metadata: LegacyMetadata): boolean => metadata.key === NAMED_TAG, + (metadata: LegacyMetadata): boolean => metadata.key === NAME_TAG, ); const managedClassElementMetadata: ManagedClassElementMetadata = { @@ -90,7 +95,8 @@ function getUnmanagedClassElementMetadata( multiInjectMetadata: LegacyMetadata | undefined, ): UnmanagedClassElementMetadata { if (multiInjectMetadata !== undefined || injectMetadata !== undefined) { - throw new Error( + throw new InversifyCoreError( + InversifyCoreErrorKind.missingInjectionDecorator, 'Expected a single @inject, @multiInject or @unmanaged metadata', ); }