MCPcopy
hub / github.com/nestjs/nest / AppController

Class AppController

integration/microservices/src/app.controller.ts:21–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19
20@Controller()
21export class AppController {
22 constructor(
23 @Inject('USE_CLASS_CLIENT') private useClassClient: ClientProxy,
24 @Inject('USE_FACTORY_CLIENT') private useFactoryClient: ClientProxy,
25 @Inject('CUSTOM_PROXY_CLIENT') private customClient: ClientProxy,
26 ) {}
27 static IS_NOTIFIED = false;
28
29 @Client({ transport: Transport.TCP })
30 client: ClientProxy;
31
32 @Post()
33 @HttpCode(200)
34 call(@Query('command') cmd, @Body() data: number[]): Observable<number> {
35 return this.client.send<number>({ cmd }, data);
36 }
37
38 @Post('useFactory')
39 @HttpCode(200)
40 callWithClientUseFactory(
41 @Query('command') cmd,
42 @Body() data: number[],
43 ): Observable<number> {
44 return this.useFactoryClient.send<number>({ cmd }, data);
45 }
46
47 @Post('useClass')
48 @HttpCode(200)
49 callWithClientUseClass(
50 @Query('command') cmd,
51 @Body() data: number[],
52 ): Observable<number> {
53 return this.useClassClient.send<number>({ cmd }, data);
54 }
55
56 @Post('stream')
57 @HttpCode(200)
58 stream(@Body() data: number[]): Observable<number> {
59 return this.client
60 .send<number>({ cmd: 'streaming' }, data)
61 .pipe(scan((a, b) => a + b));
62 }
63
64 @Post('concurrent')
65 @HttpCode(200)
66 concurrent(@Body() data: number[][]): Promise<boolean> {
67 const send = async (tab: number[]) => {
68 const expected = tab.reduce((a, b) => a + b);
69 const result = await lastValueFrom(
70 this.client.send<number>({ cmd: 'sum' }, tab),
71 );
72
73 return result === expected;
74 };
75 return data
76 .map(async tab => send(tab))
77 .reduce(async (a, b) => (await a) && b);
78 }

Callers

nothing calls this directly

Calls 5

ClientFunction · 0.90
HttpCodeFunction · 0.90
MessagePatternFunction · 0.90
EventPatternFunction · 0.90
PostClass · 0.50

Tested by

no test coverage detected