server-config.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Injectable, Injector } from '@angular/core';
  2. import { GetServerConfig } from 'shared/generated-types';
  3. import { GET_SERVER_CONFIG } from './definitions/config-definitions';
  4. import { BaseDataService } from './providers/base-data.service';
  5. export type ServerConfig = GetServerConfig.Config;
  6. export function initializeServerConfigService(serverConfigService: ServerConfigService): () => Promise<any> {
  7. return serverConfigService.init();
  8. }
  9. /**
  10. * A service which fetches the config from the server upon initialization, and then provides that config
  11. * to the components which require it.
  12. */
  13. @Injectable()
  14. export class ServerConfigService {
  15. private _serverConfig: ServerConfig = {} as any;
  16. constructor(private injector: Injector) {}
  17. /**
  18. * Fetches the ServerConfig. Should be run as part of the app bootstrap process by attaching
  19. * to the Angular APP_INITIALIZER token.
  20. */
  21. init(): () => Promise<any> {
  22. const baseDataService = this.injector.get<BaseDataService>(BaseDataService);
  23. return () =>
  24. baseDataService
  25. .query<GetServerConfig.Query>(GET_SERVER_CONFIG)
  26. .single$.toPromise()
  27. .then(
  28. result => {
  29. this._serverConfig = result.config;
  30. },
  31. err => {
  32. // Let the error fall through to be caught by the http interceptor.
  33. },
  34. );
  35. }
  36. get serverConfig(): ServerConfig {
  37. return this._serverConfig;
  38. }
  39. }