AlexDaSoul / nestjs-proto-gen-ts

Generate TypeScript interfaces for Nest.js gRPC from Protobuf files
MIT License
74 stars 28 forks source link

Add option to map the return values for the methods into Promises #2

Closed VictorGaiva closed 3 years ago

VictorGaiva commented 3 years ago

This makes possible to use the generate values to validade the controllers in NestJS, like this:

export namespace database {
    export interface NetworkService {
        getNetworkById(data: NetworkById, metadata?: Metadata): Promise<Network>;
    }
    export interface NetworkById {
        id?: string;
    }
    export interface Network {
        id?: number;
    }
}

@Controller()
export class NetworkService implements database.NetworkService{
    async getNetworkById({ id }: database.NetworkById, metadata?:Metadata) {
        return await this.database.GetById(id);
    }
}
AlexDaSoul commented 3 years ago

GrpcMethod by Nestjs always returns Rxjs Observable. But You can overraide compiller template use --templateparam and copy template from https://github.com/AlexDaSoul/nestjs-proto-gen-ts/blob/master/templates/nestjs-grpc.hbs and change Observable to Promise.

$ tsproto --path ./hero-proto --template ./templates/new-nestjs-grpc.hbs