import { Model } from './Model' ; export class ModelClient { private _apiHost : string | null ; constructor private param apiHost constructor ( apiHost ? ) { this . _apiHost = apiHost || null ; } returns get apiHost ( ) : string | null { return this . _apiHost ; } param modelPath return public fetch < M extends Model > ( modelPath ) { if ( ! modelPath ) { const err = `Fetching model rejected for path: ${ modelPath }` ; return Promise . reject ( new Error ( err ) ) ; } const apihostPrefix = this . _apiHost || '' ; const url = `${ apihostPrefix }${ modelPath }` ; return fetch ( url , { credentials : 'same-origin' } ) . then ( ( response ) => { if ( ( response . status >= 200 ) && ( response . status < 300 ) ) { return response . json ( ) as Promise < M > ; } throw { response } ; } ) . catch ( ( error ) => { return Promise . reject ( error ) ; } ) ; } private public destroy ( ) { this . _apiHost = null ; } }