import { Messages } from "./Messages" ; /**
 * Configuration service
 */ class ConfigurationService { private static configuration = { } ; param field public getConfig ( field ) { return ConfigurationService . configuration [ field ] ; } param config public setConfig ( config ) { ConfigurationService . configuration = config ; } param url public configure ( url ) { return ConfigurationService . fetchSameOrigin ( url ) . then ( ConfigurationService . onConfigResponse ) . then ( ( ) => ConfigurationService . fetchReferencedConfigurations ( ) ) ; } private static getConfiguration ( ) { return ConfigurationService . configuration ; } private static fetchReferencedConfigurations ( ) { return ConfigurationService . fetchSameOrigin ( ConfigurationService . getConfiguration ( ) [ "messages" ] ) . then ( Messages . onMessagesResponse ) ; } private static fetchSameOrigin ( url ) { return new Promise ( ( resolve , reject ) => { const xhr = new XMLHttpRequest ( ) ; xhr . onload = function ( ) { resolve ( new Response ( xhr . responseText , { status : xhr . status } ) ) ; } ; xhr . onerror = function ( ) { reject ( new TypeError ( "Same origin request failed" ) ) ; } ; xhr . open ( "GET" , url ) ; xhr . send ( null ) ; } ) ; } private static onConfigResponse ( response ) { return response . json ( ) . then ( ConfigurationService . onConfigParsed ) ; } private static onConfigParsed ( result ) { ConfigurationService . configuration = result [ "config" ] ; } } export const Configuration = new ConfigurationService ( ) ;