mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
refactor(): adjust style to the rest of codebase
This commit is contained in:
@@ -8,8 +8,8 @@ import {
|
||||
PATTERN_METADATA,
|
||||
TRANSPORT_METADATA,
|
||||
} from './constants';
|
||||
import { PatternHandler } from './enums/pattern-handler.enum';
|
||||
import { Transport } from './enums';
|
||||
import { PatternHandler } from './enums/pattern-handler.enum';
|
||||
import { ClientOptions } from './interfaces/client-metadata.interface';
|
||||
import { PatternMetadata } from './interfaces/pattern-metadata.interface';
|
||||
|
||||
@@ -20,10 +20,10 @@ export interface ClientProperties {
|
||||
|
||||
export interface PatternProperties {
|
||||
pattern: PatternMetadata;
|
||||
transport?: Transport;
|
||||
methodKey: string;
|
||||
isEventHandler: boolean;
|
||||
targetCallback: (...args: any[]) => any;
|
||||
transport?: Transport;
|
||||
}
|
||||
|
||||
export interface MessageRequestProperties {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common/interfaces';
|
||||
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
|
||||
import { isUndefined } from '@nestjs/common/utils/shared.utils';
|
||||
import { ContextIdFactory } from '@nestjs/core/helpers/context-id-factory';
|
||||
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host';
|
||||
import { STATIC_CONTEXT } from '@nestjs/core/injector/constants';
|
||||
@@ -30,7 +31,6 @@ import {
|
||||
import { ListenerMetadataExplorer } from './listener-metadata-explorer';
|
||||
import { ServerGrpc } from './server';
|
||||
import { Server } from './server/server';
|
||||
import { isUndefined } from '@nestjs/common/utils/shared.utils';
|
||||
|
||||
export class ListenersController {
|
||||
private readonly metadataExplorer = new ListenerMetadataExplorer(
|
||||
@@ -62,9 +62,15 @@ export class ListenersController {
|
||||
? DEFAULT_GRPC_CALLBACK_METADATA
|
||||
: DEFAULT_CALLBACK_METADATA;
|
||||
|
||||
patternHandlers.forEach(
|
||||
({ pattern, targetCallback, methodKey, transport, isEventHandler }) => {
|
||||
if (isUndefined(transport) || isUndefined(server.transportId) || transport === server.transportId) {
|
||||
patternHandlers
|
||||
.filter(
|
||||
({ transport }) =>
|
||||
isUndefined(transport) ||
|
||||
isUndefined(server.transportId) ||
|
||||
transport === server.transportId,
|
||||
)
|
||||
.forEach(
|
||||
({ pattern, targetCallback, methodKey, transport, isEventHandler }) => {
|
||||
if (isStatic) {
|
||||
const proxy = this.contextCreator.create(
|
||||
instance as object,
|
||||
@@ -86,9 +92,8 @@ export class ListenersController {
|
||||
defaultCallMetadata,
|
||||
);
|
||||
server.addHandler(pattern, asyncHandler, isEventHandler);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
public assignClientsToProperties(instance: Controller | Injectable) {
|
||||
|
||||
@@ -13,12 +13,12 @@ import {
|
||||
GRPC_DEFAULT_URL,
|
||||
} from '../constants';
|
||||
import { GrpcMethodStreamingType } from '../decorators';
|
||||
import { Transport } from '../enums';
|
||||
import { InvalidGrpcPackageException } from '../errors/invalid-grpc-package.exception';
|
||||
import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definition.exception';
|
||||
import { CustomTransportStrategy, MessageHandler } from '../interfaces';
|
||||
import { GrpcOptions } from '../interfaces/microservice-configuration.interface';
|
||||
import { Server } from './server';
|
||||
import { Transport } from '../enums';
|
||||
|
||||
let grpcPackage: any = {};
|
||||
let grpcProtoLoaderPackage: any = {};
|
||||
@@ -33,9 +33,10 @@ interface GrpcCall<TRequest = any, TMetadata = any> {
|
||||
}
|
||||
|
||||
export class ServerGrpc extends Server implements CustomTransportStrategy {
|
||||
public readonly transportId = Transport.GRPC;
|
||||
|
||||
private readonly url: string;
|
||||
private grpcClient: any;
|
||||
public readonly transportId: Transport = Transport.GRPC;
|
||||
|
||||
constructor(private readonly options: GrpcOptions['options']) {
|
||||
super();
|
||||
|
||||
@@ -31,6 +31,8 @@ import { Server } from './server';
|
||||
let kafkaPackage: any = {};
|
||||
|
||||
export class ServerKafka extends Server implements CustomTransportStrategy {
|
||||
public readonly transportId = Transport.KAFKA;
|
||||
|
||||
protected readonly logger = new Logger(ServerKafka.name);
|
||||
protected client: Kafka = null;
|
||||
protected consumer: Consumer = null;
|
||||
@@ -38,7 +40,6 @@ export class ServerKafka extends Server implements CustomTransportStrategy {
|
||||
private readonly brokers: string[];
|
||||
private readonly clientId: string;
|
||||
private readonly groupId: string;
|
||||
public readonly transportId: Transport = Transport.KAFKA;
|
||||
|
||||
constructor(private readonly options: KafkaOptions['options']) {
|
||||
super();
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
NO_MESSAGE_HANDLER,
|
||||
} from '../constants';
|
||||
import { MqttContext } from '../ctx-host/mqtt.context';
|
||||
import { Transport } from '../enums';
|
||||
import { MqttClient } from '../external/mqtt-client.interface';
|
||||
import {
|
||||
CustomTransportStrategy,
|
||||
@@ -21,14 +22,14 @@ import {
|
||||
} from '../interfaces';
|
||||
import { MqttOptions } from '../interfaces/microservice-configuration.interface';
|
||||
import { Server } from './server';
|
||||
import { Transport } from '../enums';
|
||||
|
||||
let mqttPackage: any = {};
|
||||
|
||||
export class ServerMqtt extends Server implements CustomTransportStrategy {
|
||||
public readonly transportId = Transport.MQTT;
|
||||
|
||||
private readonly url: string;
|
||||
private mqttClient: MqttClient;
|
||||
public readonly transportId: Transport = Transport.MQTT;
|
||||
|
||||
constructor(private readonly options: MqttOptions['options']) {
|
||||
super();
|
||||
|
||||
@@ -7,19 +7,20 @@ import {
|
||||
NO_MESSAGE_HANDLER,
|
||||
} from '../constants';
|
||||
import { NatsContext } from '../ctx-host/nats.context';
|
||||
import { Transport } from '../enums';
|
||||
import { Client } from '../external/nats-client.interface';
|
||||
import { CustomTransportStrategy, PacketId } from '../interfaces';
|
||||
import { NatsOptions } from '../interfaces/microservice-configuration.interface';
|
||||
import { IncomingRequest, ReadPacket } from '../interfaces/packet.interface';
|
||||
import { Server } from './server';
|
||||
import { Transport } from '../enums';
|
||||
|
||||
let natsPackage: any = {};
|
||||
|
||||
export class ServerNats extends Server implements CustomTransportStrategy {
|
||||
public readonly transportId = Transport.NATS;
|
||||
|
||||
private readonly url: string;
|
||||
private natsClient: Client;
|
||||
public readonly transportId: Transport = Transport.NATS;
|
||||
|
||||
constructor(private readonly options: NatsOptions['options']) {
|
||||
super();
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
REDIS_DEFAULT_URL,
|
||||
} from '../constants';
|
||||
import { RedisContext } from '../ctx-host';
|
||||
import { Transport } from '../enums';
|
||||
import {
|
||||
ClientOpts,
|
||||
RedisClient,
|
||||
@@ -16,16 +17,16 @@ import {
|
||||
import { CustomTransportStrategy, IncomingRequest } from '../interfaces';
|
||||
import { RedisOptions } from '../interfaces/microservice-configuration.interface';
|
||||
import { Server } from './server';
|
||||
import { Transport } from '../enums';
|
||||
|
||||
let redisPackage: any = {};
|
||||
|
||||
export class ServerRedis extends Server implements CustomTransportStrategy {
|
||||
public readonly transportId = Transport.REDIS;
|
||||
|
||||
private readonly url: string;
|
||||
private subClient: RedisClient;
|
||||
private pubClient: RedisClient;
|
||||
private isExplicitlyTerminated = false;
|
||||
public readonly transportId: Transport = Transport.REDIS;
|
||||
|
||||
constructor(private readonly options: RedisOptions['options']) {
|
||||
super();
|
||||
|
||||
@@ -13,17 +13,19 @@ import {
|
||||
RQM_DEFAULT_URL,
|
||||
} from '../constants';
|
||||
import { RmqContext } from '../ctx-host';
|
||||
import { Transport } from '../enums';
|
||||
import { CustomTransportStrategy, RmqOptions } from '../interfaces';
|
||||
import {
|
||||
IncomingRequest,
|
||||
OutgoingResponse,
|
||||
} from '../interfaces/packet.interface';
|
||||
import { Server } from './server';
|
||||
import { Transport } from '../enums';
|
||||
|
||||
let rqmPackage: any = {};
|
||||
|
||||
export class ServerRMQ extends Server implements CustomTransportStrategy {
|
||||
public readonly transportId = Transport.RMQ;
|
||||
|
||||
private server: any = null;
|
||||
private channel: any = null;
|
||||
private readonly urls: string[];
|
||||
@@ -31,7 +33,6 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
|
||||
private readonly prefetchCount: number;
|
||||
private readonly queueOptions: any;
|
||||
private readonly isGlobalPrefetchCount: boolean;
|
||||
public readonly transportId: Transport = Transport.RMQ;
|
||||
|
||||
constructor(private readonly options: RmqOptions['options']) {
|
||||
super();
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
TCP_DEFAULT_PORT,
|
||||
} from '../constants';
|
||||
import { TcpContext } from '../ctx-host/tcp.context';
|
||||
import { Transport } from '../enums';
|
||||
import { JsonSocket } from '../helpers/json-socket';
|
||||
import {
|
||||
CustomTransportStrategy,
|
||||
@@ -21,16 +22,15 @@ import {
|
||||
} from '../interfaces';
|
||||
import { TcpOptions } from '../interfaces/microservice-configuration.interface';
|
||||
import { Server } from './server';
|
||||
import { Transport } from '../enums';
|
||||
|
||||
export class ServerTCP extends Server implements CustomTransportStrategy {
|
||||
public readonly transportId = Transport.TCP;
|
||||
|
||||
private readonly port: number;
|
||||
private readonly host: string;
|
||||
|
||||
private server: NetSocket;
|
||||
private isExplicitlyTerminated = false;
|
||||
private retryAttemptsCount = 0;
|
||||
public readonly transportId: Transport = Transport.TCP;
|
||||
|
||||
constructor(private readonly options: TcpOptions['options']) {
|
||||
super();
|
||||
|
||||
@@ -11,9 +11,9 @@ import { ClientProxyFactory } from '../client';
|
||||
import { ClientsContainer } from '../container';
|
||||
import { ExceptionFiltersContext } from '../context/exception-filters-context';
|
||||
import { RpcContextCreator } from '../context/rpc-context-creator';
|
||||
import { Transport } from '../enums/transport.enum';
|
||||
import { ListenerMetadataExplorer } from '../listener-metadata-explorer';
|
||||
import { ListenersController } from '../listeners-controller';
|
||||
import { Transport } from '../enums/transport.enum';
|
||||
|
||||
describe('ListenersController', () => {
|
||||
let instance: ListenersController,
|
||||
@@ -79,7 +79,11 @@ describe('ListenersController', () => {
|
||||
});
|
||||
it(`should call "addHandler" method of server for each pattern handler with same transport`, () => {
|
||||
const serverHandlers = [
|
||||
{ pattern: { cmd: 'test'}, targetCallback: 'tt', transport: Transport.TCP },
|
||||
{
|
||||
pattern: { cmd: 'test' },
|
||||
targetCallback: 'tt',
|
||||
transport: Transport.TCP,
|
||||
},
|
||||
{ pattern: 'test2', targetCallback: '2', transport: Transport.KAFKA },
|
||||
];
|
||||
explorer.expects('explore').returns(serverHandlers);
|
||||
@@ -88,7 +92,7 @@ describe('ListenersController', () => {
|
||||
});
|
||||
it(`should call "addHandler" method of server without transportID for each pattern handler with any transport value`, () => {
|
||||
const serverHandlers = [
|
||||
{ pattern: { cmd: 'test'}, targetCallback: 'tt' },
|
||||
{ pattern: { cmd: 'test' }, targetCallback: 'tt' },
|
||||
{ pattern: 'test2', targetCallback: '2', transport: Transport.KAFKA },
|
||||
];
|
||||
explorer.expects('explore').returns(serverHandlers);
|
||||
@@ -99,7 +103,11 @@ describe('ListenersController', () => {
|
||||
const serverHandlers = [
|
||||
{ pattern: 'test', targetCallback: 'tt' },
|
||||
{ pattern: 'test2', targetCallback: '2', transport: Transport.KAFKA },
|
||||
{ pattern: { cmd: 'test3'}, targetCallback: '3', transport: Transport.TCP },
|
||||
{
|
||||
pattern: { cmd: 'test3' },
|
||||
targetCallback: '3',
|
||||
transport: Transport.TCP,
|
||||
},
|
||||
];
|
||||
explorer.expects('explore').returns(serverHandlers);
|
||||
instance.registerPatternHandlers(new InstanceWrapper(), serverTCP, '');
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
module.exports = {
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
project: 'tsconfig.json',
|
||||
sourceType: 'module',
|
||||
},
|
||||
plugins: ['@typescript-eslint/eslint-plugin'],
|
||||
extends: [
|
||||
'plugin:@typescript-eslint/eslint-recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
'prettier',
|
||||
'prettier/@typescript-eslint',
|
||||
],
|
||||
root: true,
|
||||
env: {
|
||||
node: true,
|
||||
jest: true,
|
||||
},
|
||||
rules: {
|
||||
'@typescript-eslint/interface-name-prefix': 'off',
|
||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||
'@typescript-eslint/no-explicit-any': 'off',
|
||||
},
|
||||
};
|
||||
21
sample/28-hybrid-app-microservices/.gitignore
vendored
21
sample/28-hybrid-app-microservices/.gitignore
vendored
@@ -1,21 +0,0 @@
|
||||
# dependencies
|
||||
/node_modules
|
||||
|
||||
# IDE
|
||||
/.idea
|
||||
/.awcache
|
||||
/.vscode
|
||||
|
||||
# misc
|
||||
npm-debug.log
|
||||
|
||||
# example
|
||||
/quick-start
|
||||
|
||||
# tests
|
||||
/test
|
||||
/coverage
|
||||
/.nyc_output
|
||||
|
||||
# dist
|
||||
/dist
|
||||
12712
sample/28-hybrid-app-microservices/package-lock.json
generated
12712
sample/28-hybrid-app-microservices/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,54 +0,0 @@
|
||||
{
|
||||
"name": "nest-typescript-starter",
|
||||
"version": "1.0.0",
|
||||
"description": "Nest TypeScript starter repository",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"prebuild": "rimraf dist",
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "eslint '{src,apps,libs,test}/**/*.ts' --fix",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||
"test:e2e": "echo 'No e2e tests implemented yet.'"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "7.0.9",
|
||||
"@nestjs/core": "7.0.9",
|
||||
"@nestjs/microservices": "7.0.9",
|
||||
"@nestjs/platform-express": "7.0.9",
|
||||
"class-transformer": "0.2.3",
|
||||
"class-validator": "0.12.2",
|
||||
"reflect-metadata": "0.1.13",
|
||||
"rimraf": "3.0.2",
|
||||
"rxjs": "6.5.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "7.1.5",
|
||||
"@nestjs/schematics": "7.0.0",
|
||||
"@nestjs/testing": "7.0.9",
|
||||
"@types/amqplib": "0.5.13",
|
||||
"@types/express": "4.17.6",
|
||||
"@types/node": "12.12.31",
|
||||
"@types/supertest": "2.0.9",
|
||||
"jest": "26.0.1",
|
||||
"prettier": "2.0.5",
|
||||
"supertest": "4.0.2",
|
||||
"ts-jest": "25.5.0",
|
||||
"ts-loader": "7.0.3",
|
||||
"ts-node": "8.10.1",
|
||||
"tsconfig-paths": "3.9.0",
|
||||
"@typescript-eslint/eslint-plugin": "2.31.0",
|
||||
"@typescript-eslint/parser": "2.31.0",
|
||||
"eslint": "6.8.0",
|
||||
"eslint-config-prettier": "6.11.0",
|
||||
"eslint-plugin-import": "2.20.2",
|
||||
"typescript": "3.7.2"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MathModule } from './math/math.module';
|
||||
|
||||
@Module({
|
||||
imports: [MathModule],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -1,10 +0,0 @@
|
||||
export default {
|
||||
client: {
|
||||
clientId: 'default',
|
||||
brokers: ['localhost:9092'],
|
||||
ssl: false,
|
||||
},
|
||||
consumer: {
|
||||
groupId: 'default-consumer',
|
||||
},
|
||||
};
|
||||
@@ -1,24 +0,0 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
|
||||
import { AppModule } from './app.module';
|
||||
import kafkaOptions from './common/kafka-options';
|
||||
|
||||
async function bootstrap() {
|
||||
/**
|
||||
* This example contains a hybrid application (HTTP + TCP + KAFKA)
|
||||
*/
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.connectMicroservice<MicroserviceOptions>({
|
||||
transport: Transport.TCP,
|
||||
options: { retryAttempts: 5, retryDelay: 3000 },
|
||||
});
|
||||
app.connectMicroservice<MicroserviceOptions>({
|
||||
transport: Transport.KAFKA,
|
||||
options: kafkaOptions,
|
||||
});
|
||||
|
||||
await app.startAllMicroservicesAsync();
|
||||
await app.listen(3001);
|
||||
console.log(`Application is running on: ${await app.getUrl()}`);
|
||||
}
|
||||
bootstrap();
|
||||
@@ -1 +0,0 @@
|
||||
export const MATH_TCP_SERVICE = 'MATH_TCP_SERVICE';
|
||||
@@ -1,43 +0,0 @@
|
||||
import { Controller, Get, Inject } from '@nestjs/common';
|
||||
import { ClientProxy, MessagePattern, Payload, Transport,ClientTCP, ClientKafka} from '@nestjs/microservices';
|
||||
import { Observable } from 'rxjs';
|
||||
import { MATH_TCP_SERVICE } from './math.constants';
|
||||
|
||||
|
||||
@Controller()
|
||||
export class MathController {
|
||||
constructor(@Inject(MATH_TCP_SERVICE) private readonly tcpClient: ClientTCP) {}
|
||||
|
||||
@Get()
|
||||
tcpExecute(): Observable<number> {
|
||||
const pattern = { cmd: 'sum' };
|
||||
const data = [1, 2, 3, 4, 5];
|
||||
return this.tcpClient.send<number>(pattern, data);
|
||||
}
|
||||
|
||||
@Get('share')
|
||||
shareTcpExecute(): Observable<number> {
|
||||
const pattern = 'sum-cmd-topic';
|
||||
const data = [1, 2, 3, 4, 5];
|
||||
return this.tcpClient.send<number>(pattern, data);
|
||||
}
|
||||
|
||||
// Share handle function for KAFKA and TCP services
|
||||
@MessagePattern('sum-cmd-topic')
|
||||
sum(message: any): number {
|
||||
const data = Array.isArray(message) ? message : message.value;
|
||||
return (data || []).reduce((a, b) => a + b);
|
||||
}
|
||||
|
||||
// handle function for KAFKA transport service
|
||||
@MessagePattern('sum', Transport.KAFKA)
|
||||
kafkaSum(@Payload() message: Record<string, any>): number {
|
||||
return (message.value || []).reduce((a, b) => a + b);
|
||||
}
|
||||
|
||||
// handle function for TCP transport service
|
||||
@MessagePattern({ cmd: 'sum' }, Transport.TCP)
|
||||
tcpSum(data: number[]): number {
|
||||
return (data || []).reduce((a, b) => a + b);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ClientsModule, Transport } from '@nestjs/microservices';
|
||||
import { MATH_TCP_SERVICE } from './math.constants';
|
||||
import { MathController } from './math.controller';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ClientsModule.register([{ name: MATH_TCP_SERVICE, transport: Transport.TCP }]),
|
||||
],
|
||||
controllers: [MathController],
|
||||
})
|
||||
export class MathModule {}
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"removeComments": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"target": "es2017",
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"baseUrl": "./",
|
||||
"incremental": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user