github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/gen/typescript/templates/client.ts.tmpl (about)

     1  {{define "client"}}
     2  {{- if .Services}}
     3  //
     4  // Client
     5  //
     6  
     7  {{- range .Services}}
     8  export class {{.Name}} implements {{.Name | serviceInterfaceName}} {
     9    private hostname: string
    10    private fetch: Fetch
    11    private path = '/rpc/{{.Name}}/'
    12  
    13    constructor(hostname: string, fetch: Fetch) {
    14      this.hostname = hostname
    15      this.fetch = fetch
    16    }
    17  
    18    private url(name: string): string {
    19      return this.hostname + this.path + name
    20    }
    21    {{range .Methods}}
    22    {{.Name | methodName}} = ({{. | methodInputs}}): {{. | methodOutputs}} => {
    23      return this.fetch(
    24        this.url('{{.Name}}'),
    25        {{- if .Inputs | len}}
    26        createHTTPRequest(args, headers)
    27        {{- else}}
    28        createHTTPRequest({}, headers)
    29        {{end -}}
    30      ).then((res) => {
    31        return buildResponse(res).then(_data => {
    32          return {
    33          {{- $outputsCount := .Outputs|len -}}
    34          {{- range $i, $output := .Outputs}}
    35            {{$output | newOutputArgResponse}}{{listComma $i $outputsCount}}
    36          {{- end}}
    37          }
    38        })
    39      })
    40    }
    41    {{end}}
    42  }
    43  {{end -}}
    44  {{end -}}
    45  {{end}}