forked from webrpc/gen-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go.tmpl
47 lines (42 loc) · 1.19 KB
/
client.go.tmpl
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
{{define "client"}}
{{- $typeMap := .TypeMap -}}
{{- if .Services}}
//
// Client
//
{{- range .Services}}
export class {{.Name}} implements {{.Name}} {
protected hostname: string
protected fetch: Fetch
protected path = '/rpc/{{.Name}}/'
constructor(hostname: string, fetch: Fetch) {
this.hostname = hostname
this.fetch = (input: RequestInfo, init?: RequestInit) => fetch(input, init)
}
private url(name: string): string {
return this.hostname + this.path + name
}
{{range $_, $method := .Methods}}
{{firstLetterToLower .Name}} = ({{template "methodInputs" dict "Method" . "TypeMap" $typeMap}}): Promise<{{$method.Name}}Return> => {
return this.fetch(
this.url('{{.Name}}'),
{{- if .Inputs | len}}
createHTTPRequest(args, headers)
{{- else}}
createHTTPRequest({}, headers)
{{end -}}
).then((res) => {
return buildResponse(res).then(_data => {
return {
{{- range $i, $output := .Outputs -}}{{if gt $i 0}}, {{end}}
{{$output.Name}}: <{{template "type" dict "Type" $output.Type "TypeMap" $typeMap}}>(_data.{{$output.Name}})
{{- end}}
}
})
})
}
{{end}}
}
{{end -}}
{{end -}}
{{end}}