Skip to content

Commit

Permalink
bugfix(@nestjs/core) support circular deps in dynamic modules (nestjs…
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed May 20, 2018
1 parent 55600ed commit e37b9f7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions bundle/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"cors": "2.8.4",
"express": "4.16.3",
"iterare": "0.0.8",
"object-hash": "1.3.0",
"optional": "0.1.4"
},
"peerDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"mqtt": "^2.16.0",
"multer": "^1.3.0",
"nats": "^0.8.4",
"object-hash": "^1.3.0",
"opencollective": "^1.0.3",
"optional": "^0.1.4",
"pump": "^3.0.0",
Expand Down
14 changes: 6 additions & 8 deletions packages/core/injector/module-token-factory.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { Type } from '@nestjs/common/interfaces/type.interface';
import { SHARED_MODULE_METADATA } from '@nestjs/common/constants';
import { DynamicModule } from '@nestjs/common';
import { SHARED_MODULE_METADATA } from '@nestjs/common/constants';
import { Type } from '@nestjs/common/interfaces/type.interface';
import * as hash from 'object-hash';

export class ModuleTokenFactory {
public create(
metatype: Type<any>,
scope: Type<any>[],
dynamicModuleMetadata?: Partial<DynamicModule> | undefined,
) {
): string {
const reflectedScope = this.reflectScope(metatype);
const isSingleScoped = reflectedScope === true;
const opaqueToken = {
module: this.getModuleName(metatype),
dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata),
scope: isSingleScoped ? this.getScopeStack(scope) : reflectedScope,
};
return JSON.stringify(opaqueToken);
return hash(opaqueToken);
}

public getDynamicMetadataToken(
Expand Down Expand Up @@ -43,10 +44,7 @@ export class ModuleTokenFactory {
}

private reflectScope(metatype: Type<any>) {
const scope = Reflect.getMetadata(
SHARED_MODULE_METADATA,
metatype,
);
const scope = Reflect.getMetadata(SHARED_MODULE_METADATA, metatype);
return scope ? scope : 'global';
}
}
10 changes: 5 additions & 5 deletions packages/core/test/injector/module-token-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as sinon from 'sinon';
import { expect } from 'chai';
import { ModuleTokenFactory } from '../../injector/module-token-factory';
import * as hash from 'object-hash';
import { SingleScope } from '../../../common';
import { ModuleTokenFactory } from '../../injector/module-token-factory';

describe('ModuleTokenFactory', () => {
let factory: ModuleTokenFactory;
Expand All @@ -14,7 +14,7 @@ describe('ModuleTokenFactory', () => {
const scope = 'global';
const token = factory.create(Module as any, [Module as any], undefined);
expect(token).to.be.deep.eq(
JSON.stringify({
hash({
module: Module.name,
dynamic: '',
scope,
Expand All @@ -28,7 +28,7 @@ describe('ModuleTokenFactory', () => {
undefined,
);
expect(token).to.be.deep.eq(
JSON.stringify({
hash({
module: Module.name,
dynamic: '',
scope: [Module.name],
Expand All @@ -44,7 +44,7 @@ describe('ModuleTokenFactory', () => {
} as any,
);
expect(token).to.be.deep.eq(
JSON.stringify({
hash({
module: Module.name,
dynamic: JSON.stringify({
components: [{}],
Expand Down

0 comments on commit e37b9f7

Please sign in to comment.