github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/_examples/golang-nodejs/client/client.gen.mjs (about)

     1  // example  v0.0.1 07d79ad7e0e7bc2320ac29fdd065307ce932cf47
     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  // WebRPC description and code-gen version
     7  export const WebRPCVersion = "v1"
     8  
     9  // Schema version of your RIDL schema
    10  export const WebRPCSchemaVersion = " v0.0.1"
    11  
    12  // Schema hash generated from your RIDL schema
    13  export const WebRPCSchemaHash = "07d79ad7e0e7bc2320ac29fdd065307ce932cf47"
    14  
    15  
    16  //
    17  // Types
    18  //
    19  
    20  export var Kind;
    21  (function (Kind) {
    22    Kind["USER"] = "USER"
    23    Kind["ADMIN"] = "ADMIN"
    24  })(Kind || (Kind = {}))
    25  
    26  export class Empty {
    27    constructor(_data) {
    28      this._data = {}
    29      if (_data) {
    30        
    31      }
    32    }
    33    
    34    toJSON() {
    35      return this._data
    36    }
    37  }
    38  
    39  export class GetUserRequest {
    40    constructor(_data) {
    41      this._data = {}
    42      if (_data) {
    43        this._data['userID'] = _data['userID']
    44        
    45      }
    46    }
    47    get userID() {
    48      return this._data['userID']
    49    }
    50    set userID(value) {
    51      this._data['userID'] = value
    52    }
    53    
    54    toJSON() {
    55      return this._data
    56    }
    57  }
    58  
    59  export class User {
    60    constructor(_data) {
    61      this._data = {}
    62      if (_data) {
    63        this._data['id'] = _data['id']
    64        this._data['USERNAME'] = _data['USERNAME']
    65        this._data['created_at'] = _data['created_at']
    66        
    67      }
    68    }
    69    get id() {
    70      return this._data['id']
    71    }
    72    set id(value) {
    73      this._data['id'] = value
    74    }
    75    get USERNAME() {
    76      return this._data['USERNAME']
    77    }
    78    set USERNAME(value) {
    79      this._data['USERNAME'] = value
    80    }
    81    get created_at() {
    82      return this._data['created_at']
    83    }
    84    set created_at(value) {
    85      this._data['created_at'] = value
    86    }
    87    
    88    toJSON() {
    89      return this._data
    90    }
    91  }
    92  
    93  export class RandomStuff {
    94    constructor(_data) {
    95      this._data = {}
    96      if (_data) {
    97        this._data['meta'] = _data['meta']
    98        this._data['metaNestedExample'] = _data['metaNestedExample']
    99        this._data['namesList'] = _data['namesList']
   100        this._data['numsList'] = _data['numsList']
   101        this._data['doubleArray'] = _data['doubleArray']
   102        this._data['listOfMaps'] = _data['listOfMaps']
   103        this._data['listOfUsers'] = _data['listOfUsers']
   104        this._data['mapOfUsers'] = _data['mapOfUsers']
   105        this._data['user'] = _data['user']
   106        
   107      }
   108    }
   109    get meta() {
   110      return this._data['meta']
   111    }
   112    set meta(value) {
   113      this._data['meta'] = value
   114    }
   115    get metaNestedExample() {
   116      return this._data['metaNestedExample']
   117    }
   118    set metaNestedExample(value) {
   119      this._data['metaNestedExample'] = value
   120    }
   121    get namesList() {
   122      return this._data['namesList']
   123    }
   124    set namesList(value) {
   125      this._data['namesList'] = value
   126    }
   127    get numsList() {
   128      return this._data['numsList']
   129    }
   130    set numsList(value) {
   131      this._data['numsList'] = value
   132    }
   133    get doubleArray() {
   134      return this._data['doubleArray']
   135    }
   136    set doubleArray(value) {
   137      this._data['doubleArray'] = value
   138    }
   139    get listOfMaps() {
   140      return this._data['listOfMaps']
   141    }
   142    set listOfMaps(value) {
   143      this._data['listOfMaps'] = value
   144    }
   145    get listOfUsers() {
   146      return this._data['listOfUsers']
   147    }
   148    set listOfUsers(value) {
   149      this._data['listOfUsers'] = value
   150    }
   151    get mapOfUsers() {
   152      return this._data['mapOfUsers']
   153    }
   154    set mapOfUsers(value) {
   155      this._data['mapOfUsers'] = value
   156    }
   157    get user() {
   158      return this._data['user']
   159    }
   160    set user(value) {
   161      this._data['user'] = value
   162    }
   163    
   164    toJSON() {
   165      return this._data
   166    }
   167  }
   168  
   169    
   170  //
   171  // Client
   172  //
   173  
   174  export class ExampleService {
   175    constructor(hostname, fetch) {
   176      this.path = '/rpc/ExampleService/'
   177      this.hostname = hostname
   178      this.fetch = fetch
   179    }
   180  
   181    url(name) {
   182      return this.hostname + this.path + name
   183    }
   184    
   185    ping = (headers) => {
   186      return this.fetch(
   187        this.url('Ping'),
   188        createHTTPRequest({}, headers)
   189      ).then((res) => {
   190        return buildResponse(res).then(_data => {
   191          return {
   192            status: (_data.status)
   193          }
   194        })
   195      })
   196    }
   197    
   198    getUser = (args, headers) => {
   199      return this.fetch(
   200        this.url('GetUser'),
   201        createHTTPRequest(args, headers)
   202      ).then((res) => {
   203        return buildResponse(res).then(_data => {
   204          return {
   205            user: new User(_data.user)
   206          }
   207        })
   208      })
   209    }
   210    
   211  }
   212  
   213    
   214  const createHTTPRequest = (body = {}, headers = {}) => {
   215    return {
   216      method: 'POST',
   217      headers: { ...headers, 'Content-Type': 'application/json' },
   218      body: JSON.stringify(body || {})
   219    }
   220  }
   221  
   222  const buildResponse = (res) => {
   223    return res.text().then(text => {
   224      let data
   225      try {
   226        data = JSON.parse(text)
   227      } catch(err) {
   228        throw { code: 'unknown', msg: `expecting JSON, got: ${text}`, status: res.status }
   229      }
   230      if (!res.ok) {
   231        throw data // webrpc error response
   232      }
   233      return data
   234    })
   235  }