rpcgen

package module
v2.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2021 License: MIT Imports: 7 Imported by: 3

README

rpcgen: JSON-RPC 2.0 Client Generator Implementation for zenrpc

Go Report Card Go Reference

rpcgen is a JSON-RPC 2.0 client library generator for zenrpc. It supports client generation for following languages:

  • Golang
  • PHP
  • TypeScript

Examples

Basic usage
package main

import (
	"fmt"
	"log"

	"github.com/vmkteam/rpcgen"
	"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)
}
Generate in HTTP handler
package main 

import (
	"net/http"
	
	"github.com/vmkteam/rpcgen"
	"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()))
	http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(nil)))
	http.HandleFunc("/RpcClient.php", rpcgen.Handler(gen.PHPClient("")))
}
Add custom TypeScript type mapper
package main

import (
	"net/http"

	"github.com/vmkteam/rpcgen"
	"github.com/vmkteam/rpcgen/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)))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(gen Generator) func(http.ResponseWriter, *http.Request)

Handler create http handler with fn

Types

type Generator

type Generator interface {
	Generate() ([]byte, error)
}

type RPCGen

type RPCGen struct {
	// contains filtered or unexported fields
}

func FromSMD

func FromSMD(schema smd.Schema) *RPCGen

FromSMD create Generator from smd schema

func FromSMDv1

func FromSMDv1(schema smd1.Schema) *RPCGen

FromSMDv1 create Generator from smd v1 schema

func (RPCGen) GoClient

func (g RPCGen) GoClient() Generator

func (RPCGen) PHPClient added in v2.1.0

func (g RPCGen) PHPClient(phpNamespace string) Generator

func (RPCGen) TSClient

func (g RPCGen) TSClient(typeMapper typescript.TypeMapper) Generator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL