github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/_examples/hello-webrpc-with-goschema-js/webapp/client.gen.js (about)

     1  //   
     2  // --
     3  // This file has been generated by https://github.com/webrpc/webrpc using gen/javascript
     4  // Do not edit by hand. Update your webrpc schema and re-generate.
     5  
     6  
     7  
     8  //
     9  // Types
    10  //
    11  
    12  var Kind;
    13  (function (Kind) {
    14  })(Kind || (Kind = {}))
    15  
    16  class Empty {
    17    constructor(_data) {
    18      this._data = {}
    19      if (_data) {
    20        
    21      }
    22    }
    23    
    24    toJSON() {
    25      return this._data
    26    }
    27  }
    28  
    29  class User {
    30    constructor(_data) {
    31      this._data = {}
    32      if (_data) {
    33        this._data['ID'] = _data['ID']
    34        this._data['username'] = _data['username']
    35        this._data['createdAt'] = _data['createdAt']
    36        
    37      }
    38    }
    39    get ID() {
    40      return this._data['ID']
    41    }
    42    set ID(value) {
    43      this._data['ID'] = value
    44    }
    45    get username() {
    46      return this._data['username']
    47    }
    48    set username(value) {
    49      this._data['username'] = value
    50    }
    51    get createdAt() {
    52      return this._data['createdAt']
    53    }
    54    set createdAt(value) {
    55      this._data['createdAt'] = value
    56    }
    57    
    58    toJSON() {
    59      return this._data
    60    }
    61  }
    62  
    63    
    64  //
    65  // Client
    66  //
    67  
    68  class ExampleService {
    69    constructor(hostname, fetch) {
    70      this.path = '/rpc/ExampleService/'
    71      this.hostname = hostname
    72      this.fetch = fetch
    73    }
    74  
    75    url(name) {
    76      return this.hostname + this.path + name
    77    }
    78    
    79    getUser = (args, headers) => {
    80      return this.fetch(
    81        this.url('GetUser'),
    82        createHTTPRequest(args, headers)
    83      ).then((res) => {
    84        return buildResponse(res).then(_data => {
    85          return {
    86            user: (_data.user)
    87          }
    88        })
    89      })
    90    }
    91    
    92    ping = (headers) => {
    93      return this.fetch(
    94        this.url('Ping'),
    95        createHTTPRequest({}, headers)
    96      ).then((res) => {
    97        return buildResponse(res).then(_data => {
    98          return {
    99            bool: (_data.bool)
   100          }
   101        })
   102      })
   103    }
   104    
   105  }
   106  
   107    
   108  const createHTTPRequest = (body = {}, headers = {}) => {
   109    return {
   110      method: 'POST',
   111      headers: { ...headers, 'Content-Type': 'application/json' },
   112      body: JSON.stringify(body || {})
   113    }
   114  }
   115  
   116  const buildResponse = (res) => {
   117    return res.text().then(text => {
   118      let data
   119      try {
   120        data = JSON.parse(text)
   121      } catch(err) {
   122        throw { code: 'unknown', msg: `expecting JSON, got: ${text}`, status: res.status }
   123      }
   124      if (!res.ok) {
   125        throw data // webrpc error response
   126      }
   127      return data
   128    })
   129  }