Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: grpc-js support for nestjs #307

Merged
merged 8 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { maybeAddComment, singular } from './utils';
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 @@ -97,6 +97,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