All files / lib/controller grpc.controller.ts

48.27% Statements 14/29
33.33% Branches 2/6
25% Functions 3/12
50% Lines 13/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                            1x 1x                                         1x             1x 1x 1x 1x 1x   1x       1x 1x                   1x         1x                                                
import { nxngstarter } from '@app/proto';
import { Controller, Get, Inject, OnModuleInit, Param } from '@nestjs/common';
import { type ClientGrpc, GrpcMethod, GrpcStreamMethod } from '@nestjs/microservices';
import { from, Observable, of, ReplaySubject, Subject } from 'rxjs';
import { toArray } from 'rxjs/operators';
 
import { NXNGSTARTER_PACKAGE } from '../grpc-client.options';
 
export interface IEntityService {
  findOne(data: nxngstarter.IEntityById): Observable<nxngstarter.IEntity>;
  findMany(upstream: Observable<nxngstarter.IEntityById>): Observable<nxngstarter.IEntity>;
}
 
@Controller('grpc')
export class AppGrpcController implements OnModuleInit {
  private readonly items: nxngstarter.IEntity[] = [
    {
      id: 'id1',
      integer: 1,
      boolean: true,
      float: 0.5,
      any: null,
      subEntities: [],
    },
    {
      id: 'id2',
      integer: 2,
      boolean: false,
      float: 1.5,
      any: null,
      subEntities: [{ id: 'subid1' }],
    },
  ];
 
  private sampleService?: IEntityService;
 
  constructor(@Inject(NXNGSTARTER_PACKAGE) private readonly client: ClientGrpc) {}
 
  public onModuleInit() {
    this.sampleService = this.client.getService<IEntityService>('EntityService');
  }
 
  @Get()
  public getMany(): Observable<nxngstarter.IEntity[]> {
    const idsSubject = new ReplaySubject<nxngstarter.IEntityById>();
    idsSubject.next({ id: 'id1' });
    idsSubject.next({ id: 'id2' });
    idsSubject.complete();
 
    return typeof this.sampleService !== 'undefined' ? this.sampleService.findMany(idsSubject.asObservable()).pipe(toArray()) : of([]);
  }
 
  @Get(':id')
  public getById(@Param('id') id: string): Observable<nxngstarter.IEntity> {
    return typeof this.sampleService !== 'undefined'
      ? from(this.sampleService.findOne({ id }))
      : of(
          nxngstarter.Entity.toObject(new nxngstarter.Entity(), {
            defaults: true,
          }),
        );
  }
 
  @GrpcMethod('EntityService', 'FindOne')
  public findOne(data: nxngstarter.IEntityById, metadata: Record<string, unknown>): nxngstarter.IEntity | undefined {
    return this.items.find(({ id }) => id === data.id);
  }
 
  @GrpcStreamMethod('EntityService', 'FindMany')
  public findMany(data$: Observable<nxngstarter.IEntityById>, metadata: Record<string, unknown>): Observable<nxngstarter.IEntity> {
    const entitySubject = new Subject<nxngstarter.IEntity>();
 
    const onNext = (entityById: nxngstarter.IEntityById) => {
      const item = this.items.find(({ id }) => id === entityById.id);
      if (typeof item !== 'undefined') {
        entitySubject.next(item);
      }
    };
    const onComplete = () => {
      entitySubject.complete();
    };
    void data$.subscribe({
      next(value) {
        return onNext(value);
      },
      complete() {
        return onComplete();
      },
    });
 
    return entitySubject.asObservable();
  }
}