Skip to content

Commit

Permalink
update(@nestjs) update to 4.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 23, 2017
1 parent 73ae76d commit ac1303e
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 70 deletions.
62 changes: 43 additions & 19 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
const gulp = require('gulp');
const ts = require('gulp-typescript');
const gulpSequence = require('gulp-sequence')
const gulpSequence = require('gulp-sequence');

const packages = {
common: ts.createProject('src/common/tsconfig.json'),
core: ts.createProject('src/core/tsconfig.json'),
microservices: ts.createProject('src/microservices/tsconfig.json'),
websockets: ts.createProject('src/websockets/tsconfig.json'),
testing: ts.createProject('src/testing/tsconfig.json')
common: ts.createProject('src/common/tsconfig.json'),
core: ts.createProject('src/core/tsconfig.json'),
microservices: ts.createProject('src/microservices/tsconfig.json'),
websockets: ts.createProject('src/websockets/tsconfig.json'),
testing: ts.createProject('src/testing/tsconfig.json'),
};
const modules = Object.keys(packages);
const source = 'src';
const distId = process.argv.indexOf('--dist');
const dist = distId < 0 ? 'node_modules/@nestjs' : process.argv[distId + 1];

gulp.task('default', function () {
modules.forEach((module) => {
gulp.watch([`${source}/${module}/**/*.ts`, `${source}/${module}/*.ts`], [module]);
});
gulp.task('default', function() {
modules.forEach(module => {
gulp.watch(
[`${source}/${module}/**/*.ts`, `${source}/${module}/*.ts`],
[module]
);
});
});

modules.forEach((module) => {
gulp.task(module, () => {
return packages[module].src()
.pipe(packages[module]())
.pipe(gulp.dest(`${dist}/${module}`));
});
modules.forEach(module => {
gulp.task(module, () => {
return packages[module]
.src()
.pipe(packages[module]())
.pipe(gulp.dest(`${dist}/${module}`));
});
});

gulp.task('build', function (cb) {
gulpSequence(modules, cb);
gulp.task('build', function(cb) {
gulpSequence(modules, cb);
});


gulp.task('move', function() {
gulp.src(['node_modules/@nestjs/**/*']).pipe(
gulp.dest('examples/01-cats-app/node_modules/@nestjs')
).pipe(
gulp.dest('examples/02-gateways/node_modules/@nestjs')
).pipe(
gulp.dest('examples/03-microservices/node_modules/@nestjs')
).pipe(
gulp.dest('examples/04-injector/node_modules/@nestjs')
).pipe(
gulp.dest('examples/05-sql-typeorm/node_modules/@nestjs')
).pipe(
gulp.dest('examples/06-mongoose/node_modules/@nestjs')
).pipe(
gulp.dest('examples/07-sequelize/node_modules/@nestjs')
).pipe(
gulp.dest('examples/08-passport/node_modules/@nestjs')
).pipe(
gulp.dest('examples/09-babel-example/node_modules/@nestjs')
);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start:live": "nodemon -e ts --watch src index.js",
"test": "nyc --require ts-node/register mocha src/**/*.spec.ts --reporter spec",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"build": "gulp build",
"build": "gulp build && gulp move",
"build:lib": "gulp build --dist lib",
"prepublish": "npm run build:lib",
"publish": "./node_modules/.bin/lerna publish --skip-git"
Expand Down
2 changes: 1 addition & 1 deletion src/core/adapters/express-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export class ExpressAdapter {
}

public static createRouter(): any {
return express.Router();
return express.Router({ mergeParams: true });
}
}
26 changes: 13 additions & 13 deletions src/core/middlewares/middlewares-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import { ApplicationConfig } from './../application-config';
import { RouterExceptionFilters } from './../router/router-exception-filters';

export class MiddlewaresModule {
private static readonly routesMapper = new RoutesMapper();
private static readonly routerProxy = new RouterProxy();
private static readonly routerMethodFactory = new RouterMethodFactory();
private static routerExceptionFilter: RouterExceptionFilters;
private static resolver: MiddlewaresResolver;
private readonly routesMapper = new RoutesMapper();
private readonly routerProxy = new RouterProxy();
private readonly routerMethodFactory = new RouterMethodFactory();
private routerExceptionFilter: RouterExceptionFilters;
private resolver: MiddlewaresResolver;

public static async setup(
public async setup(
middlewaresContainer: MiddlewaresContainer,
container: NestContainer,
config: ApplicationConfig,
Expand All @@ -38,7 +38,7 @@ export class MiddlewaresModule {
await this.resolveMiddlewares(middlewaresContainer, modules);
}

public static async resolveMiddlewares(
public async resolveMiddlewares(
middlewaresContainer: MiddlewaresContainer,
modules: Map<string, Module>,
) {
Expand All @@ -50,7 +50,7 @@ export class MiddlewaresModule {
}));
}

public static loadConfiguration(
public loadConfiguration(
middlewaresContainer: MiddlewaresContainer,
instance: NestModule,
module: string,
Expand All @@ -66,7 +66,7 @@ export class MiddlewaresModule {
middlewaresContainer.addConfig(config, module);
}

public static async setupMiddlewares(middlewaresContainer: MiddlewaresContainer, app) {
public async setupMiddlewares(middlewaresContainer: MiddlewaresContainer, app) {
const configs = middlewaresContainer.getConfigs();
await Promise.all([...configs.entries()].map(async ([module, moduleConfigs]) => {
await Promise.all([...moduleConfigs].map(async (config: MiddlewareConfiguration) => {
Expand All @@ -75,7 +75,7 @@ export class MiddlewaresModule {
}));
}

public static async setupMiddlewareConfig(
public async setupMiddlewareConfig(
middlewaresContainer: MiddlewaresContainer,
config: MiddlewareConfiguration,
module: string,
Expand All @@ -87,7 +87,7 @@ export class MiddlewaresModule {
}));
}

public static async setupRouteMiddleware(
public async setupRouteMiddleware(
middlewaresContainer: MiddlewaresContainer,
route: ControllerMetadata & { method: RequestMethod },
config: MiddlewareConfiguration,
Expand All @@ -109,7 +109,7 @@ export class MiddlewaresModule {
}));
}

private static async setupHandler(
private async setupHandler(
instance: NestMiddleware,
metatype: Metatype<NestMiddleware>,
app: any,
Expand All @@ -134,7 +134,7 @@ export class MiddlewaresModule {
setupWithProxy(middleware);
}

private static setupHandlerWithProxy(
private setupHandlerWithProxy(
exceptionsHandler: ExceptionsHandler,
router: (...args) => void,
middleware: (req, res, next) => void,
Expand Down
25 changes: 16 additions & 9 deletions src/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ const { NestMicroservice } = optional('@nestjs/microservices/nest-microservice')
const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {} as any;

export class NestApplication implements INestApplication {
private readonly middlewaresContainer = new MiddlewaresContainer();
private readonly logger = new Logger(NestApplication.name, true);
private readonly middlewaresModule = new MiddlewaresModule();
private readonly middlewaresContainer = new MiddlewaresContainer();
private readonly microservicesModule = MicroservicesModule
? new MicroservicesModule()
: null;
private readonly socketModule = SocketModule
? new SocketModule()
: null;

private readonly httpServer: http.Server = null;
private readonly routesResolver: Resolver = null;
private readonly config: ApplicationConfig;
Expand All @@ -59,13 +67,13 @@ export class NestApplication implements INestApplication {
}

public async setupModules() {
SocketModule && SocketModule.setup(this.container, this.config);
this.socketModule && this.socketModule.setup(this.container, this.config);

if (MicroservicesModule) {
MicroservicesModule.setup(this.container, this.config);
MicroservicesModule.setupClients(this.container);
if (this.microservicesModule) {
this.microservicesModule.setup(this.container, this.config);
this.microservicesModule.setupClients(this.container);
}
await MiddlewaresModule.setup(
await this.middlewaresModule.setup(
this.middlewaresContainer,
this.container,
this.config,
Expand Down Expand Up @@ -93,7 +101,6 @@ export class NestApplication implements INestApplication {
if (!NestMicroservice) {
throw new MicroservicesPackageNotFoundException();
}

const instance = new NestMicroservice(this.container as any, config as any);
instance.setupListeners();
instance.setIsInitialized(true);
Expand Down Expand Up @@ -137,7 +144,7 @@ export class NestApplication implements INestApplication {
}

public close() {
SocketModule && SocketModule.close();
this.socketModule && this.socketModule.close();
this.httpServer && this.httpServer.close();
this.microservices.forEach((microservice) => {
microservice.setIsTerminated(true);
Expand Down Expand Up @@ -171,7 +178,7 @@ export class NestApplication implements INestApplication {
}

private async setupMiddlewares(instance) {
await MiddlewaresModule.setupMiddlewares(this.middlewaresContainer, instance);
await this.middlewaresModule.setupMiddlewares(this.middlewaresContainer, instance);
}

private listenToPromise(microservice: INestMicroservice) {
Expand Down
13 changes: 8 additions & 5 deletions src/core/test/middlewares/middlewares-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { ApplicationConfig } from '../../application-config';
import { MiddlewaresContainer } from "../../middlewares/container";

describe('MiddlewaresModule', () => {
let middlewaresModule: MiddlewaresModule;

@Controller('test')
class AnotherRoute { }

Expand All @@ -36,7 +38,8 @@ describe('MiddlewaresModule', () => {
}

beforeEach(() => {
(MiddlewaresModule as any).routerExceptionFilter = new RouterExceptionFilters(
middlewaresModule = new MiddlewaresModule();
(middlewaresModule as any).routerExceptionFilter = new RouterExceptionFilters(
new ApplicationConfig(),
);
});
Expand All @@ -49,7 +52,7 @@ describe('MiddlewaresModule', () => {
configure: configureSpy,
};

MiddlewaresModule.loadConfiguration(new MiddlewaresContainer(), mockModule as any, 'Test' as any);
middlewaresModule.loadConfiguration(new MiddlewaresContainer(), mockModule as any, 'Test' as any);

expect(configureSpy.calledOnce).to.be.true;
expect(configureSpy.calledWith(new MiddlewareBuilder(new RoutesMapper()))).to.be.true;
Expand All @@ -69,7 +72,7 @@ describe('MiddlewaresModule', () => {
const app = { use: useSpy };

expect(
MiddlewaresModule.setupRouteMiddleware(new MiddlewaresContainer(), route as any, configuration, 'Test' as any, app as any),
middlewaresModule.setupRouteMiddleware(new MiddlewaresContainer(), route as any, configuration, 'Test' as any, app as any),
).to.eventually.be.rejectedWith(RuntimeException);
});

Expand Down Expand Up @@ -97,7 +100,7 @@ describe('MiddlewaresModule', () => {
} as any);

expect(
MiddlewaresModule.setupRouteMiddleware(container, route as any, configuration, moduleKey, app as any),
middlewaresModule.setupRouteMiddleware(container, route as any, configuration, moduleKey, app as any),
).to.be.rejectedWith(InvalidMiddlewareException);
});

Expand All @@ -123,7 +126,7 @@ describe('MiddlewaresModule', () => {
instance,
});

MiddlewaresModule.setupRouteMiddleware(container, route, configuration, moduleKey, app as any);
middlewaresModule.setupRouteMiddleware(container, route, configuration, moduleKey, app as any);
expect(useSpy.calledOnce).to.be.true;
});

Expand Down
18 changes: 9 additions & 9 deletions src/microservices/microservices-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import { InterceptorsContextCreator } from '@nestjs/core/interceptors/intercepto
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';

export class MicroservicesModule {
private static readonly clientsContainer = new ClientsContainer();
private static listenersController: ListenersController;
private readonly clientsContainer = new ClientsContainer();
private listenersController: ListenersController;

public static setup(container, config) {
public setup(container, config) {
const contextCreator = new RpcContextCreator(
new RpcProxy(),
new ExceptionFiltersContext(config),
Expand All @@ -31,20 +31,20 @@ export class MicroservicesModule {
new InterceptorsConsumer(),
);
this.listenersController = new ListenersController(
MicroservicesModule.clientsContainer,
this.clientsContainer,
contextCreator,
);
}

public static setupListeners(container, server: Server & CustomTransportStrategy) {
public setupListeners(container, server: Server & CustomTransportStrategy) {
if (!this.listenersController) {
throw new RuntimeException();
}
const modules = container.getModules();
modules.forEach(({ routes }, module) => this.bindListeners(routes, server, module));
}

public static setupClients(container) {
public setupClients(container) {
if (!this.listenersController) {
throw new RuntimeException();
}
Expand All @@ -55,7 +55,7 @@ export class MicroservicesModule {
});
}

public static bindListeners(
public bindListeners(
controllers: Map<string, InstanceWrapper<Controller>>,
server: Server & CustomTransportStrategy,
module: string) {
Expand All @@ -65,13 +65,13 @@ export class MicroservicesModule {
});
}

public static bindClients(controllers: Map<string, InstanceWrapper<Controller>>) {
public bindClients(controllers: Map<string, InstanceWrapper<Controller>>) {
controllers.forEach(({ instance, isNotMetatype }) => {
!isNotMetatype && this.listenersController.bindClientsToProperties(instance);
});
}

public static close() {
public close() {
const clients = this.clientsContainer.getAllClients();
clients.forEach((client) => client.close());
this.clientsContainer.clear();
Expand Down
Loading

0 comments on commit ac1303e

Please sign in to comment.