mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
update(@nestjs) update to 4.4.0
This commit is contained in:
62
gulpfile.js
62
gulpfile.js
@@ -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')
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -6,6 +6,6 @@ export class ExpressAdapter {
|
||||
}
|
||||
|
||||
public static createRouter(): any {
|
||||
return express.Router();
|
||||
return express.Router({ mergeParams: true });
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
@@ -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>,
|
||||
) {
|
||||
@@ -50,7 +50,7 @@ export class MiddlewaresModule {
|
||||
}));
|
||||
}
|
||||
|
||||
public static loadConfiguration(
|
||||
public loadConfiguration(
|
||||
middlewaresContainer: MiddlewaresContainer,
|
||||
instance: NestModule,
|
||||
module: string,
|
||||
@@ -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) => {
|
||||
@@ -75,7 +75,7 @@ export class MiddlewaresModule {
|
||||
}));
|
||||
}
|
||||
|
||||
public static async setupMiddlewareConfig(
|
||||
public async setupMiddlewareConfig(
|
||||
middlewaresContainer: MiddlewaresContainer,
|
||||
config: MiddlewareConfiguration,
|
||||
module: string,
|
||||
@@ -87,7 +87,7 @@ export class MiddlewaresModule {
|
||||
}));
|
||||
}
|
||||
|
||||
public static async setupRouteMiddleware(
|
||||
public async setupRouteMiddleware(
|
||||
middlewaresContainer: MiddlewaresContainer,
|
||||
route: ControllerMetadata & { method: RequestMethod },
|
||||
config: MiddlewareConfiguration,
|
||||
@@ -109,7 +109,7 @@ export class MiddlewaresModule {
|
||||
}));
|
||||
}
|
||||
|
||||
private static async setupHandler(
|
||||
private async setupHandler(
|
||||
instance: NestMiddleware,
|
||||
metatype: Metatype<NestMiddleware>,
|
||||
app: any,
|
||||
@@ -134,7 +134,7 @@ export class MiddlewaresModule {
|
||||
setupWithProxy(middleware);
|
||||
}
|
||||
|
||||
private static setupHandlerWithProxy(
|
||||
private setupHandlerWithProxy(
|
||||
exceptionsHandler: ExceptionsHandler,
|
||||
router: (...args) => void,
|
||||
middleware: (req, res, next) => void,
|
||||
|
||||
@@ -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;
|
||||
@@ -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,
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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) {
|
||||
|
||||
@@ -15,6 +15,8 @@ import { ApplicationConfig } from '../../application-config';
|
||||
import { MiddlewaresContainer } from "../../middlewares/container";
|
||||
|
||||
describe('MiddlewaresModule', () => {
|
||||
let middlewaresModule: MiddlewaresModule;
|
||||
|
||||
@Controller('test')
|
||||
class AnotherRoute { }
|
||||
|
||||
@@ -36,7 +38,8 @@ describe('MiddlewaresModule', () => {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
(MiddlewaresModule as any).routerExceptionFilter = new RouterExceptionFilters(
|
||||
middlewaresModule = new MiddlewaresModule();
|
||||
(middlewaresModule as any).routerExceptionFilter = new RouterExceptionFilters(
|
||||
new ApplicationConfig(),
|
||||
);
|
||||
});
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
@@ -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),
|
||||
@@ -31,12 +31,12 @@ 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();
|
||||
}
|
||||
@@ -44,7 +44,7 @@ export class MicroservicesModule {
|
||||
modules.forEach(({ routes }, module) => this.bindListeners(routes, server, module));
|
||||
}
|
||||
|
||||
public static setupClients(container) {
|
||||
public setupClients(container) {
|
||||
if (!this.listenersController) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
@@ -55,7 +55,7 @@ export class MicroservicesModule {
|
||||
});
|
||||
}
|
||||
|
||||
public static bindListeners(
|
||||
public bindListeners(
|
||||
controllers: Map<string, InstanceWrapper<Controller>>,
|
||||
server: Server & CustomTransportStrategy,
|
||||
module: string) {
|
||||
@@ -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();
|
||||
|
||||
@@ -20,6 +20,11 @@ const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {} a
|
||||
|
||||
export class NestMicroservice implements INestMicroservice {
|
||||
private readonly logger = new Logger(NestMicroservice.name, true);
|
||||
private readonly microservicesModule = new MicroservicesModule();
|
||||
private readonly socketModule = SocketModule
|
||||
? new SocketModule()
|
||||
: null;
|
||||
|
||||
private readonly microserviceConfig: MicroserviceConfiguration;
|
||||
private readonly server: Server & CustomTransportStrategy;
|
||||
private readonly config: ApplicationConfig;
|
||||
@@ -34,7 +39,7 @@ export class NestMicroservice implements INestMicroservice {
|
||||
const ioAdapter = IoAdapter ? new IoAdapter() : null;
|
||||
this.config = new ApplicationConfig(ioAdapter);
|
||||
|
||||
MicroservicesModule.setup(container, this.config);
|
||||
this.microservicesModule.setup(container, this.config);
|
||||
this.microserviceConfig = {
|
||||
transport: Transport.TCP,
|
||||
...config,
|
||||
@@ -44,8 +49,8 @@ export class NestMicroservice implements INestMicroservice {
|
||||
}
|
||||
|
||||
public setupModules() {
|
||||
SocketModule && SocketModule.setup(this.container, this.config);
|
||||
MicroservicesModule.setupClients(this.container);
|
||||
this.socketModule && this.socketModule.setup(this.container, this.config);
|
||||
this.microservicesModule.setupClients(this.container);
|
||||
|
||||
this.setupListeners();
|
||||
this.setIsInitialized(true);
|
||||
@@ -54,7 +59,7 @@ export class NestMicroservice implements INestMicroservice {
|
||||
}
|
||||
|
||||
public setupListeners() {
|
||||
MicroservicesModule.setupListeners(this.container, this.server);
|
||||
this.microservicesModule.setupListeners(this.container, this.server);
|
||||
}
|
||||
|
||||
public useWebSocketAdapter(adapter: WebSocketAdapter) {
|
||||
@@ -102,7 +107,7 @@ export class NestMicroservice implements INestMicroservice {
|
||||
}
|
||||
|
||||
private closeApplication() {
|
||||
SocketModule && SocketModule.close();
|
||||
this.socketModule && this.socketModule.close();
|
||||
|
||||
this.callDestroyHook();
|
||||
this.setIsTerminated(true);
|
||||
|
||||
@@ -18,24 +18,23 @@ import { InterceptorsContextCreator } from '@nestjs/core/interceptors/intercepto
|
||||
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';
|
||||
|
||||
export class SocketModule {
|
||||
private static socketsContainer = new SocketsContainer();
|
||||
private static webSocketsController: WebSocketsController;
|
||||
private socketsContainer = new SocketsContainer();
|
||||
private webSocketsController: WebSocketsController;
|
||||
|
||||
public static setup(container, config) {
|
||||
public setup(container, config) {
|
||||
this.webSocketsController = new WebSocketsController(
|
||||
new SocketServerProvider(this.socketsContainer, config), container, config,
|
||||
this.getContextCreator(container),
|
||||
);
|
||||
|
||||
const modules = container.getModules();
|
||||
modules.forEach(({ components }, moduleName) => this.hookGatewaysIntoServers(components, moduleName));
|
||||
}
|
||||
|
||||
public static hookGatewaysIntoServers(components: Map<string, InstanceWrapper<Injectable>>, moduleName: string) {
|
||||
public hookGatewaysIntoServers(components: Map<string, InstanceWrapper<Injectable>>, moduleName: string) {
|
||||
components.forEach((wrapper) => this.hookGatewayIntoServer(wrapper, moduleName));
|
||||
}
|
||||
|
||||
public static hookGatewayIntoServer(wrapper: InstanceWrapper<Injectable>, moduleName: string) {
|
||||
public hookGatewayIntoServer(wrapper: InstanceWrapper<Injectable>, moduleName: string) {
|
||||
const { instance, metatype, isNotMetatype } = wrapper;
|
||||
if (isNotMetatype) {
|
||||
return;
|
||||
@@ -51,13 +50,13 @@ export class SocketModule {
|
||||
);
|
||||
}
|
||||
|
||||
public static close() {
|
||||
public close() {
|
||||
const servers = this.socketsContainer.getAllServers();
|
||||
servers.forEach(({ server }) => server.close());
|
||||
this.socketsContainer.clear();
|
||||
}
|
||||
|
||||
private static getContextCreator(container): WsContextCreator {
|
||||
private getContextCreator(container): WsContextCreator {
|
||||
return new WsContextCreator(
|
||||
new WsProxy(),
|
||||
new ExceptionFiltersContext(),
|
||||
|
||||
Reference in New Issue
Block a user