github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/_examples/node-ts/webapp/client.gen.ts (about) 1 /* tslint:disable */ 2 // node-ts v1.0.0 4d2858fa129683e5775e9b863ceceb740e7e09b1 3 // -- 4 // This file has been generated by https://github.com/webrpc/webrpc using gen/typescript 5 // Do not edit by hand. Update your webrpc schema and re-generate. 6 7 // WebRPC description and code-gen version 8 export const WebRPCVersion = "v1" 9 10 // Schema version of your RIDL schema 11 export const WebRPCSchemaVersion = "v1.0.0" 12 13 // Schema hash generated from your RIDL schema 14 export const WebRPCSchemaHash = "4d2858fa129683e5775e9b863ceceb740e7e09b1" 15 16 17 // 18 // Types 19 // 20 export enum Kind { 21 USER = 'USER', 22 ADMIN = 'ADMIN' 23 } 24 25 export interface User { 26 id: number 27 USERNAME: string 28 role: Kind 29 meta: {[key: string]: any} 30 31 createdAt?: string 32 } 33 34 export interface Page { 35 num: number 36 } 37 38 export interface ExampleService { 39 ping(headers?: object): Promise<PingReturn> 40 getUser(args: GetUserArgs, headers?: object): Promise<GetUserReturn> 41 } 42 43 export interface PingArgs { 44 } 45 46 export interface PingReturn { 47 status: boolean 48 } 49 export interface GetUserArgs { 50 userID: number 51 } 52 53 export interface GetUserReturn { 54 code: number 55 user: User 56 } 57 58 59 60 // 61 // Client 62 // 63 export class ExampleService implements ExampleService { 64 private hostname: string 65 private fetch: Fetch 66 private path = '/rpc/ExampleService/' 67 68 constructor(hostname: string, fetch: Fetch) { 69 this.hostname = hostname 70 this.fetch = fetch 71 } 72 73 private url(name: string): string { 74 return this.hostname + this.path + name 75 } 76 77 ping = (headers?: object): Promise<PingReturn> => { 78 return this.fetch( 79 this.url('Ping'), 80 createHTTPRequest({}, headers) 81 ).then((res) => { 82 return buildResponse(res).then(_data => { 83 return { 84 status: <boolean>(_data.status) 85 } 86 }) 87 }) 88 } 89 90 getUser = (args: GetUserArgs, headers?: object): Promise<GetUserReturn> => { 91 return this.fetch( 92 this.url('GetUser'), 93 createHTTPRequest(args, headers)).then((res) => { 94 return buildResponse(res).then(_data => { 95 return { 96 code: <number>(_data.code), 97 user: <User>(_data.user) 98 } 99 }) 100 }) 101 } 102 103 } 104 105 106 export interface WebRPCError extends Error { 107 code: string 108 msg: string 109 status: number 110 } 111 112 const createHTTPRequest = (body: object = {}, headers: object = {}): object => { 113 return { 114 method: 'POST', 115 headers: { ...headers, 'Content-Type': 'application/json' }, 116 body: JSON.stringify(body || {}) 117 } 118 } 119 120 const buildResponse = (res: Response): Promise<any> => { 121 return res.text().then(text => { 122 let data 123 try { 124 data = JSON.parse(text) 125 } catch(err) { 126 throw { code: 'unknown', msg: `expecting JSON, got: ${text}`, status: res.status } as WebRPCError 127 } 128 if (!res.ok) { 129 throw data // webrpc error response 130 } 131 return data 132 }) 133 } 134 135 export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>