import { HttpConnection , HttpResponse } from "./web-client" ; import { delay } from '../util/primise' ; export class FetchConnection implements HttpConnection { constructor ( private url ) { } get ( path ) { const url = this . url ; async function callFetch ( ) { const response = await fetch ( url + path ) ; const body = await response . json ( ) ; return body ; } async function timeout ( ) { await delay ( 1000 ) ; throw new Error ( 'Timeout in login.' ) ; } return Promise . race ( [ callFetch ( ) , timeout ( ) ] ) ; } async post ( path , body , timeoutSeconds ) { const response = await fetch ( this . url + path , { method : 'POST' , body : JSON . stringify ( body ) , headers : { 'Content-Type' : 'application/json' } } ) ; const json = await response . json ( ) ; return { result : 'success' , response : json } ; } }