protoc-gen-typescript-http

command module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2023 License: MIT Imports: 9 Imported by: 0

README

protoc-gen-typescript-http

Generates Typescript types and service clients from protobuf definitions annotated with http rules. The generated types follow the canonical JSON encoding.

Experimental: This library is under active development and breaking changes to config files, APIs and generated code are expected between releases.

Using the plugin

For examples of correctly annotated protobuf defintions and the generated code, look at examples.

Install the plugin
go get github.com/moecasts/protoc-gen-typescript-http

Or download a prebuilt binary from releases.

Invocation
protoc 
  --typescript-http_out [OUTPUT DIR] \
  --typescript-http_opt use_enum_numbers=true,use_multi_line_comment=true
  [.proto files ...]
Support options
// UseProtoNames controls the casing of generated field names.
// If set to true, fields will use proto names (typically snake_case).
// If omitted or set to false, fields will use JSON names (typically camelCase).
use_proto_names: bool

// UseEnumNumbers emits enum values as numbers.
use_enum_numbers: bool

// The method names of service methods naming case.
// Only work when `UseEnumNumbers=true`
// opt:
// camelcase: convert name to lower camel case like `camelCase`
// pascalcase: convert name to pascalcase like `PascalCase`
// default is pascalcase
enum_field_naming: string

// Generate comments as multiline comments.
// multiline comments: /** ... */
// single line comments: // ...
use_multi_line_comment: bool

// force add `undefined` to message field.
// default true
force_message_field_undefinable: bool

// If set to true, body will be JSON.stringify before send
// default true
use_body_stringify: bool

// The method names of service methods naming case.
// opt:
// camelcase: convert name to lower camel case like `camelCase`
// pascalcase: convert name to pascalcase like `PascalCase`
// default is pascalcase
service_method_naming: 'camelcase' | 'pascalcase'

// If set to true, field int64 and uint64 will convert to string
force_long_as_string: bool

The generated clients can be used with any HTTP client that returns a Promise containing JSON data.

const rootUrl = "...";

type Request = {
  path: string,
  method: string,
  body: string | null
}

// This is optional
type RequestOptions = {
  useCache?: boolean;
}

function fetchRequestHandler({path, method, body}: Request & RequestOptions) {
  return fetch(rootUrl + path, {method, body}).then(response => response.json())
}

export function siteClient() {
  // This Generics is optional
  return createShipperServiceClient<RequestOptions>(fetchRequestHandler);
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL