Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-proto-gen-ts

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-proto-gen-ts

Generate TypeScript interfaces for Nest.js gRPC from proto files

  • 1.0.21
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Nest.js TypeScript generator from Protobuf

Generate TypeScript code from proto files.

Circle CI npm Licence MIT

This package uses the protobufjs library to auto-generate TypeScript or JavaScript code using handlebars templates.

The package does not rely on the `protoc' compiler and generates TypeScript code directly, rather than outputting types from the generated JavaScript code. This makes it easy to use the automatically generated code because it does not need to be compiled at the time of creation.

Installation

$ npm install nestjs-proto-gen-ts

Example

Protobuf file hero-proto/hero.proto:

syntax = "proto3";

package hero;

service HeroesService {
  rpc FindOne (HeroById) returns (Hero) {}
}

message HeroById {
  int32 id = 1;
}

message Hero {
  int32 id = 1;
  string name = 2;
}

Generate interfaces:

$ tsproto --path ./hero-proto

Output:

import { Observable } from 'rxjs';
import { Metadata } from '@grpc/grpc-js';

export namespace hero {
    export interface HeroesService {
        findOne(data: HeroById, metadata?: Metadata): Observable<Hero>;
    }
    export interface HeroById {
        id?: number;
    }
    export interface Hero {
        id?: number;
        name?: string;
    }
}

Controller:

...
import { hero } from 'hero-proto/hero';

type HeroById = hero.HeroById;

@Controller()
export class HeroesController implements hero.HeroesService {
  @GrpcMethod('HeroesService', 'FindOne')
  findOne(data: HeroById, meta: Metadata): Observable<hero.Hero> {
    const items = [
      { id: 1, name: 'John' },
      { id: 2, name: 'Doe' },
    ];

    return items.find(({ id }) => id === data.id);
  }
}

Client:

...
import { hero } from 'hero-proto/hero';

@Injectable()
export class AppService implements OnModuleInit {
  private heroesService: hero.HeroesService;

  constructor(@Inject('HERO_PACKAGE') private client: ClientGrpc) {}

  onModuleInit() {
    this.heroesService = this.client.getService<hero.HeroesService>('HeroesService');
  }

  getHero(): Observable<string> {
    return this.heroesService.findOne({ id: 1 });
  }
}

Usage

Base usage:

$ tsproto --path grpc-proto

Output dir:

$ tsproto --path grpc-proto --output any-dir

Target files:

$ tsproto --path grpc-proto --target one.proto two.proto

Ignore directories or files:

$ tsproto --path grpc-proto --ignore grpc-proto/ignore-dir

Custom handlebar's template for output:

$ tsproto --path grpc-proto --template custom-template.hbs

Options

The following options are available:

  --version, -v   Show version number                                  [boolean]
  --help, -h      Show help                                            [boolean]
  --path, -p      Path to root directory                      [array] [required]
  --output, -o    Path to output directory                              [string]
  --template      Handlebar's template for output
                                 [string] [default: "templates/nestjs-grpc.hbs"]
  --target, -t    Proto files                      [array] [default: [".proto"]]
  --ignore, -i    Ignore file or directories
                                      [array] [default: ["node_modules","dist"]]
  --comments, -c  Add comments from proto              [boolean] [default: true]
  --verbose       Log all output to console            [boolean] [default: true]

Keywords

FAQs

Package last updated on 02 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc