Skip to content

Commit

Permalink
fix: grpc-js support for nestjs (#307)
Browse files Browse the repository at this point in the history
* 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
jsbrain and stephenh authored Jul 11, 2021
1 parent 0ccb76a commit d11c8c2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
Binary file added integration/nestjs-metadata-grpc-js/hero.bin
Binary file not shown.
27 changes: 27 additions & 0 deletions integration/nestjs-metadata-grpc-js/hero.proto
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;
}
68 changes: 68 additions & 0 deletions integration/nestjs-metadata-grpc-js/hero.ts
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();
}
8 changes: 8 additions & 0 deletions integration/nestjs-metadata-grpc-js/nestjs-metadata-test.ts
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();
});
});
1 change: 1 addition & 0 deletions integration/nestjs-metadata-grpc-js/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nestJs=true,addGrpcMetadata=true,outputServices=grpc-js
25 changes: 25 additions & 0 deletions integration/nestjs-metadata-grpc-js/sample-service.ts
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();
}
}
6 changes: 4 additions & 2 deletions src/generate-nestjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { assertInstanceOf, FormattedMethodDescriptor, maybeAddComment, singular
import { camelCase } from './case';
import { Context } from './context';

const Metadata = imp('Metadata@grpc');

export function generateNestjsServiceController(
ctx: Context,
fileDesc: FileDescriptorProto,
Expand All @@ -25,6 +23,8 @@ export function generateNestjsServiceController(
const { options } = ctx;
const chunks: Code[] = [];

const Metadata = options.outputServices === 'grpc-js' ? imp('Metadata@@grpc/grpc-js') : imp('Metadata@grpc');

maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);
const t = options.context ? `<${contextTypeVar}>` : '';
chunks.push(code`
Expand Down Expand Up @@ -96,6 +96,8 @@ export function generateNestjsServiceClient(
const { options } = ctx;
const chunks: Code[] = [];

const Metadata = options.outputServices === 'grpc-js' ? imp('Metadata@@grpc/grpc-js') : imp('Metadata@grpc');

maybeAddComment(sourceInfo, chunks);
const t = options.context ? `<${contextTypeVar}>` : ``;
chunks.push(code`
Expand Down

0 comments on commit d11c8c2

Please sign in to comment.