Skip to content

Commit cd3470e

Browse files
Merge pull request nestjs#33 from kamilmysliwiec/fix-30/injector
Issue nestjs#20 / Fix inject by custom value
2 parents 1c48c8a + c394385 commit cd3470e

4 files changed

Lines changed: 42 additions & 48 deletions

File tree

example/modules/users/users.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { ChatGateway } from './chat.gateway';
77

88
@Module({
99
controllers: [ UsersController ],
10-
components: [ UsersService, ChatGateway ],
10+
components: [
11+
{ provide: 'UsersService', useClass: UsersService },
12+
ChatGateway,
13+
],
1114
})
1215
export class UsersModule implements NestModule {
1316
public configure(consumer: MiddlewaresConsumer) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nest.js",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Modern, fast, powerful node.js web framework",
55
"main": "index.js",
66
"scripts": {

src/core/injector/module.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { RuntimeException } from '../../errors/exceptions/runtime.exception';
99

1010
export interface CustomComponent {
1111
provide: any;
12+
name: string;
1213
}
1314
export type OpaqueToken = string | symbol | object | Metatype<any>;
1415
export type CustomClass = CustomComponent & { useClass: Metatype<any> };
@@ -91,10 +92,17 @@ export class Module {
9192
return !isNil((component as CustomComponent).provide);
9293
}
9394

94-
public addCustomComponent(component: ComponentMetatype) {
95-
if (this.isCustomClass(component)) this.addCustomClass(component);
96-
else if (this.isCustomValue(component)) this.addCustomValue(component);
97-
else if (this.isCustomFactory(component)) this.addCustomFactory(component);
95+
public addCustomComponent(component: CustomFactory | CustomValue | CustomClass) {
96+
const { provide } = component;
97+
const name = isFunction(provide) ? provide.name : provide;
98+
const comp = {
99+
...component,
100+
name,
101+
};
102+
103+
if (this.isCustomClass(comp)) this.addCustomClass(comp);
104+
else if (this.isCustomValue(comp)) this.addCustomValue(comp);
105+
else if (this.isCustomFactory(comp)) this.addCustomFactory(comp);
98106
}
99107

100108
public isCustomClass(component): component is CustomClass {
@@ -110,19 +118,17 @@ export class Module {
110118
}
111119

112120
public addCustomClass(component: CustomClass) {
113-
const { provide: metatype, useClass } = component;
114-
this._components.set(metatype.name, {
115-
name: metatype.name,
121+
const { provide, name, useClass } = component;
122+
this._components.set(name, {
123+
name,
116124
metatype: useClass,
117125
instance: null,
118126
isResolved: false,
119127
});
120128
}
121129

122130
public addCustomValue(component: CustomValue) {
123-
const { provide, useValue: value } = component;
124-
const name = isFunction(provide) ? provide.name : provide;
125-
131+
const { provide, name, useValue: value } = component;
126132
this._components.set(name, {
127133
name,
128134
metatype: null,
@@ -133,7 +139,7 @@ export class Module {
133139
}
134140

135141
public addCustomFactory(component: CustomFactory){
136-
const { provide: name, useFactory: factory, inject } = component;
142+
const { provide, name, useFactory: factory, inject } = component;
137143
this._components.set(name, {
138144
name,
139145
metatype: factory as any,

src/core/test/injector/module.spec.ts

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('Module', () => {
9393

9494
describe('addCustomClass', () => {
9595
const type = { name: 'TypeTest' };
96-
const component = { provide: type, useClass: type };
96+
const component = { provide: type, useClass: type, name: 'test' };
9797
let setSpy;
9898
beforeEach(() => {
9999
const collection = new Map();
@@ -102,8 +102,8 @@ describe('Module', () => {
102102
});
103103
it('should store component', () => {
104104
module.addCustomClass(component as any);
105-
expect(setSpy.calledWith(type.name, {
106-
name: type.name,
105+
expect(setSpy.calledWith(component.name, {
106+
name: component.name,
107107
metatype: type,
108108
instance: null,
109109
isResolved: false,
@@ -113,47 +113,32 @@ describe('Module', () => {
113113

114114
describe('addCustomValue', () => {
115115
let setSpy;
116+
const value = () => ({});
117+
const name = 'test';
118+
const component = { provide: value, name, useValue: value };
119+
116120
beforeEach(() => {
117121
const collection = new Map();
118122
setSpy = sinon.spy(collection, 'set');
119123
(module as any)._components = collection;
120124
});
121-
describe('when value is a function', () => {
122-
const value = () => ({});
123-
const component = { provide: value, useValue: value };
124-
125-
it('should store component', () => {
126-
module.addCustomValue(component as any);
127-
expect(setSpy.calledWith(value.name, {
128-
name: value.name,
129-
metatype: null,
130-
instance: value,
131-
isResolved: true,
132-
isNotMetatype: true,
133-
})).to.be.true;
134-
});
135-
});
136-
describe('when value is not a function', () => {
137-
const value = 'Test';
138-
const component = { provide: value, useValue: value };
139-
140-
it('should store component', () => {
141-
module.addCustomValue(component as any);
142-
expect(setSpy.calledWith(value, {
143-
name: value,
144-
metatype: null,
145-
instance: value,
146-
isResolved: true,
147-
isNotMetatype: true,
148-
})).to.be.true;
149-
});
125+
126+
it('should store component', () => {
127+
module.addCustomValue(component as any);
128+
expect(setSpy.calledWith(name, {
129+
name,
130+
metatype: null,
131+
instance: value,
132+
isResolved: true,
133+
isNotMetatype: true,
134+
})).to.be.true;
150135
});
151136
});
152137

153138
describe('addCustomFactory', () => {
154139
const type = { name: 'TypeTest' };
155140
const inject = [1, 2, 3];
156-
const component = { provide: type, useFactory: type, inject };
141+
const component = { provide: type, useFactory: type, name: 'test', inject };
157142

158143
let setSpy;
159144
beforeEach(() => {
@@ -163,8 +148,8 @@ describe('Module', () => {
163148
});
164149
it('should store component', () => {
165150
module.addCustomFactory(component as any);
166-
expect(setSpy.getCall(0).args).to.deep.equal([type, {
167-
name: type,
151+
expect(setSpy.getCall(0).args).to.deep.equal([component.name, {
152+
name: component.name,
168153
metatype: type,
169154
instance: null,
170155
isResolved: false,

0 commit comments

Comments
 (0)