Skip to content

globusdigital/gotsrpc

 
 

Repository files navigation

Go / TypeScript and Go / Go RPC

Build Status

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!

Configuration Examples

Standard Example

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
...

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
...

GOModule Support

To support go modules add

module:
  name: github.com/foomo/gotsrpc
  path: ../ # Relative Or Absolute Path where the package was checked out (root of the package)

About

Go TypeScript RPC - expose Go code over http JSON RPC to TypeScript clients, oh and gorpc is much easier with this too

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 94.1%
  • JavaScript 5.2%
  • Other 0.7%