npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grpc-ts-protoc-gen

v0.1.1

Published

Protoc Plugin for TypeScript Declarations for gRPC

Downloads

29

Readme

ts-grpc-protoc-gen

Protoc Plugin for generating TypeScript Declarations for gRPC

This repository contains a protoc plugin that generates TypeScript declarations (.d.ts files) that match the JavaScript output of protoc --js_out=import_style=commonjs,binary --grpc_out. This plugin depends on ts-protoc-gen to also be run.

This plugin is tested and written using TypeScript 2.7.

Installation

  • Install the standard C++ implementation of protocol buffers from developers.google.com/protocol-buffers
  • Install this package using npm, eg: npm install grpc-ts-protoc-gen or clone, install and build this repository.

Usage

As mention above, this plugin for protoc serves two purposes:

  1. Generating gRPC TypeScript Definitions for CommonJS modules generated by protoc

Generating TypeScript Definitions for CommonJS modules generated by protoc

By default, protoc will generate ES5 code when the --js_out flag is used (see javascript compiler documentation). You have the choice of two module syntaxes, CommonJS or closure. This plugin (ts-grpc-protoc-gen) can be used to generate Typescript definition files (.d.ts) to provide type hints for CommonJS modules only.

To generate TypeScript definitions you must first configure protoc to use this plugin and then specify where you want the TypeScript definitions to be written to using the --grpc-ts_out flag.

PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"
PROTOC_GEN_GRPC_PATH=""
# Path to this plugin
PROTOC_GEN_GRPC_TS_PATH="./node_modules/bin/protoc-gen-grpc-ts"

# Directory to write generated code to (.js and .d.ts files) 
OUT_DIR="./generated"

protoc \
    --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
    --plugin="protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH}"
    --plugin="protoc-gen-grpc-ts=${PROTOC_GEN_GRPC_TS_PATH}" \
    --js_out="import_style=commonjs,binary:${OUT_DIR}" \
    --ts_out="${OUT_DIR}" \
    --grpc_out="${OUT_DIR}" \
    --grpc-ts_out="${OUT_DIR}" \
    users.proto base.proto

In the above example, the generated folder will contain both .js and .d.ts files which you can reference in your TypeScript project to get full type completion and make use of ES6-style import statements, eg:

import grpc from "grpc";
import { GetUserRequest } from "../generated/users_pb";
import { UserServiceClient } from "../generated/users_grpc_pb";

const client = new UserServiceClient('localhost', grpc.credentials.createInsecure());
const req = new GetUserRequest();
req.setUsername("johndoe");
client.getUser(req, (err, user) => { /* ... */ });

Contributing

Contributions are welcome! Please refer to CONTRIBUTING.md for more information.