Skip to content
Go TypeScript RPC - expose Go code over http JSON RPC to TypeScript clients, oh and gorpc is much easier with this too
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd
config
demo
prometheus
.gitignore
.goreleaser.yml
.travis.yml
Makefile
README.md
build.go
client.go
code.go
glide.lock
glide.yaml
go.go
gorpc.go
gotsrpc.go
gotsrpc_test.go
http.go
http_test.go
model.go
php.go
reader_test.go
servicereader.go
stats.go
transport.go
typereader.go
typescript.go
typescript_test.go
typescriptclient.go
typscriptclientasync.go

README.md

Go / TypeScript and Go / Go RPC

Installation

From source to /usr/local/bin/gotsrpc:

go get github.com/foomo/gotsrpc
cd $GOPATH/src/github.com/foomo/gotsrpc
make install

If you trust us there are precompiled versions:

releases

On the mac:

brew install foomo/gotsrpc/gotsrpc

Usage

gotsrpc gotsrpc.yml

Will generate client and server side go and TypeScript code. Have fun!

config examples

commonjs

demo/config.yml

---
modulekind: commonjs
# if you want an async api vs classic callbacks - here you are
tsclientflavor: async
targets:
  demo:
    services:
      /service/foo: Foo
      /service/demo: Demo
    package: github.com/foomo/gotsrpc/demo
    out: /tmp/test.ts
    gorpc:
      - Foo
      - Demo
    tsrpc:
      - Foo
      - Demo

mappings:
  github.com/foomo/gotsrpc/demo:
    out: /tmp/test-files-demo.ts
  github.com/foomo/gotsrpc/demo/nested:
    out: /tmp/test-files-demo-nested.ts
...

an async example

How to use async clients in this case with axios:

import axios, { AxiosResponse } from "axios";
import { ServiceClient as ExampleClient } from "./some/generated/client";

// axios transport
let getTransport = endpoint => async <T>(method, args = []) => {
	return new Promise<T>(async (resolve, reject) => {
		try {
			let axiosPromise: AxiosResponse<T> = await axios.post<T>(
				endpoint + "/" + encodeURIComponent(method),
				JSON.stringify(args),
			);
			return resolve(axiosPromise.data);
		} catch (e) {
			return reject(e);
		}
	});
};

let client = new ExampleClient(getTransport(ExampleClient.defaultEndpoint));

export async function test() {
	try {
		let result = await client.getResult();
		console.log("here is the result", result);
	} catch (e) {
		// e => network?
		// e => json
		// e => domain error type
		console.error("something went wrong ...", e);
	}
}

oldschool TypeScript

demo/config.yml

---
targets:
  demo:
    module: GoTSRPC.Demo
    services:
      /service/foo: Foo
      /service/demo: Demo
    package: github.com/foomo/gotsrpc/demo
    out: /tmp/test.ts
    gorpc:
      - Foo
      - Demo
    tsrpc:
      - Foo
      - Demo

mappings:
  github.com/foomo/gotsrpc/demo:
    module: GoTSRPC.Demo
    out: /tmp/test-files-demo.ts
  github.com/foomo/gotsrpc/demo/nested:
    module: GoTSRPC.Demo.Nested
    out: /tmp/test-files-demo-nested.ts
...
You can’t perform that action at this time.