rpcgen
is a JSON-RPC 2.0 client library generator for zenrpc. It supports client generation for following languages:
- Dart
- Golang
- PHP
- TypeScript
- Swift
- OpenRPC schema
package main
import (
"fmt"
"log"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/zenrpc/v2"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
generated, err := rpcgen.FromSMD(rpc.SMD()).GoClient().Generate()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", generated)
}
package main
import (
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/dart"
"github.com/vmkteam/rpcgen/v2/golang"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/zenrpc/v2"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
http.HandleFunc("/client.go", rpcgen.Handler(gen.GoClient(golang.Settings{})))
http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(nil)))
http.HandleFunc("/RpcClient.php", rpcgen.Handler(gen.PHPClient("")))
http.HandleFunc("/client.swift", rpcgen.Handler(gen.SwiftClient(swift.Settings{})))
http.HandleFunc("/client.dart", rpcgen.Handler(gen.DartClient(dart.Settings{ Part: "client"})))
}
package main
import (
"fmt"
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/typescript"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
typeMapper := func(in smd.JSONSchema, tsType typescript.Type) typescript.Type {
if in.Type == "object" {
if in.Description == "Group" && in.Name == "groups" {
tsType.Type = fmt.Sprintf("Record<number, I%s>", in.Description)
}
}
return tsType
}
http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(typeMapper)))
}
package main
import (
"fmt"
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
typeMapper := func(typeName string, in smd.Property, param swift.Parameter) swift.Parameter {
switch typeName {
case "Group":
switch in.Name {
case "groups":
param.Type = fmt.Sprintf("[Int: %s]", param.Type)
param.DecodableDefault = swift.DefaultMap
}
}
return param
}
http.HandleFunc("/client.swift", rpcgen.Handler(gen.SwiftClient(swift.Settings{"", typeMapper})))
}
package main
import (
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/dart"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
typeMapper := func(in smd.JSONSchema, param dart.Parameter) dart.Parameter {
if in.Type == smd.Object {
switch in.TypeName {
case "Time", "Date":
param.Type = "String"
}
}
if in.Type == smd.Array {
switch in.TypeName {
case "[]Date", "[]Time":
param.Type = "List<String>"
param.ReturnType = "List<String>"
}
}
return param
}
http.HandleFunc("/client.dart", rpcgen.Handler(gen.DartClient(dart.Settings{Part: "client", TypeMapper: typeMapper})))
}