github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/_examples/hello-webrpc/webapp/client.gen.js (about) 1 // hello-webrpc v1.0.0 c929128d878e94653f3a856f80c4671008e22a45 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 = "v1.0.0" 11 12 // Schema hash generated from your RIDL schema 13 export const WebRPCSchemaHash = "c929128d878e94653f3a856f80c4671008e22a45" 14 15 16 // 17 // Types 18 // 19 20 var Kind; 21 (function (Kind) { 22 Kind["USER"] = "USER" 23 Kind["ADMIN"] = "ADMIN" 24 })(Kind || (Kind = {})) 25 26 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 class User { 40 constructor(_data) { 41 this._data = {} 42 if (_data) { 43 this._data['id'] = _data['id'] 44 this._data['USERNAME'] = _data['USERNAME'] 45 this._data['created_at'] = _data['created_at'] 46 47 } 48 } 49 get id() { 50 return this._data['id'] 51 } 52 set id(value) { 53 this._data['id'] = value 54 } 55 get USERNAME() { 56 return this._data['USERNAME'] 57 } 58 set USERNAME(value) { 59 this._data['USERNAME'] = value 60 } 61 get created_at() { 62 return this._data['created_at'] 63 } 64 set created_at(value) { 65 this._data['created_at'] = value 66 } 67 68 toJSON() { 69 return this._data 70 } 71 } 72 73 74 // 75 // Client 76 // 77 78 class ExampleService { 79 constructor(hostname, fetch) { 80 this.path = '/rpc/ExampleService/' 81 this.hostname = hostname 82 this.fetch = fetch 83 } 84 85 url(name) { 86 return this.hostname + this.path + name 87 } 88 89 ping = (headers) => { 90 return this.fetch( 91 this.url('Ping'), 92 createHTTPRequest({}, headers) 93 ).then((res) => { 94 return buildResponse(res).then(_data => { 95 return { 96 status: (_data.status) 97 } 98 }) 99 }) 100 } 101 102 getUser = (args, headers) => { 103 return this.fetch( 104 this.url('GetUser'), 105 createHTTPRequest(args, headers) 106 ).then((res) => { 107 return buildResponse(res).then(_data => { 108 return { 109 user: new User(_data.user) 110 } 111 }) 112 }) 113 } 114 115 } 116 117 118 const createHTTPRequest = (body = {}, headers = {}) => { 119 return { 120 method: 'POST', 121 headers: { ...headers, 'Content-Type': 'application/json' }, 122 body: JSON.stringify(body || {}) 123 } 124 } 125 126 const buildResponse = (res) => { 127 return res.text().then(text => { 128 let data 129 try { 130 data = JSON.parse(text) 131 } catch(err) { 132 throw { code: 'unknown', msg: `expecting JSON, got: ${text}`, status: res.status } 133 } 134 if (!res.ok) { 135 throw data // webrpc error response 136 } 137 return data 138 }) 139 }