forked from stephenh/ts-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Toon van Strijp
committed
May 4, 2020
1 parent
edc39ca
commit 7213e8b
Showing
10 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
import { HeroService, HeroById, Hero, Villain, VillainById } from './hero'; | ||
import { Observable, of } from 'rxjs'; | ||
import { Observable, of, Subject } from 'rxjs'; | ||
|
||
export class SampleService implements HeroService { | ||
findOneHero(request: HeroById): Observable<Hero> { | ||
return of({ id: 1, name: 'test' }); | ||
} | ||
|
||
findOneVillain(request: VillainById): Observable<Villain> { | ||
return of({ id: 1, name: 'test' }); | ||
} | ||
|
||
findManyVillain(request: Observable<VillainById>): Observable<Villain> { | ||
const hero$ = new Subject<Villain>(); | ||
|
||
const onNext = (villainById: VillainById) => { | ||
hero$.next({ id: 1, name: 'test' }); | ||
}; | ||
const onComplete = () => hero$.complete(); | ||
request.subscribe(onNext, null, onComplete); | ||
|
||
return hero$.asObservable(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { HeroController } from './hero.controller'; | ||
import { ClientsModule, Transport } from '@nestjs/microservices'; | ||
import { join } from 'path'; | ||
|
||
@Module({ | ||
imports: [ | ||
ClientsModule.register([ | ||
{ | ||
name: 'HERO_PACKAGE', | ||
transport: Transport.GRPC, | ||
options: { | ||
url: '0.0.0.0:8080', | ||
package: 'hero', | ||
protoPath: join(__dirname, '../hero.proto'), | ||
}, | ||
}, | ||
]), | ||
], | ||
controllers: [HeroController] | ||
}) | ||
export class AppModule {} |
47 changes: 47 additions & 0 deletions
47
integration/nestjs-simple/nestjs-project/hero.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Controller, OnModuleInit } from '@nestjs/common'; | ||
import { | ||
GrpcMethod, | ||
GrpcStreamMethod, | ||
} from '@nestjs/microservices'; | ||
import { Observable, Subject } from 'rxjs'; | ||
import { HeroById, Hero, HeroService, VillainById, Villain } from '../hero'; | ||
|
||
@Controller('hero') | ||
export class HeroController implements OnModuleInit, HeroService { | ||
private readonly heroes: Hero[] = [ | ||
{ id: 1, name: 'Stephenh' }, | ||
{ id: 2, name: 'Iangregsondev' }, | ||
]; | ||
|
||
private readonly villains: Villain[] = [ | ||
{ id: 1, name: 'John' }, | ||
{ id: 2, name: 'Doe' }, | ||
]; | ||
|
||
onModuleInit() { | ||
} | ||
|
||
@GrpcMethod('HeroService') | ||
async findOneHero(data: HeroById): Promise<Hero> { | ||
return this.heroes.find(({ id }) => id === data.id)!; | ||
} | ||
|
||
@GrpcMethod('HeroService') | ||
async findOneVillain(data: VillainById): Promise<Villain> { | ||
return this.villains.find(({ id }) => id === data.id)!; | ||
} | ||
|
||
@GrpcStreamMethod('HeroService') | ||
findManyVillain(request: Observable<VillainById>): Observable<Villain> { | ||
const hero$ = new Subject<Villain>(); | ||
|
||
const onNext = (villainById: VillainById) => { | ||
const item = this.villains.find(({ id }) => id === villainById.id); | ||
hero$.next(item); | ||
}; | ||
const onComplete = () => hero$.complete(); | ||
request.subscribe(onNext, null, onComplete); | ||
|
||
return hero$.asObservable(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { MicroserviceOptions, Transport } from '@nestjs/microservices'; | ||
import { join } from 'path'; | ||
import { AppModule } from './app.module'; | ||
|
||
export async function createApp() { | ||
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, { | ||
transport: Transport.GRPC, | ||
options: { | ||
url: '0.0.0.0:8080', | ||
package: 'hero', | ||
protoPath: join(__dirname, '../hero.proto'), | ||
}, | ||
}); | ||
|
||
return app; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,44 @@ | ||
import { SampleService } from './sample-service'; | ||
import { createApp } from './nestjs-project/main'; | ||
import { INestMicroservice } from '@nestjs/common'; | ||
import { ClientGrpc } from '@nestjs/microservices'; | ||
import { HeroService } from './hero'; | ||
import { Observable } from 'rxjs'; | ||
import { Hero } from '../nestjs-metadata/hero'; | ||
|
||
describe('nestjs-simple-test', () => { | ||
it('compiles', () => { | ||
const service = new SampleService(); | ||
expect(service).not.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('nestjs-simple-test nestjs', () => { | ||
let app: INestMicroservice; | ||
let client: ClientGrpc; | ||
let heroService: HeroService; | ||
|
||
beforeAll(async () => { | ||
app = await createApp(); | ||
client = app.get('HERO_PACKAGE'); | ||
heroService = client.getService<HeroService>('HeroService'); | ||
await app.listenAsync(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
it('should get grpc client', async () => { | ||
expect(client).not.toBeUndefined(); | ||
}); | ||
|
||
it('should get heroService', async () => { | ||
expect(heroService).not.toBeUndefined(); | ||
}); | ||
|
||
it('should findOneHero', async() => { | ||
const hero = await (heroService.findOneHero({ id: 1 }) as unknown as Observable<Hero>).toPromise(); | ||
expect(hero).toEqual({ id: 1, name: 'Stephenh' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters