github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/_examples/hello-webrpc-ts/webapp/src/index.ts (about) 1 // 2 // hello-webrpc-ts example program. 3 // -- 4 // see the `hello-api.ridl` schema where the API is defined 5 // and the client.gen.ts file is code-generated which we use below.. 6 // 7 import { fetch as polyfetch } from 'whatwg-fetch' 8 import * as client from './client.gen' 9 10 const api = new client.ExampleService('http://127.0.01:4242', polyfetch) 11 12 async function main() { 13 14 // 15 // Ping the API 16 // 17 console.log('[A] webrpc -- calling Ping() rpc method (expecting true):') 18 19 try { 20 const resp = await api.ping() 21 console.log('[A]', resp.status) 22 } catch (err) { 23 console.log('[A]', {err}) 24 } 25 26 // 27 // Get a user from the API 28 // 29 console.log('[B] webrpc -- calling GetUser() rpc method (expecting User object):') 30 31 try { 32 const { user }: client.GetUserReturn = await api.getUser({ userID: 966 }) 33 console.log('[B]', user) 34 console.log('[B] welcome user ID', user.id, 'with username', user.USERNAME) 35 } catch (err) { 36 console.log('[B]', {err}) 37 } 38 39 // 40 // Get an unknown user from the API -- we expect a 404 from the server 41 // 42 console.log('[C] webrpc -- calling GetUser() rpc method of an unknown user (expecting a 404):') 43 44 try { 45 const resp = await api.getUser({ userID: 911 }) 46 console.log('[C]', resp.user) 47 } catch (err) { 48 console.log('[C]', {err}) 49 } 50 51 // 52 // Get a user from the API -- different code syntax example 53 // 54 console.log('[D] webrpc -- calling GetUser() rpc method (expecting User object):') 55 56 await api.getUser({ userID: 1337 }).then(resp => { 57 const user = resp.user 58 console.log('[D]', user) 59 const meta = user.meta 60 console.log('[D]', {meta}) 61 console.log('[D] welcome user ID', user.id, 'with username', user.USERNAME) 62 }).catch(err => { 63 console.log('[D]', {err}) 64 }) 65 66 // 67 // Find users from the API 68 // 69 try { 70 const resp = await api.findUsers({ q: 'a-z' }) 71 console.log('[E]', {resp}) 72 } catch (err) { 73 console.log('[E]', {err}) 74 } 75 76 77 } 78 79 main()