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

     1  /* tslint:disable */
     2  // hello-webrpc v1.0.0 87ce8159bce3ad056518dfb1f1877b1a1012b34d
     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 = "87ce8159bce3ad056518dfb1f1877b1a1012b34d"
    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    created_at?: 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    findUsers(args: FindUsersArgs, headers?: object): Promise<FindUsersReturn>
    42  }
    43  
    44  export interface PingArgs {
    45  }
    46  
    47  export interface PingReturn {
    48    status: boolean  
    49  }
    50  export interface GetUserArgs {
    51    userID: number
    52  }
    53  
    54  export interface GetUserReturn {
    55    user: User  
    56  }
    57  export interface FindUsersArgs {
    58    q: string
    59  }
    60  
    61  export interface FindUsersReturn {
    62    page: Page
    63    users: Array<User>  
    64  }
    65  
    66  
    67    
    68  //
    69  // Client
    70  //
    71  export class ExampleService implements ExampleService {
    72    private hostname: string
    73    private fetch: Fetch
    74    private path = '/rpc/ExampleService/'
    75  
    76    constructor(hostname: string, fetch: Fetch) {
    77      this.hostname = hostname
    78      this.fetch = fetch
    79    }
    80  
    81    private url(name: string): string {
    82      return this.hostname + this.path + name
    83    }
    84    
    85    ping = (headers?: object): Promise<PingReturn> => {
    86      return this.fetch(
    87        this.url('Ping'),
    88        createHTTPRequest({}, headers)
    89        ).then((res) => {
    90        return buildResponse(res).then(_data => {
    91          return {
    92            status: <boolean>(_data.status)
    93          }
    94        })
    95      })
    96    }
    97    
    98    getUser = (args: GetUserArgs, headers?: object): Promise<GetUserReturn> => {
    99      return this.fetch(
   100        this.url('GetUser'),
   101        createHTTPRequest(args, headers)).then((res) => {
   102        return buildResponse(res).then(_data => {
   103          return {
   104            user: <User>(_data.user)
   105          }
   106        })
   107      })
   108    }
   109    
   110    findUsers = (args: FindUsersArgs, headers?: object): Promise<FindUsersReturn> => {
   111      return this.fetch(
   112        this.url('FindUsers'),
   113        createHTTPRequest(args, headers)).then((res) => {
   114        return buildResponse(res).then(_data => {
   115          return {
   116            page: <Page>(_data.page), 
   117            users: <Array<User>>(_data.users)
   118          }
   119        })
   120      })
   121    }
   122    
   123  }
   124  
   125    
   126  export interface WebRPCError extends Error {
   127    code: string
   128    msg: string
   129  	status: number
   130  }
   131  
   132  const createHTTPRequest = (body: object = {}, headers: object = {}): object => {
   133    return {
   134      method: 'POST',
   135      headers: { ...headers, 'Content-Type': 'application/json' },
   136      body: JSON.stringify(body || {})
   137    }
   138  }
   139  
   140  const buildResponse = (res: Response): Promise<any> => {
   141    return res.text().then(text => {
   142      let data
   143      try {
   144        data = JSON.parse(text)
   145      } catch(err) {
   146        throw { code: 'unknown', msg: `expecting JSON, got: ${text}`, status: res.status } as WebRPCError
   147      }
   148      if (!res.ok) {
   149        throw data // webrpc error response
   150      }
   151      return data
   152    })
   153  }
   154  
   155  export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>