Skip to content

Commit 55f4050

Browse files
author
kamil.mysliwiec
committed
Issue nestjs#59 - hierarchical injector improvements
1 parent d4fecd1 commit 55f4050

23 files changed

Lines changed: 318 additions & 100 deletions

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- Hierarchical injector. Injector improvements,
2+
- @Shared() decorator for scoped Modules,
3+
14
## 1.0.0 (Final - 01.05.2017)
25

36
- Added **Gateway Middlewares** support:

example/modules/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Module } from './../../src/';
22
import { UsersModule } from './users/users.module';
33
import { ClientController } from './client/client.controller';
4+
import { Component } from '../../src/common/index';
5+
import { UsersService } from './users/users.service';
46

57
@Module({
68
modules: [ UsersModule ],

example/modules/shared/shared.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Module } from '../../../src/common/utils/decorators/module.decorator';
22
import { ChatGateway } from '../users/chat.gateway';
3+
import { Shared } from '../../../src/index';
34

5+
@Shared()
46
@Module({
57
components: [ ChatGateway ],
68
exports: [ ChatGateway ],

example/modules/users/users.controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { CustomExceptionFilter } from './exception.filter';
99
@Controller('users')
1010
@ExceptionFilters(CustomExceptionFilter)
1111
export class UsersController {
12-
constructor(
13-
private usersService: UsersService) {}
12+
constructor(private usersService: UsersService) {}
1413

1514
@Get()
1615
public async getAllUsers(@Response() res: express.Response) {

example/modules/users/users.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module } from './../../../src/';
1+
import { Module, Shared } from './../../../src/';
22
import { UsersController } from './users.controller';
33
import { UsersService } from './users.service';
44
import { AuthMiddleware } from './auth.middleware';

src/common/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export const metadata = {
55
EXPORTS: 'exports',
66
};
77

8+
export const SHARED_MODULE_METADATA = '__sharedModule__';
9+
810
export const PATH_METADATA = 'path';
911
export const PARAMTYPES_METADATA = 'design:paramtypes';
1012
export const SELF_DECLARED_DEPS_METADATA = 'self:paramtypes';

src/common/interfaces/module-metadata.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NestModule } from './nest-module.interface';
22
import { Controller } from './controller.interface';
33

44
export interface ModuleMetadata {
5-
modules?: NestModule[];
5+
modules?: NestModule[] | any[];
66
components?: any[];
77
controllers?: Controller[] | any[];
88
exports?: any[];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'reflect-metadata';
2+
import { expect } from 'chai';
3+
import { Shared } from '../../utils/decorators/shared.decorator';
4+
import { SHARED_MODULE_METADATA } from '../../constants';
5+
6+
describe('Shared', () => {
7+
let type;
8+
const token = '_';
9+
class Test {}
10+
11+
beforeEach(() => {
12+
type = Shared(token)(Test);
13+
});
14+
it('should enrich metatype with SharedModule token', () => {
15+
const opaqueToken = Reflect.getMetadata(SHARED_MODULE_METADATA, type);
16+
expect(opaqueToken).to.be.equal(token);
17+
});
18+
it('should set name of the metatype', () => {
19+
expect(type.name).to.eq(Test.name);
20+
});
21+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'reflect-metadata';
2+
import { ControllerMetadata } from '../../interfaces/controller-metadata.interface';
3+
import { isString } from '../shared.utils';
4+
import { PATH_METADATA, SHARED_MODULE_METADATA } from '../../constants';
5+
import { NestModuleMetatype } from '../../interfaces/module-metatype.interface';
6+
7+
export const Shared = (token: string = 'global'): ClassDecorator => {
8+
return (target: FunctionConstructor) => {
9+
const Type = class extends target {
10+
constructor(...args) {
11+
super(...args);
12+
}
13+
};
14+
Reflect.defineMetadata(SHARED_MODULE_METADATA, token, Type);
15+
Object.defineProperty(Type, 'name', { value: target.name });
16+
return Type;
17+
};
18+
};

src/common/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export * from './decorators/inject.decorator';
77
export { Component as Middleware } from './decorators/component.decorator';
88
export * from './decorators/route-params.decorator';
99
export * from './decorators/catch.decorator';
10-
export * from './decorators/exception-filters.decorator';
10+
export * from './decorators/exception-filters.decorator';
11+
export * from './decorators/shared.decorator';

0 commit comments

Comments
 (0)