-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: grpc-js support for nestjs (#307)
* fix: grpc-js support for nestjs * fix: code style * fix: update snapshots * Re-codegen. * Remove js files. * Remove out. * Remove test.ts. * Update snapshot. Co-authored-by: Stephen Haberman <stephen.haberman@gmail.com>
- Loading branch information
Showing
7 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
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,27 @@ | ||
syntax = "proto3"; | ||
|
||
package hero; | ||
|
||
service HeroService { | ||
rpc FindOneHero (HeroById) returns (Hero) {} | ||
rpc FindOneVillain (VillainById) returns (Villain) {} | ||
rpc FindManyVillain (stream VillainById) returns (stream Villain) {} | ||
} | ||
|
||
message HeroById { | ||
int32 id = 1; | ||
} | ||
|
||
message VillainById { | ||
int32 id = 1; | ||
} | ||
|
||
message Hero { | ||
int32 id = 1; | ||
string name = 2; | ||
} | ||
|
||
message Villain { | ||
int32 id = 1; | ||
string name = 2; | ||
} |
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,68 @@ | ||
/* eslint-disable */ | ||
import { GrpcMethod, GrpcStreamMethod } from '@nestjs/microservices'; | ||
import { util, configure } from 'protobufjs/minimal'; | ||
import * as Long from 'long'; | ||
import { Observable } from 'rxjs'; | ||
import { Metadata } from '@grpc/grpc-js'; | ||
|
||
export const protobufPackage = 'hero'; | ||
|
||
export interface HeroById { | ||
id: number; | ||
} | ||
|
||
export interface VillainById { | ||
id: number; | ||
} | ||
|
||
export interface Hero { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
export interface Villain { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
export const HERO_PACKAGE_NAME = 'hero'; | ||
|
||
export interface HeroServiceClient { | ||
findOneHero(request: HeroById, metadata?: Metadata): Observable<Hero>; | ||
|
||
findOneVillain(request: VillainById, metadata?: Metadata): Observable<Villain>; | ||
|
||
findManyVillain(request: Observable<VillainById>, metadata?: Metadata): Observable<Villain>; | ||
} | ||
|
||
export interface HeroServiceController { | ||
findOneHero(request: HeroById, metadata?: Metadata): Promise<Hero> | Observable<Hero> | Hero; | ||
|
||
findOneVillain(request: VillainById, metadata?: Metadata): Promise<Villain> | Observable<Villain> | Villain; | ||
|
||
findManyVillain(request: Observable<VillainById>, metadata?: Metadata): Observable<Villain>; | ||
} | ||
|
||
export function HeroServiceControllerMethods() { | ||
return function (constructor: Function) { | ||
const grpcMethods: string[] = ['findOneHero', 'findOneVillain']; | ||
for (const method of grpcMethods) { | ||
const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); | ||
GrpcMethod('HeroService', method)(constructor.prototype[method], method, descriptor); | ||
} | ||
const grpcStreamMethods: string[] = ['findManyVillain']; | ||
for (const method of grpcStreamMethods) { | ||
const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); | ||
GrpcStreamMethod('HeroService', method)(constructor.prototype[method], method, descriptor); | ||
} | ||
}; | ||
} | ||
|
||
export const HERO_SERVICE_NAME = 'HeroService'; | ||
|
||
// If you get a compile-error about 'Constructor<Long> and ... have no overlap', | ||
// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. | ||
if (util.Long !== Long) { | ||
util.Long = Long as any; | ||
configure(); | ||
} |
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,8 @@ | ||
import { SampleService } from './sample-service'; | ||
|
||
describe('nestjs-metadata-test', () => { | ||
it('compiles', () => { | ||
const service = new SampleService(); | ||
expect(service).not.toBeUndefined(); | ||
}); | ||
}); |
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 @@ | ||
nestJs=true,addGrpcMetadata=true,outputServices=grpc-js |
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,25 @@ | ||
import { HeroServiceController, HeroById, Hero, Villain, VillainById } from './hero'; | ||
import { Metadata } from 'grpc'; | ||
import { Observable, Subject } from 'rxjs'; | ||
|
||
export class SampleService implements HeroServiceController { | ||
findOneHero(request: HeroById, metadata?: Metadata): Promise<Hero> { | ||
return Promise.resolve({ id: 1, name: 'test' }); | ||
} | ||
|
||
findOneVillain(request: VillainById, metadata?: Metadata): Promise<Villain> { | ||
return Promise.resolve({ 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