Skip to content

schmidt-sebastian/gapic-generator-typescript

 
 

Repository files navigation

API Client Generator for TypeScript

Background

This tool is a TypeScript client library generator for network APIs specified by protocol buffers (including, but not limited to, client libraries for Google Cloud APIs).

It can be used for any API that uses protocol buffers and follows the conventions described in API Improvement Proposals.

Getting started

The Showcase API is a good API to play with if you want to start generating your own client libraries. It has several services, we'll use Echo service as an example.

Proto definitions for Echo service

Take a look at echo.proto that describes a simple network service.

Download the proto files locally (the following examples work in Linux or macOS):

$ curl -L https://github.com/googleapis/gapic-showcase/releases/download/v0.6.1/gapic-showcase-0.6.1-protos.tar.gz | tar xz

Run the generator to generate TypeScript libraries

The easiest way to get started is to use our Docker image:

$ mkdir showcase-typescript
$ docker run --rm \
  --mount type=bind,source=`pwd`/google/showcase/v1beta1,destination=/in/google/showcase/v1beta1,readonly \
  --mount type=bind,source=`pwd`/showcase-typescript,destination=/out \
  gcr.io/gapic-images/gapic-generator-typescript:latest

The resulting files are in showcase-typescript folder:

$ cd showcase-typescript
$ npm install  # install dependencies
$ npm run fix  # format the code
$ npm test     # run unit tests

Start the server that serves an API

Download the pre-compiled binary for your operating system from the Releases page and start the binary, or just use Docker:

# in another window
$ docker run -p 7469:7469/tcp -p 7469:7469/udp -p 1337:1337/tcp --rm gcr.io/gapic-images/gapic-showcase:0.6.1

Write your JavaScript or TypeScript code that uses the server!

Assuming you have Node.js installed and node binary in your path:

$ node
Welcome to Node.js v12.1.0.
Type ".help" for more information.
> const showcase = require('.')  // assuming you're in showcase-typescript
undefined
> const client = new showcase.EchoClient({ sslCreds: require('@grpc/grpc-js').credentials.createInsecure() })
undefined
> client.echo({ content: 'hello world!' }).then(console.log)
Promise { <pending> }
> [ { content: 'hello world!' }, undefined, undefined ]

Want to know more?

Read the AIPs or just create an issue in this repository if you have questions! We support some cool things such as streaming RPCs, auto-pagination for certain types of RPCs, and long running operations.

Disclaimer

This is not an official Google product.

About

Generate Typescript API client libraries from Protocol Buffers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 52.3%
  • HTML 32.7%
  • JavaScript 12.8%
  • Shell 1.6%
  • Dockerfile 0.6%