| 20 | |
| 21 | @Controller() |
| 22 | export class GrpcController { |
| 23 | private readonly customClient: ClientGrpc; |
| 24 | constructor() { |
| 25 | this.customClient = new ErrorHandlingProxy({ |
| 26 | package: 'math', |
| 27 | protoPath: join(__dirname, 'math.proto'), |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | @Client({ |
| 32 | transport: Transport.GRPC, |
| 33 | options: { |
| 34 | package: 'math', |
| 35 | protoPath: join(__dirname, 'math.proto'), |
| 36 | }, |
| 37 | }) |
| 38 | client: ClientGrpc; |
| 39 | |
| 40 | @Client({ |
| 41 | transport: Transport.GRPC, |
| 42 | options: { |
| 43 | package: ['math', 'math2'], |
| 44 | protoPath: [ |
| 45 | join(__dirname, 'math.proto'), |
| 46 | join(__dirname, 'math2.proto'), |
| 47 | ], |
| 48 | }, |
| 49 | }) |
| 50 | clientMulti: ClientGrpc; |
| 51 | |
| 52 | @Post('sum') |
| 53 | @HttpCode(200) |
| 54 | call(@Body() data: number[]): Observable<number> { |
| 55 | const svc = this.client.getService<any>('Math'); |
| 56 | return svc.sum({ data }); |
| 57 | } |
| 58 | |
| 59 | // Test that getService generate both lower and uppercase method |
| 60 | @Post('upperMethod/sum') |
| 61 | @HttpCode(200) |
| 62 | callWithOptions(@Body() data: number[]): Observable<number> { |
| 63 | const svc = this.client.getService<any>('Math'); |
| 64 | return svc.Sum({ data }); |
| 65 | } |
| 66 | |
| 67 | @GrpcMethod('Math') |
| 68 | async sum({ data }: { data: number[] }): Promise<any> { |
| 69 | return of({ |
| 70 | result: data.reduce((a, b) => a + b), |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | @GrpcStreamMethod('Math') |
| 75 | async sumStream(messages: Observable<any>): Promise<any> { |
| 76 | return new Promise<any>((resolve, reject) => { |
| 77 | messages.subscribe({ |
| 78 | next: msg => { |
| 79 | resolve({ |
nothing calls this directly
no test coverage detected