Go to file
Kevin Franklin Kim 42557c9a93
Merge pull request #31 from globusdigital/v1
feat: derive TS map key type from map.value if empty
2022-08-18 10:31:01 +02:00
cmd support for floats and ints as keys in map types 2020-02-10 15:23:48 +01:00
config feat: add support of go-modules for gotsrpc 2019-05-28 10:43:38 +02:00
demo TSRPC Call Cleanup (#23) 2020-03-17 16:56:14 +01:00
prometheus feat: update go-modules and add objectives to summary vectors (#21) 2019-10-25 12:32:32 +02:00
.gitignore Add .idea to gitignore 2017-09-28 11:42:11 +02:00
.goreleaser.yml chore: remove go mod tidy 2020-02-11 13:28:28 +01:00
.travis.yml ci: bump go version 2021-04-29 13:38:31 +02:00
build.go fix: set correct target path when using gomodules 2019-07-12 14:47:34 +02:00
client_test.go feat: add setting of custom headers to clients 2019-11-20 14:48:05 +01:00
client.go TSRPC Call Cleanup (#23) 2020-03-17 16:56:14 +01:00
code.go async client flavor added 2018-06-12 14:09:46 +02:00
go.go feat: load arguments only if arguments are present 2020-03-29 20:34:11 +02:00
go.mod go mod tidy 2020-04-07 14:24:45 +02:00
go.sum chore: go mod tidy 2021-04-29 13:37:56 +02:00
gorpc.go fixed types 2016-12-17 15:55:32 +01:00
gotsrpc_test.go improved interface{} support and refactored arg loading 2017-07-25 08:46:42 +02:00
gotsrpc.go chore: remove error print 2020-03-27 21:10:44 +01:00
http_test.go feat: add http factory to inject global configurators 2018-12-20 11:53:04 +01:00
http.go chore: force connection re-establishment for LB 2019-11-20 14:17:50 +01:00
instrumentation_test.go feat: update go-modules and add objectives to summary vectors (#21) 2019-10-25 12:32:32 +02:00
instrumentation.go feat: update go-modules and add objectives to summary vectors (#21) 2019-10-25 12:32:32 +02:00
Makefile revert: fix goreleaser version 2021-04-29 13:38:17 +02:00
model.go feat: Go 1.18 compatibility because of missing any type 2022-08-16 16:50:05 +02:00
php.go Change comment information for gotsrpc to conform to generated file std 2017-09-28 11:43:04 +02:00
reader_test.go feat: remove interface from entry creation 2019-11-20 14:47:38 +01:00
README.md added travis build status 2020-02-11 10:39:07 +01:00
servicereader.go feat: Go 1.18 compatibility because of missing any type 2022-08-16 16:50:05 +02:00
stats.go added response and request size to stats 2017-01-27 16:59:03 +01:00
transport.go go modules, no longer attempting to set promoted field in literal 2019-07-23 10:21:24 +02:00
typereader.go feat: Go 1.18 compatibility because of missing any type 2022-08-16 16:50:05 +02:00
typescript_test.go async client flavor added 2018-06-12 14:09:46 +02:00
typescript.go feat: derive TS map key type from map.value if empty 2022-08-17 15:30:51 +02:00
typescriptclient.go added support for *ast.ArrayType, *ast.MapType 2018-09-02 18:09:04 +02:00
typscriptclientasync.go fixing code for calls without a single return value 2018-11-16 10:16:51 +01:00

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)