Skip to content

Commit

Permalink
fix(core): update getClassElementMetadataFromLegacyMetadata
Browse files Browse the repository at this point in the history
update getClassElementMetadataFromLegacyMetadata to fetch right name and target name
  • Loading branch information
notaphplover committed Nov 6, 2024
1 parent 20f58ea commit 6469c67
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-clouds-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": patch
---

Updated `getClassMetadata` to correctly fetch name and target names
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -38,11 +40,12 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => {
});

it('should throw an Error', () => {
const expectedErrorProperties: Partial<Error> = {
const expectedErrorProperties: Partial<InversifyCoreError> = {
kind: InversifyCoreErrorKind.missingInjectionDecorator,
message: 'Expected @inject, @multiInject or @unmanaged metadata',
};

expect(result).toBeInstanceOf(Error);
expect(result).toBeInstanceOf(InversifyCoreError);
expect(result).toStrictEqual(
expect.objectContaining(expectedErrorProperties),
);
Expand Down Expand Up @@ -121,12 +124,13 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => {
});

it('should throw an Error', () => {
const expectedErrorProperties: Partial<Error> = {
const expectedErrorProperties: Partial<InversifyCoreError> = {
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),
);
Expand Down Expand Up @@ -225,15 +229,15 @@ describe(getClassElementMetadataFromLegacyMetadata.name, () => {
value: 'customTagValue',
};
nameMetadataFixture = {
key: NAME_TAG,
key: NAMED_TAG,
value: 'name-fixture',
};
optionalMetadataFixture = {
key: OPTIONAL_TAG,
value: true,
};
targetNameMetadataFixture = {
key: NAMED_TAG,
key: NAME_TAG,
value: 'target-name-fixture',
};
metadataListFixture = [
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -39,19 +41,22 @@ 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(
(metadata: LegacyMetadata): boolean => metadata.key === OPTIONAL_TAG,
);

const targetNameMetadata: LegacyMetadata | undefined = metadataList.find(
(metadata: LegacyMetadata): boolean => metadata.key === NAMED_TAG,
(metadata: LegacyMetadata): boolean => metadata.key === NAME_TAG,
);

const managedClassElementMetadata: ManagedClassElementMetadata = {
Expand Down Expand Up @@ -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',
);
}
Expand Down

0 comments on commit 6469c67

Please sign in to comment.