Skip to content

Commit 1e9b945

Browse files
authored
fix: #18 Each instance should have its dependencies applied correctly (#19)
1 parent a7a1bbe commit 1e9b945

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

src/__test__/aop.module.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Test } from '@nestjs/testing';
55
import { AopModule } from '../aop.module';
66
import { BarModule, BarService, barThisValue } from './fixture/bar';
77
import { FooModule, FooService, fooThisValue } from './fixture/foo';
8+
import { DuplicateService } from './fixture/foo/duplicate.service';
89
import { FooController } from './fixture/foo/foo.controller';
910

1011
describe('AopModule', () => {
@@ -105,4 +106,19 @@ describe('AopModule', () => {
105106
expect(barThisValue).toBe(barService);
106107
});
107108
});
109+
110+
it('Each instance should have its dependencies applied correctly', async () => {
111+
const module = await Test.createTestingModule({
112+
imports: [AopModule, FooModule],
113+
}).compile();
114+
115+
const app = module.createNestApplication(new FastifyAdapter());
116+
await app.init();
117+
118+
const duplicateService1: DuplicateService = app.get('DUPLICATE_1');
119+
const duplicateService2: DuplicateService = app.get('DUPLICATE_2');
120+
121+
expect(duplicateService1.getValue()).toMatchInlineSnapshot(`"1:dependency_value"`);
122+
expect(duplicateService2.getValue()).toMatchInlineSnapshot(`"2:dependency_value"`);
123+
});
108124
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { Foo } from '../foo';
3+
4+
@Injectable()
5+
export class DuplicateService {
6+
constructor(private readonly value: number) {}
7+
8+
@Foo({ options: 'value' })
9+
getValue() {
10+
return this.value;
11+
}
12+
}

src/__test__/fixture/foo/foo.module.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ import { SampleModule } from '../sample';
33
import { FooController } from './foo.controller';
44
import { FooDecorator } from './foo.decorator';
55

6+
import { DuplicateService } from './duplicate.service';
67
import { FooService } from './foo.service';
78

89
@Module({
910
imports: [SampleModule],
1011
controllers: [FooController],
11-
providers: [FooService, FooDecorator],
12-
exports: [FooService],
12+
providers: [
13+
FooService,
14+
{
15+
provide: 'DUPLICATE_1',
16+
useValue: new DuplicateService(1),
17+
},
18+
{
19+
provide: 'DUPLICATE_2',
20+
useValue: new DuplicateService(2),
21+
},
22+
FooDecorator,
23+
],
24+
exports: [FooService, 'DUPLICATE_1', 'DUPLICATE_2'],
1325
})
1426
export class FooModule {}

src/auto-aspect-executor.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ export class AutoAspectExecutor implements OnModuleInit {
6767
method: originalFn.bind(instance),
6868
metadata,
6969
});
70+
7071
Object.setPrototypeOf(wrappedMethod, instance[methodName]);
71-
instance[methodName][aopSymbol] = wrappedMethod;
72+
73+
instance[aopSymbol] ??= {};
74+
instance[aopSymbol][methodName] = wrappedMethod;
7275
}
7376
});
7477
}

src/create-decorator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export const createDecorator = (
2727
const originalFn = descriptor.value;
2828

2929
descriptor.value = function (this: any, ...args: any[]) {
30-
if (this[propertyKey][aopSymbol]) {
30+
if (this[aopSymbol]?.[propertyKey]) {
3131
// If there is a wrapper stored in the method, use it
32-
return this[propertyKey][aopSymbol].apply(this, args);
32+
return this[aopSymbol][propertyKey].apply(this, args);
3333
}
3434
// if there is no wrapper that comes out of method, call originalFn
3535
return originalFn.apply(this, args);

0 commit comments

Comments
 (0)