/
githubmirror
/
nest
Обзор
Документация
Войти
/
githubmirror
/
nest
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sample/04-grpc/src/hero/hero.controller.ts
68 строк
2 KB
YoungKi Lyu
sample(04-grpc): synchronize pluralisation docs
26 фев 2024, 17:52
26 фев 2024, 17:52
cad84c6
Код
Авторство
О чём код?
import { Controller, Get, Inject, OnModuleInit, Param } from '@nestjs/common'; import { ClientGrpc, GrpcMethod, GrpcStreamMethod, } from '@nestjs/microservices'; import { Observable, ReplaySubject, Subject } from 'rxjs'; import { toArray } from 'rxjs/operators'; import { HeroById } from './interfaces/hero-by-id.interface'; import { Hero } from './interfaces/hero.interface'; interface HeroesService { findOne(data: HeroById): Observable<Hero>; findMany(upstream: Observable<HeroById>): Observable<Hero>; } @Controller('hero') export class HeroController implements OnModuleInit { private readonly items: Hero[] = [ { id: 1, name: 'John' }, { id: 2, name: 'Doe' }, ]; private heroesService: HeroesService; constructor(@Inject('HERO_PACKAGE') private readonly client: ClientGrpc) {} onModuleInit() { this.heroesService = this.client.getService<HeroesService>('HeroesService'); } @Get() getMany(): Observable<Hero[]> { const ids$ = new ReplaySubject<HeroById>(); ids$.next({ id: 1 }); ids$.next({ id: 2 }); ids$.complete(); const stream = this.heroesService.findMany(ids$.asObservable()); return stream.pipe(toArray()); } @Get(':id') getById(@Param('id') id: string): Observable<Hero> { return this.heroesService.findOne({ id: +id }); } @GrpcMethod('HeroesService') findOne(data: HeroById): Hero { return this.items.find(({ id }) => id === data.id); } @GrpcStreamMethod('HeroesService') findMany(data$: Observable<HeroById>): Observable<Hero> { const hero$ = new Subject<Hero>(); const onNext = (heroById: HeroById) => { const item = this.items.find(({ id }) => id === heroById.id); hero$.next(item); }; const onComplete = () => hero$.complete(); data$.subscribe({ next: onNext, complete: onComplete, }); return hero$.asObservable(); } }