github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/gen/typescript/templates/client_helpers.ts.tmpl (about) 1 {{define "client_helpers"}} 2 export interface WebRPCError extends Error { 3 code: string 4 msg: string 5 status: number 6 } 7 8 const createHTTPRequest = (body: object = {}, headers: object = {}): object => { 9 return { 10 method: 'POST', 11 headers: { ...headers, 'Content-Type': 'application/json' }, 12 body: JSON.stringify(body || {}) 13 } 14 } 15 16 const buildResponse = (res: Response): Promise<any> => { 17 return res.text().then(text => { 18 let data 19 try { 20 data = JSON.parse(text) 21 } catch(err) { 22 throw { code: 'unknown', msg: `expecting JSON, got: ${text}`, status: res.status } as WebRPCError 23 } 24 if (!res.ok) { 25 throw data // webrpc error response 26 } 27 return data 28 }) 29 } 30 31 export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response> 32 {{end}}