-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtypscriptclient.go
More file actions
140 lines (125 loc) · 3.58 KB
/
typscriptclient.go
File metadata and controls
140 lines (125 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gotsrpc
import (
"fmt"
"strconv"
"strings"
"github.com/foomo/gotsrpc/v2/config"
)
func renderTypescriptClient(service *Service, mappings config.TypeScriptMappings, scalars map[string]*Scalar, structs map[string]*Struct, ts *code) error {
clientName := service.Name + "Client"
ts.l("export class " + clientName + " {")
ts.ind(1)
// ts.l(`static defaultInst = new ` + clientName + `()`)
// ts.l(`constructor(public endpoint = "` + service.Endpoint + `") {}`)
ts.l(`public static defaultEndpoint = "` + service.Endpoint + `";`)
ts.l("constructor(")
ts.ind(1)
ts.l("public transport:<T>(method: string, data?: any[]) => Promise<T>")
ts.ind(-1)
ts.l(") {}")
for _, method := range service.Methods {
ts.app("async " + lcfirst(method.Name) + "(")
var callArgs []string
argOffset := 0
for index, arg := range method.Args {
if index == 0 && arg.Value.isHTTPResponseWriter() {
trace("skipping first arg is a http.ResponseWriter")
argOffset = 1
continue
} else if index == 0 && arg.Value.isContext() {
trace("skipping first arg is a context.Context")
argOffset = 1
continue
}
if index == 1 && arg.Value.isHTTPRequest() {
trace("skipping second arg is a *http.Request")
argOffset = 2
continue
}
}
argCount := 0
for index, arg := range method.Args {
if index < argOffset {
continue
}
if index > argOffset {
ts.app(", ")
}
ts.app(arg.tsName())
ts.app(":")
arg.Value.tsType(mappings, scalars, structs, ts, arg.JSONInfo)
callArgs = append(callArgs, arg.Name)
argCount++
}
ts.app("):")
returnTypeTS := newCode(" ")
returnTypeTS.app("{")
innerReturnTypeTS := newCode(" ")
innerReturnTypeTS.app("{")
firstReturnType := ""
countReturns := 0
countInnerReturns := 0
responseObjectPrefix := ""
responseObject := "return {"
for index, retField := range method.Return {
countInnerReturns++
retArgName := retField.tsName()
if len(retArgName) == 0 {
retArgName = "ret"
if index > 0 {
retArgName += "_" + fmt.Sprint(index)
}
}
if index > 0 {
returnTypeTS.app("; ")
innerReturnTypeTS.app("; ")
}
innerReturnTypeTS.app(strconv.Itoa(index))
innerReturnTypeTS.app(":")
retField.Value.tsType(mappings, scalars, structs, innerReturnTypeTS, retField.JSONInfo)
if index == 0 {
firstReturnTypeTS := newCode(" ")
retField.Value.tsType(mappings, scalars, structs, firstReturnTypeTS, retField.JSONInfo)
firstReturnType = firstReturnTypeTS.string()
}
countReturns++
returnTypeTS.app(retArgName)
returnTypeTS.app(":")
responseObject += responseObjectPrefix + retArgName + " : response[" + strconv.Itoa(index) + "]"
retField.Value.tsType(mappings, scalars, structs, returnTypeTS, retField.JSONInfo)
responseObjectPrefix = ", "
}
responseObject += "};"
returnTypeTS.app("}")
innerReturnTypeTS.app("}")
if countReturns == 0 {
ts.app("Promise<void> {")
} else if countReturns == 1 {
ts.app("Promise<" + firstReturnType + "> {")
} else if countReturns > 1 {
ts.app("Promise<" + returnTypeTS.string() + "> {")
}
ts.nl()
ts.ind(1)
innerCallTypeString := "void"
if countInnerReturns > 0 {
innerCallTypeString = innerReturnTypeTS.string()
}
call := "this.transport<" + innerCallTypeString + ">(\"" + method.Name + "\", [" + strings.Join(callArgs, ", ") + "])"
switch countReturns {
case 0:
ts.l("await " + call)
case 1:
ts.l("return (await " + call + ")[0]")
default:
ts.l("const response = await " + call)
ts.l(responseObject)
}
ts.ind(-1)
ts.app("}")
ts.nl()
}
ts.ind(-1)
ts.l("}")
return nil
}