mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Packages codependency refactor
This commit is contained in:
@@ -55,6 +55,7 @@
|
||||
"iterare": "0.0.8",
|
||||
"json-socket": "^0.2.1",
|
||||
"opencollective": "^1.0.3",
|
||||
"optional": "^0.1.4",
|
||||
"redis": "^2.7.1",
|
||||
"reflect-metadata": "^0.1.10",
|
||||
"rxjs": "^5.4.2",
|
||||
|
||||
@@ -16,4 +16,5 @@ export const FILTER_CATCH_EXCEPTIONS = '__filterCatchExceptions__';
|
||||
export const PIPES_METADATA = '__pipes__';
|
||||
export const GUARDS_METADATA = '__guards__';
|
||||
export const INTERCEPTORS_METADATA = '__interceptors__';
|
||||
export const HTTP_CODE_METADATA = '__httpCode__';
|
||||
export const HTTP_CODE_METADATA = '__httpCode__';
|
||||
export const GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
|
||||
4
src/common/enums/transport.enum.ts
Normal file
4
src/common/enums/transport.enum.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export enum Transport {
|
||||
TCP = 0,
|
||||
REDIS = 1,
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface CustomTransportStrategy {
|
||||
listen(callback: () => void): any;
|
||||
close(): any;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { CustomTransportStrategy } from './custom-transport-strategy.interface';
|
||||
import { Transport } from './../../enums/transport.enum';
|
||||
|
||||
export interface MicroserviceConfiguration {
|
||||
transport?: Transport;
|
||||
url?: string;
|
||||
port?: number;
|
||||
host?: string;
|
||||
strategy?: CustomTransportStrategy;
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
import { IoAdapter } from '@nestjs/websockets/adapters/io-adapter';
|
||||
import * as optional from 'optional';
|
||||
import { PipeTransform, WebSocketAdapter, ExceptionFilter, NestInterceptor, CanActivate } from '@nestjs/common';
|
||||
import { ConfigurationProvider } from '@nestjs/common/interfaces/configuration-provider.interface';
|
||||
|
||||
const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {} as any;
|
||||
|
||||
export class ApplicationConfig implements ConfigurationProvider {
|
||||
private globalPipes: PipeTransform<any>[] = [];
|
||||
private globalFilters: ExceptionFilter[] = [];
|
||||
private globalInterceptors: NestInterceptor[] = [];
|
||||
private globalGuards: CanActivate[] = [];
|
||||
private ioAdapter: WebSocketAdapter = new IoAdapter();
|
||||
private ioAdapter: WebSocketAdapter = IoAdapter ? new IoAdapter() : null;
|
||||
private globalPrefix = '';
|
||||
|
||||
public setGlobalPrefix(prefix: string) {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { RuntimeException } from './runtime.exception';
|
||||
import { MICROSERVICES_PACKAGE_NOT_FOUND_EXCEPTION } from '../messages';
|
||||
|
||||
export class MicroservicesPackageNotFoundException extends RuntimeException {
|
||||
constructor() {
|
||||
super(MICROSERVICES_PACKAGE_NOT_FOUND_EXCEPTION);
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,5 @@ export const UnknownExportMessage = (name: string) =>
|
||||
export const INVALID_MIDDLEWARE_CONFIGURATION = `Invalid middleware configuration passed inside the module 'configure()' method.`;
|
||||
export const UNKNOWN_REQUEST_MAPPING = `Request mapping properties not defined in the @RequestMapping() annotation!`;
|
||||
export const UNHANDLED_RUNTIME_EXCEPTION = `Unhandled Runtime Exception.`;
|
||||
export const INVALID_EXCEPTION_FILTER = `Invalid exception filters (@ExceptionFilters()).`;
|
||||
export const INVALID_EXCEPTION_FILTER = `Invalid exception filters (@ExceptionFilters()).`;
|
||||
export const MICROSERVICES_PACKAGE_NOT_FOUND_EXCEPTION = `Unable to load @nestjs/microservices packages (please, make sure whether it's installed already).`
|
||||
@@ -10,5 +10,4 @@ export { MiddlewareBuilder } from './middlewares/builder';
|
||||
export { ModuleRef } from './injector/module-ref';
|
||||
export * from './services/reflector.service';
|
||||
export * from './nest-factory';
|
||||
export * from './nest-application';
|
||||
export * from './nest-microservice';
|
||||
export * from './nest-application';
|
||||
@@ -1,27 +1,30 @@
|
||||
import * as optional from 'optional';
|
||||
import iterate from 'iterare';
|
||||
import {
|
||||
CanActivate,
|
||||
ExceptionFilter,
|
||||
NestInterceptor,
|
||||
OnModuleDestroy,
|
||||
PipeTransform,
|
||||
WebSocketAdapter
|
||||
WebSocketAdapter,
|
||||
} from '@nestjs/common';
|
||||
import { INestApplication, INestMicroservice, OnModuleInit } from '@nestjs/common';
|
||||
import { Logger } from '@nestjs/common/services/logger.service';
|
||||
import { isNil, isUndefined, validatePath } from '@nestjs/common/utils/shared.utils';
|
||||
import { MicroserviceConfiguration } from '@nestjs/microservices';
|
||||
import { MicroservicesModule } from '@nestjs/microservices/microservices-module';
|
||||
import { SocketModule } from '@nestjs/websockets/socket-module';
|
||||
import iterate from 'iterare';
|
||||
import { MicroserviceConfiguration } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface';
|
||||
import { ExpressAdapter } from './adapters/express-adapter';
|
||||
import { ApplicationConfig } from './application-config';
|
||||
import { messages } from './constants';
|
||||
import { NestMicroservice } from './index';
|
||||
import { NestContainer } from './injector/container';
|
||||
import { Module } from './injector/module';
|
||||
import { MiddlewaresModule } from './middlewares/middlewares-module';
|
||||
import { Resolver } from './router/interfaces/resolver.interface';
|
||||
import { RoutesResolver } from './router/routes-resolver';
|
||||
import { MicroservicesPackageNotFoundException } from './errors/exceptions/microservices-package-not-found.exception';
|
||||
|
||||
const { SocketModule } = optional('@nestjs/websockets/socket-module') || {} as any;
|
||||
const { MicroservicesModule } = optional('@nestjs/microservices/microservices-module') || {} as any;
|
||||
const { NestMicroservice } = optional('@nestjs/microservices/nest-microservice') || {} as any;
|
||||
|
||||
export class NestApplication implements INestApplication {
|
||||
private readonly config = new ApplicationConfig();
|
||||
@@ -41,9 +44,12 @@ export class NestApplication implements INestApplication {
|
||||
}
|
||||
|
||||
public async setupModules() {
|
||||
SocketModule.setup(this.container, this.config);
|
||||
MicroservicesModule.setup(this.container, this.config);
|
||||
MicroservicesModule.setupClients(this.container);
|
||||
SocketModule && SocketModule.setup(this.container, this.config);
|
||||
|
||||
if (MicroservicesModule) {
|
||||
MicroservicesModule.setup(this.container, this.config);
|
||||
MicroservicesModule.setupClients(this.container);
|
||||
}
|
||||
await MiddlewaresModule.setup(this.container, this.config);
|
||||
}
|
||||
|
||||
@@ -65,7 +71,11 @@ export class NestApplication implements INestApplication {
|
||||
}
|
||||
|
||||
public connectMicroservice(config: MicroserviceConfiguration): INestMicroservice {
|
||||
const instance = new NestMicroservice(this.container, config);
|
||||
if (!NestMicroservice) {
|
||||
throw new MicroservicesPackageNotFoundException();
|
||||
}
|
||||
|
||||
const instance = new NestMicroservice(this.container as any, config as any);
|
||||
instance.setupListeners();
|
||||
instance.setIsInitialized(true);
|
||||
instance.setIsInitHookCalled(true);
|
||||
@@ -108,7 +118,7 @@ export class NestApplication implements INestApplication {
|
||||
}
|
||||
|
||||
public close() {
|
||||
SocketModule.close();
|
||||
SocketModule && SocketModule.close();
|
||||
this.server && this.server.close();
|
||||
this.microservices.forEach((microservice) => {
|
||||
microservice.setIsTerminated(true);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as optional from 'optional';
|
||||
import { DependenciesScanner } from './scanner';
|
||||
import { InstanceLoader } from './injector/instance-loader';
|
||||
import { NestContainer } from './injector/container';
|
||||
@@ -6,12 +7,14 @@ import { NestModuleMetatype } from '@nestjs/common/interfaces/modules/module-met
|
||||
import { Logger } from '@nestjs/common/services/logger.service';
|
||||
import { messages } from './constants';
|
||||
import { NestApplication } from './nest-application';
|
||||
import { NestMicroservice } from './nest-microservice';
|
||||
import { isFunction } from '@nestjs/common/utils/shared.utils';
|
||||
import { MicroserviceConfiguration } from '@nestjs/microservices/interfaces/microservice-configuration.interface';
|
||||
import { MicroserviceConfiguration } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface';
|
||||
import { ExpressAdapter } from './adapters/express-adapter';
|
||||
import { INestApplication, INestMicroservice } from '@nestjs/common';
|
||||
import { MetadataScanner } from './metadata-scanner';
|
||||
import { MicroservicesPackageNotFoundException } from './errors/exceptions/microservices-package-not-found.exception';
|
||||
|
||||
const { NestMicroservice } = optional('@nestjs/microservices/nest-microservice') || {} as any;
|
||||
|
||||
export class NestFactoryStatic {
|
||||
private container = new NestContainer();
|
||||
@@ -46,9 +49,13 @@ export class NestFactoryStatic {
|
||||
module,
|
||||
config?: MicroserviceConfiguration): Promise<INestMicroservice> {
|
||||
|
||||
if (!NestMicroservice) {
|
||||
throw new MicroservicesPackageNotFoundException();
|
||||
}
|
||||
|
||||
await this.initialize(module);
|
||||
return this.createNestInstance<NestMicroservice>(
|
||||
new NestMicroservice(this.container, config),
|
||||
return this.createNestInstance<INestMicroservice>(
|
||||
new NestMicroservice(this.container, config as any),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,6 @@
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@nestjs/common": "^4.*",
|
||||
"@nestjs/websockets": "^4.*",
|
||||
"@nestjs/microservices": "^4.*",
|
||||
"reflect-metadata": "0.1.10",
|
||||
"rxjs": "^5.4.2"
|
||||
}
|
||||
|
||||
@@ -2,10 +2,9 @@ import 'reflect-metadata';
|
||||
import { NestContainer } from './injector/container';
|
||||
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
|
||||
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
|
||||
import { metadata, EXCEPTION_FILTERS_METADATA, GUARDS_METADATA, INTERCEPTORS_METADATA } from '@nestjs/common/constants';
|
||||
import { metadata, GATEWAY_MIDDLEWARES, EXCEPTION_FILTERS_METADATA, GUARDS_METADATA, INTERCEPTORS_METADATA } from '@nestjs/common/constants';
|
||||
import { NestModuleMetatype } from '@nestjs/common/interfaces/modules/module-metatype.interface';
|
||||
import { Metatype } from '@nestjs/common/interfaces/metatype.interface';
|
||||
import { GATEWAY_MIDDLEWARES } from '@nestjs/websockets/constants';
|
||||
import { MetadataScanner } from '../core/metadata-scanner';
|
||||
|
||||
export class DependenciesScanner {
|
||||
|
||||
@@ -8,5 +8,4 @@
|
||||
export * from './common';
|
||||
export * from './core';
|
||||
export * from './core/nest-factory';
|
||||
export * from './core/nest-application';
|
||||
export * from './core/nest-microservice';
|
||||
export * from './core/nest-application';
|
||||
@@ -10,4 +10,5 @@ export * from './interfaces';
|
||||
export * from './client';
|
||||
export * from './enums';
|
||||
export * from './server';
|
||||
export * from './exceptions';
|
||||
export * from './exceptions';
|
||||
export * from './nest-microservice';
|
||||
@@ -1,20 +1,22 @@
|
||||
import * as optional from 'optional';
|
||||
import iterate from 'iterare';
|
||||
import { NestContainer } from './injector/container';
|
||||
import { MicroservicesModule } from '@nestjs/microservices/microservices-module';
|
||||
import { messages } from './constants';
|
||||
import { NestContainer } from '@nestjs/core/injector/container';
|
||||
import { MicroservicesModule } from './microservices-module';
|
||||
import { messages } from '@nestjs/core/constants';
|
||||
import { Logger } from '@nestjs/common/services/logger.service';
|
||||
import { Server } from '@nestjs/microservices/server/server';
|
||||
import { MicroserviceConfiguration } from '@nestjs/microservices/interfaces/microservice-configuration.interface';
|
||||
import { ServerFactory } from '@nestjs/microservices/server/server-factory';
|
||||
import { Transport } from '@nestjs/microservices/enums/transport.enum';
|
||||
import { Server } from './server/server';
|
||||
import { MicroserviceConfiguration } from './interfaces/microservice-configuration.interface';
|
||||
import { ServerFactory } from './server/server-factory';
|
||||
import { Transport } from './enums/transport.enum';
|
||||
import { INestMicroservice, WebSocketAdapter, CanActivate, PipeTransform, NestInterceptor, ExceptionFilter, OnModuleInit } from '@nestjs/common';
|
||||
import { ApplicationConfig } from './application-config';
|
||||
import { SocketModule } from '@nestjs/websockets/socket-module';
|
||||
import { ApplicationConfig } from '@nestjs/core/application-config';
|
||||
import { CustomTransportStrategy } from '@nestjs/microservices';
|
||||
import { Module } from './injector/module';
|
||||
import { Module } from '@nestjs/core/injector/module';
|
||||
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
|
||||
import { OnModuleDestroy } from '@nestjs/common/interfaces';
|
||||
|
||||
const { SocketModule } = optional('@nestjs/websockets/socket-module') || {} as any;
|
||||
|
||||
export class NestMicroservice implements INestMicroservice {
|
||||
private readonly config = new ApplicationConfig();
|
||||
private readonly logger = new Logger(NestMicroservice.name, true);
|
||||
@@ -38,7 +40,7 @@ export class NestMicroservice implements INestMicroservice {
|
||||
}
|
||||
|
||||
public setupModules() {
|
||||
SocketModule.setup(this.container, this.config);
|
||||
SocketModule && SocketModule.setup(this.container, this.config);
|
||||
MicroservicesModule.setupClients(this.container);
|
||||
|
||||
this.setupListeners();
|
||||
@@ -96,7 +98,7 @@ export class NestMicroservice implements INestMicroservice {
|
||||
}
|
||||
|
||||
private closeApplication() {
|
||||
SocketModule.close();
|
||||
SocketModule && SocketModule.close();
|
||||
|
||||
this.callDestroyHook();
|
||||
this.setIsTerminated(true);
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as io from 'socket.io';
|
||||
import { MessageMappingProperties } from '../gateway-metadata-explorer';
|
||||
import { CONNECTION_EVENT, DISCONNECT_EVENT } from './../constants';
|
||||
import { CONNECTION_EVENT, DISCONNECT_EVENT } from '../constants';
|
||||
import { WebSocketAdapter } from '@nestjs/common';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import 'rxjs/add/observable/fromEvent';
|
||||
|
||||
Reference in New Issue
Block a user