Skip to content

Commit

Permalink
feat(all packages): upgrade to Nest v6
Browse files Browse the repository at this point in the history
BREAKING CHANGE: upgrades underlying nest package dependencies to v6 versions
  • Loading branch information
WonderPanda committed Mar 25, 2019
1 parent 280dec8 commit 5a8e903
Show file tree
Hide file tree
Showing 14 changed files with 1,376 additions and 1,069 deletions.
16 changes: 8 additions & 8 deletions package.json
Expand Up @@ -6,8 +6,8 @@
"packages/*"
],
"dependencies": {
"@nestjs/common": "^5.7.0",
"@nestjs/core": "^5.7.0",
"@nestjs/common": "^6.0.1",
"@nestjs/core": "^6.0.1",
"lodash": "^4.17.11",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.4.0"
Expand All @@ -16,21 +16,21 @@
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@commitlint/prompt": "^7.5.0",
"@nestjs/testing": "^5.7.0",
"@nestjs/testing": "^6.0.1",
"@types/express": "^4.16.1",
"@types/jest": "^24.0.0",
"@types/jest": "^24.0.11",
"@types/lodash": "^4.14.120",
"@types/node": "^10.12.18",
"commitizen": "^3.0.7",
"cz-conventional-changelog": "^2.1.0",
"husky": "^1.3.1",
"jest": "^24.1.0",
"lerna": "^3.10.2",
"jest": "^24.5.0",
"lerna": "^3.13.1",
"lint-staged": "^8.1.4",
"prettier": "^1.16.4",
"rimraf": "^2.6.3",
"ts-jest": "^23.10.5",
"typescript": "^3.2.4"
"ts-jest": "^24.0.0",
"typescript": "^3.3.3333"
},
"scripts": {
"commit": "yarn run git-cz",
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Expand Up @@ -46,7 +46,7 @@
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
"^.+\\.ts$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/README.md
Expand Up @@ -104,7 +104,7 @@ export interface DiscoveredClassWithMeta<T> {
Assuming you were using a custom decorator in your application that attached metadata at a key called `exampleKey`:

```typescript
const ExampleDecorator = (meta: string) => ReflectMetadata('exampleKey', meta);
const ExampleDecorator = (meta: string) => SetMetadata('exampleKey', meta);
```

Find all controller methods that have been decorated with `@ExampleDecorator` and retrieve the value they set for meta:
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/package.json
Expand Up @@ -42,7 +42,7 @@
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
"^.+\\.ts$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
Expand Down
42 changes: 23 additions & 19 deletions packages/discovery/src/discovery.service.ts
@@ -1,10 +1,11 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Scope, Type } from '@nestjs/common';
import { PATH_METADATA } from '@nestjs/common/constants';
import { InstanceWrapper } from '@nestjs/core/injector/container';
import { STATIC_CONTEXT } from '@nestjs/core/injector/constants';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { Module } from '@nestjs/core/injector/module';
import { ModulesContainer } from '@nestjs/core/injector/modules-container';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { flatMap, some, uniqBy } from 'lodash';
import { flatMap, get, some, uniqBy } from 'lodash';
import {
DiscoveredClass,
DiscoveredClassWithMeta,
Expand Down Expand Up @@ -65,18 +66,18 @@ export class DiscoveryService {
this.discoveredControllers = Promise.all(
flatMap(modulesMap, ([key, nestModule]) => {
const components = [...nestModule.routes.values()];
return components.map(component =>
this.toDiscoveredClass(nestModule, component)
);
return components
.filter(component => component.scope !== Scope.REQUEST)
.map(component => this.toDiscoveredClass(nestModule, component));
})
);

this.discoveredProviders = Promise.all(
flatMap(modulesMap, ([key, nestModule]) => {
const components = [...nestModule.components.values()];
return components.map(component =>
this.toDiscoveredClass(nestModule, component)
);
return components
.filter(component => component.scope !== Scope.REQUEST)
.map(component => this.toDiscoveredClass(nestModule, component));
})
);
}
Expand Down Expand Up @@ -217,24 +218,27 @@ export class DiscoveryService {

private async toDiscoveredClass(
nestModule: Module,
component: InstanceWrapper<any>
wrapper: InstanceWrapper<any>
): Promise<DiscoveredClass> {
// This may be a bug in NestJS core as it doesn't seem that isPending is properly
// updated once the component is resolved
if (component.isPending && !component.isResolved) {
await component.done$;
const instanceHost = wrapper.getInstanceByContextId(
STATIC_CONTEXT,
wrapper && wrapper.id ? wrapper.id : undefined
);

if (instanceHost.isPending && !instanceHost.isResolved) {
await instanceHost.donePromise;
}

return {
name: component.name as string,
instance: component.instance,
injectType: component.metatype,
dependencyType: component.instance.constructor,
name: wrapper.name as string,
instance: instanceHost.instance,
injectType: wrapper.metatype,
dependencyType: get(instanceHost, 'instance.constructor'),
parentModule: {
name: nestModule.metatype.name,
instance: nestModule.instance,
injectType: nestModule.metatype,
dependencyType: component.instance.constructor
dependencyType: nestModule.instance.constructor as Type<{}>
}
};
}
Expand Down
Expand Up @@ -4,16 +4,16 @@ import {
Module,
Post,
Put,
ReflectMetadata,
RequestMethod
RequestMethod,
SetMetadata
} from '@nestjs/common';
import { METHOD_METADATA, PATH_METADATA } from '@nestjs/common/constants';
import { Test, TestingModule } from '@nestjs/testing';
import { DiscoveryModule, DiscoveryService } from '..';
import { getComponentMetaAtKey } from '../discovery.service';

const rolesKey = 'roles';
const Roles = (roles: string[]) => ReflectMetadata(rolesKey, roles);
const Roles = (roles: string[]) => SetMetadata(rolesKey, roles);

@Controller('guest')
@Roles(['guest', 'anotherRole'])
Expand Down
20 changes: 15 additions & 5 deletions packages/discovery/src/tests/discovery.spec.ts
Expand Up @@ -3,7 +3,7 @@ import {
Get,
Injectable,
Module,
ReflectMetadata
SetMetadata
} from '@nestjs/common';
import { PATH_METADATA } from '@nestjs/common/constants';
import { Test, TestingModule } from '@nestjs/testing';
Expand All @@ -16,10 +16,10 @@ const ExampleClassSymbol = Symbol('ExampleClassSymbol');
const ExampleMethodSymbol = Symbol('ExampleMethodSymbol');

const ExampleClassDecorator = (config: any) =>
ReflectMetadata(ExampleClassSymbol, config);
SetMetadata(ExampleClassSymbol, config);

const ExampleMethodDecorator = (config: any) =>
ReflectMetadata(ExampleMethodSymbol, config);
SetMetadata(ExampleMethodSymbol, config);

@Injectable()
@ExampleClassDecorator('class')
Expand Down Expand Up @@ -86,7 +86,12 @@ describe('Discovery', () => {
methodName: 'specialMethod',
parentClass: {
injectType: ExampleService,
dependencyType: ExampleService
dependencyType: ExampleService,
parentModule: {
name: 'ExampleModule',
dependencyType: ExampleModule,
injectType: ExampleModule
}
}
}
});
Expand Down Expand Up @@ -125,7 +130,12 @@ describe('Discovery', () => {
methodName: 'get',
parentClass: {
injectType: ExampleController,
dependencyType: ExampleController
dependencyType: ExampleController,
parentModule: {
name: 'ExampleModule',
dependencyType: ExampleModule,
injectType: ExampleModule
}
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions packages/discovery/src/tests/provider-types.spec.ts
@@ -1,8 +1,8 @@
import { Injectable, Module, ReflectMetadata } from '@nestjs/common';
import { Injectable, Module, SetMetadata } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { DiscoveryModule, DiscoveryService, withMetaAtKey } from '..';

const TestDecorator = (config: any) => ReflectMetadata('test', config);
const TestDecorator = (config: any) => SetMetadata('test', config);

@Injectable()
@TestDecorator('dynamicProvider')
Expand All @@ -29,7 +29,7 @@ class DynamicProvider {
class ExampleModule {}

describe('Provider Types', () => {
describe('Value Providers', async () => {
describe('Value Providers', () => {
let app: TestingModule;
let discover: DiscoveryService;

Expand Down
2 changes: 1 addition & 1 deletion packages/rabbitmq/package.json
Expand Up @@ -52,7 +52,7 @@
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
"^.+\\.ts$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
Expand Down
10 changes: 3 additions & 7 deletions packages/rabbitmq/src/rabbitmq.decorators.ts
@@ -1,4 +1,4 @@
import { ReflectMetadata } from '@nestjs/common';
import { SetMetadata } from '@nestjs/common';
import { RABBIT_HANDLER } from './rabbitmq.constants';
import { RabbitHandlerConfig } from './rabbitmq.interfaces';

Expand All @@ -7,17 +7,13 @@ export const makeRabbitDecorator = <T extends Partial<RabbitHandlerConfig>>(
) => (
config: Pick<RabbitHandlerConfig, Exclude<keyof RabbitHandlerConfig, keyof T>>
) => (target, key, descriptor) =>
ReflectMetadata(RABBIT_HANDLER, { ...input, ...config })(
target,
key,
descriptor
);
SetMetadata(RABBIT_HANDLER, { ...input, ...config })(target, key, descriptor);

export const RabbitHandler = (config: RabbitHandlerConfig) => (
target,
key,
descriptor
) => ReflectMetadata(RABBIT_HANDLER, config)(target, key, descriptor);
) => SetMetadata(RABBIT_HANDLER, config)(target, key, descriptor);

export const RabbitSubscribe = makeRabbitDecorator({ type: 'subscribe' });

Expand Down
13 changes: 13 additions & 0 deletions packages/rabbitmq/src/rabbitmq.module.ts
Expand Up @@ -35,6 +35,19 @@ export class RabbitMQModule implements OnModuleInit {
};
}

public static attach(connection: AmqpConnection): DynamicModule {
return {
module: RabbitMQModule,
providers: [
{
provide: AmqpConnection,
useValue: connection
}
],
exports: [AmqpConnection]
};
}

public async onModuleInit() {
const rabbitMeta = await this.discover.providerMethodsWithMetaAtKey<
RabbitHandlerConfig
Expand Down
24 changes: 13 additions & 11 deletions packages/rabbitmq/src/rabbitmq.spec.ts
Expand Up @@ -4,6 +4,8 @@ import { AmqpConnection } from './amqp/AmqpConnection';
import { RabbitRPC, RabbitSubscribe } from './rabbitmq.decorators';
import { RabbitMQModule } from './rabbitmq.module';

jest.mock('./amqp/AmqpConnection');

@Injectable()
class ExampleService {
@RabbitRPC({
Expand All @@ -29,30 +31,30 @@ describe('RabbitMQ', () => {
let amqpMock: AmqpConnection;

beforeEach(async () => {
amqpMock = new AmqpConnection({
uri: '',
exchanges: []
});

app = await Test.createTestingModule({
imports: [RabbitMQModule, ExampleModule]
})
.overrideProvider(AmqpConnection)
.useValue({
createRpc: jest.fn(),
createSubscriber: jest.fn()
})
.compile();
imports: [ExampleModule, RabbitMQModule.attach(amqpMock)]
}).compile();

await app.init();
amqpMock = app.get<AmqpConnection>(AmqpConnection);
});

it('should register rabbit rpc handlers', async () => {
expect(amqpMock.createRpc).toBeCalledTimes(1);

expect(amqpMock.createRpc).toBeCalledWith(expect.any(Function), {
exchange: 'exchange1',
routingKey: 'rpc'
});

expect(amqpMock.createRpc).toBeCalledTimes(1);
});

it('should register rabbit subscribe handlers', async () => {
expect(amqpMock.createSubscriber).toBeCalledTimes(1);

expect(amqpMock.createSubscriber).toBeCalledWith(expect.any(Function), {
exchange: 'exchange2',
routingKey: 'subscribe'
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -3,14 +3,15 @@
"composite": true,
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"noImplicitAny": false,
"removeComments": false,
"strict": true,
"strictNullChecks": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"target": "es2017",
"sourceMap": true,
"lib": ["es7"],
"baseUrl": "./src",
Expand Down

0 comments on commit 5a8e903

Please sign in to comment.