mirror of
https://github.com/JuLi0n21/goTsClient.git
synced 2026-06-05 03:06:28 +00:00
add broadcasts to api clie
nt
This commit is contained in:
+30
-21
@@ -15,9 +15,9 @@ var backendClient string
|
||||
|
||||
var errorType = reflect.TypeOf((*error)(nil)).Elem()
|
||||
|
||||
func GenClient(api any, outPutPath string) error {
|
||||
func GenClient(api any, broadcasts any, outPutPath string) error {
|
||||
|
||||
ts, err := GenerateTS(reflect.TypeOf(api), "RPCClient")
|
||||
ts, err := GenerateTS(reflect.TypeOf(api), reflect.TypeOf(broadcasts), "RPCClient")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -27,20 +27,39 @@ func GenClient(api any, outPutPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GenerateTS(apiType reflect.Type, clientName string) (string, error) {
|
||||
func GenerateTS(apiType reflect.Type, broadcastType reflect.Type, clientName string) (string, error) {
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString("// --- AUTO-GENERATED ---\n")
|
||||
b.WriteString("// Generated by generator. Do not edit by hand (unless you know what you do).\n")
|
||||
b.WriteString("// Types\n\n")
|
||||
|
||||
structs := map[string]reflect.Type{}
|
||||
|
||||
b.WriteString("export interface BroadcastEvents {\n")
|
||||
|
||||
if broadcastType != nil {
|
||||
bt := broadcastType
|
||||
if bt.Kind() == reflect.Ptr {
|
||||
bt = bt.Elem()
|
||||
}
|
||||
|
||||
if bt.Kind() == reflect.Struct {
|
||||
for i := 0; i < bt.NumField(); i++ {
|
||||
f := bt.Field(i)
|
||||
topicName := f.Name
|
||||
tag := f.Tag.Get("json")
|
||||
if tag != "" {
|
||||
topicName = strings.Split(tag, ",")[0]
|
||||
}
|
||||
collectStructs(f.Type, structs)
|
||||
b.WriteString(fmt.Sprintf(" '%s': %s;\n", topicName, goTypeToTS(f.Type)))
|
||||
}
|
||||
}
|
||||
}
|
||||
b.WriteString("}\n\n")
|
||||
|
||||
b.WriteString("export type BroadcastTopic = keyof BroadcastEvents;\n\n")
|
||||
|
||||
for method := range apiType.Methods() {
|
||||
i := -1
|
||||
for param := range method.Type.Ins() {
|
||||
i++
|
||||
//skip self
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -48,14 +67,11 @@ func GenerateTS(apiType reflect.Type, clientName string) (string, error) {
|
||||
}
|
||||
|
||||
outParams := method.Type.NumOut()
|
||||
|
||||
unsupportedMethod := "not supported, allowed layout are \nfunc() (error) \nfunc() (struct, error), \nfunc(x...) (struct, error)"
|
||||
//func()
|
||||
|
||||
if outParams == 0 {
|
||||
return "", errors.New(method.Name + unsupportedMethod)
|
||||
}
|
||||
|
||||
//func() (err)
|
||||
if outParams == 1 {
|
||||
if method.Type.Out(0).Implements(errorType) {
|
||||
collectStructs(method.Type.Out(0), structs)
|
||||
@@ -63,23 +79,18 @@ func GenerateTS(apiType reflect.Type, clientName string) (string, error) {
|
||||
return "", errors.New(method.Name + unsupportedMethod)
|
||||
}
|
||||
}
|
||||
|
||||
//func() (struct, err)
|
||||
if outParams > 1 && method.Type.Out(1).Implements(errorType) {
|
||||
collectStructs(method.Type.Out(0), structs)
|
||||
}
|
||||
|
||||
if outParams > 2 {
|
||||
return "", errors.New(method.Name + unsupportedMethod)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
names := make([]string, 0, len(structs))
|
||||
for n := range structs {
|
||||
names = append(names, n)
|
||||
}
|
||||
|
||||
slices.Sort(names)
|
||||
|
||||
for _, name := range names {
|
||||
@@ -106,7 +117,6 @@ func GenerateTS(apiType reflect.Type, clientName string) (string, error) {
|
||||
b.WriteString("}\n\n")
|
||||
}
|
||||
|
||||
b.WriteString("// Generic RPC method result\n")
|
||||
b.WriteString("export type RPCResult<T> = { data: T; error?: any };\n\n")
|
||||
|
||||
b.WriteString(fmt.Sprintf("export interface %s {\n", clientName))
|
||||
@@ -141,7 +151,7 @@ func GenerateTS(apiType reflect.Type, clientName string) (string, error) {
|
||||
if m.Type.NumOut() > 0 {
|
||||
resType = goTypeToTS(m.Type.Out(0))
|
||||
if m.Type.Out(0).Implements(errorType) {
|
||||
resType = "any" // It's just a func() error
|
||||
resType = "any"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +178,6 @@ func GenerateTS(apiType reflect.Type, clientName string) (string, error) {
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
func collectStructs(t reflect.Type, m map[string]reflect.Type) {
|
||||
if t == nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user