Skip to content

Commit 7db42a1

Browse files
refactor(microservices) adjust to codebase style
1 parent bacad91 commit 7db42a1

18 files changed

Lines changed: 399 additions & 356 deletions

integration/microservices/e2e/sum-kafka.spec.ts

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,21 @@
1-
import {
2-
ArgumentsHost,
3-
Catch,
4-
ExceptionFilter,
5-
HttpException,
6-
INestApplication,
7-
RpcExceptionFilter,
8-
} from '@nestjs/common';
9-
import { RpcException, Transport } from '@nestjs/microservices';
1+
import { INestApplication } from '@nestjs/common';
2+
import { Transport } from '@nestjs/microservices';
103
import { Test } from '@nestjs/testing';
114
import { expect } from 'chai';
125
import * as request from 'supertest';
13-
import { KafkaController } from '../src/kafka/kafka.controller';
14-
import { APP_FILTER } from '@nestjs/core';
15-
import { Observable, throwError } from 'rxjs';
16-
import { KafkaMessagesController } from '../src/kafka/kafka.messages.controller';
6+
import { BusinessDto } from '../src/kafka/dtos/business.dto';
177
import { UserDto } from '../src/kafka/dtos/user.dto';
188
import { UserEntity } from '../src/kafka/entities/user.entity';
19-
import { BusinessDto } from '../src/kafka/dtos/business.dto';
20-
import { BusinessEntity } from '../src/kafka/entities/business.entity';
21-
22-
@Catch()
23-
class KafkaExceptionFilter implements ExceptionFilter {
24-
catch(exception: HttpException, host: ArgumentsHost): any {
25-
console.log(exception);
26-
}
27-
}
28-
@Catch()
29-
class RpcErrorFilter implements RpcExceptionFilter {
30-
catch(exception: RpcException): Observable<any> {
31-
console.log(exception);
32-
return throwError(exception);
33-
}
34-
}
9+
import { KafkaController } from '../src/kafka/kafka.controller';
10+
import { KafkaMessagesController } from '../src/kafka/kafka.messages.controller';
3511

3612
describe('Kafka transport', () => {
3713
let server;
3814
let app: INestApplication;
3915

4016
it(`Start Kafka app`, async () => {
4117
const module = await Test.createTestingModule({
42-
controllers: [
43-
KafkaController,
44-
KafkaMessagesController,
45-
],
46-
providers: [
47-
{
48-
provide: APP_FILTER,
49-
useClass: RpcErrorFilter,
50-
},
51-
{
52-
provide: APP_FILTER,
53-
useClass: KafkaExceptionFilter,
54-
},
55-
],
18+
controllers: [KafkaController, KafkaMessagesController],
5619
}).compile();
5720

5821
app = module.createNestApplication();
@@ -161,13 +124,17 @@ describe('Kafka transport', () => {
161124
for (let concurrencyKey = 0; concurrencyKey < 100; concurrencyKey++) {
162125
const innerUserDto = JSON.parse(JSON.stringify(userDto));
163126
innerUserDto.name += `+${concurrencyKey}`;
164-
promises.push(request(server).post('/user').send(userDto).expect(200));
127+
promises.push(
128+
request(server)
129+
.post('/user')
130+
.send(userDto)
131+
.expect(200),
132+
);
165133
}
166134
await Promise.all(promises);
167135
});
168136

169137
after(`Stopping Kafka app`, async () => {
170138
await app.close();
171139
});
172-
173140
}).timeout(30000);
Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import * as util from 'util';
2-
import { Body, Controller, HttpCode, Post, OnModuleInit } from '@nestjs/common';
3-
import {
4-
Client,
5-
Transport,
6-
ClientKafka,
7-
} from '@nestjs/microservices';
1+
import { Body, Controller, HttpCode, OnModuleInit, Post } from '@nestjs/common';
82
import { Logger } from '@nestjs/common/services/logger.service';
9-
3+
import { Client, ClientKafka, Transport } from '@nestjs/microservices';
104
import { Observable } from 'rxjs';
11-
import { UserDto } from './dtos/user.dto';
125
import { BusinessDto } from './dtos/business.dto';
6+
import { UserDto } from './dtos/user.dto';
137

148
@Controller()
159
export class KafkaController implements OnModuleInit {
@@ -27,7 +21,7 @@ export class KafkaController implements OnModuleInit {
2721
})
2822
private readonly client: ClientKafka;
2923

30-
onModuleInit(){
24+
onModuleInit() {
3125
const requestPatterns = [
3226
'math.sum.sync.kafka.message',
3327
'math.sum.sync.without.key',
@@ -39,7 +33,7 @@ export class KafkaController implements OnModuleInit {
3933
'business.create',
4034
];
4135

42-
requestPatterns.forEach((pattern) => {
36+
requestPatterns.forEach(pattern => {
4337
this.client.subscribeToResponseOf(pattern);
4438
});
4539
}
@@ -50,13 +44,15 @@ export class KafkaController implements OnModuleInit {
5044
async mathSumSyncKafkaMessage(
5145
@Body() data: number[],
5246
): Promise<Observable<any>> {
53-
const result = await this.client.send('math.sum.sync.kafka.message', {
54-
key: '1',
55-
value: {
56-
numbers: data,
57-
},
58-
}).toPromise();
59-
return result.value;
47+
const result = await this.client
48+
.send('math.sum.sync.kafka.message', {
49+
key: '1',
50+
value: {
51+
numbers: data,
52+
},
53+
})
54+
.toPromise();
55+
return result;
6056
}
6157

6258
// sync send kafka(ish) message without key and only the value
@@ -65,12 +61,14 @@ export class KafkaController implements OnModuleInit {
6561
async mathSumSyncWithoutKey(
6662
@Body() data: number[],
6763
): Promise<Observable<any>> {
68-
const result = await this.client.send('math.sum.sync.without.key', {
69-
value: {
70-
numbers: data,
71-
},
72-
}).toPromise();
73-
return result.value;
64+
const result = await this.client
65+
.send('math.sum.sync.without.key', {
66+
value: {
67+
numbers: data,
68+
},
69+
})
70+
.toPromise();
71+
return result;
7472
}
7573

7674
// sync send message without key or value
@@ -79,70 +77,76 @@ export class KafkaController implements OnModuleInit {
7977
async mathSumSyncPlainObject(
8078
@Body() data: number[],
8179
): Promise<Observable<any>> {
82-
const result = await this.client.send('math.sum.sync.plain.object', {
83-
numbers: data,
84-
}).toPromise();
85-
return result.value;
80+
const result = await this.client
81+
.send('math.sum.sync.plain.object', {
82+
numbers: data,
83+
})
84+
.toPromise();
85+
return result;
8686
}
8787

8888
// sync send message without key or value
8989
@Post('mathSumSyncArray')
9090
@HttpCode(200)
91-
async mathSumSyncArray(
92-
@Body() data: number[],
93-
): Promise<Observable<any>> {
94-
const result = await this.client.send('math.sum.sync.array', data).toPromise();
95-
return result.value;
91+
async mathSumSyncArray(@Body() data: number[]): Promise<Observable<any>> {
92+
const result = await this.client
93+
.send('math.sum.sync.array', data)
94+
.toPromise();
95+
return result;
9696
}
9797

9898
@Post('mathSumSyncString')
9999
@HttpCode(200)
100-
async mathSumSyncString(
101-
@Body() data: number[],
102-
): Promise<Observable<any>> {
100+
async mathSumSyncString(@Body() data: number[]): Promise<Observable<any>> {
103101
// this.logger.error(util.format('mathSumSyncString() data: %o', data));
104-
const result = await this.client.send('math.sum.sync.string', data.toString()).toPromise();
105-
return result.value;
102+
const result = await this.client
103+
.send('math.sum.sync.string', data.toString())
104+
.toPromise();
105+
return result;
106106
}
107107

108108
@Post('mathSumSyncNumber')
109109
@HttpCode(200)
110-
async mathSumSyncNumber(
111-
@Body() data: number[],
112-
): Promise<Observable<any>> {
113-
const result = await this.client.send('math.sum.sync.number', data[0]).toPromise();
114-
return result.value;
110+
async mathSumSyncNumber(@Body() data: number[]): Promise<Observable<any>> {
111+
const result = await this.client
112+
.send('math.sum.sync.number', data[0])
113+
.toPromise();
114+
return result;
115115
}
116116

117117
// async notify
118118
@Post('notify')
119119
async sendNotification(): Promise<any> {
120-
return this.client.emit('notify', {notify: true});
120+
return this.client.emit('notify', { notify: true });
121121
}
122122

123123
// Complex data to send.
124124
@Post('/user')
125125
@HttpCode(200)
126126
async createUser(@Body() user: UserDto): Promise<Observable<any>> {
127-
const result = await this.client.send('user.create', {
128-
key: '1',
129-
value: {
130-
user,
131-
},
132-
}).toPromise();
133-
return result.value;
127+
const result = await this.client
128+
.send('user.create', {
129+
key: '1',
130+
value: {
131+
user,
132+
},
133+
})
134+
.toPromise();
135+
return result;
134136
}
135137

136138
// Complex data to send.
137139
@Post('/business')
138140
@HttpCode(200)
139141
async createBusiness(@Body() business: BusinessDto) {
140-
const result = await this.client.send('business.create', {
141-
key: '1',
142-
value: {
143-
business,
144-
},
145-
}).toPromise();
146-
return result.value;
142+
const result = await this.client
143+
.send('business.create', {
144+
key: '1',
145+
value: {
146+
business,
147+
},
148+
})
149+
.toPromise();
150+
return result;
147151
}
148152
}

0 commit comments

Comments
 (0)