mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
| .github/workflows | ||
| cmd/gotsrpc | ||
| config | ||
| example | ||
| prometheus | ||
| .gitignore | ||
| .golangci.yaml | ||
| .goreleaser.yml | ||
| build.go | ||
| client_test.go | ||
| client.go | ||
| clienterror.go | ||
| code.go | ||
| error.go | ||
| go.go | ||
| go.mod | ||
| go.sum | ||
| gorpc.go | ||
| gotsrpc_test.go | ||
| gotsrpc.go | ||
| http_test.go | ||
| http.go | ||
| httperror.go | ||
| instrumentation_test.go | ||
| instrumentation.go | ||
| Makefile | ||
| model.go | ||
| monitor.go | ||
| README.md | ||
| responsewriter.go | ||
| servicereader.go | ||
| stats.go | ||
| timeext.go | ||
| transport.go | ||
| typereader.go | ||
| typescript_test.go | ||
| typescript.go | ||
| typscriptclient.go | ||
| unionext.go | ||
Go / TypeScript and Go / Go RPC
Installation
From source to /usr/local/bin/gotsrpc:
go get github.com/foomo/gotsrpc/v2
cd $GOPATH/src/github.com/foomo/gotsrpc
make install
If you trust us there are precompiled versions:
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
targets:
demo:
services:
/service/foo: Foo
/service/demo: Demo
package: github.com/foomo/gotsrpc/v2/demo
out: /tmp/test.ts
gorpc:
- Foo
- Demo
tsrpc:
- Foo
- Demo
mappings:
github.com/foomo/gotsrpc/v2/demo:
out: /tmp/test-files-demo.ts
github.com/foomo/gotsrpc/v2/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);
}
}
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)