You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // @flow
  2. import { parseURLParams } from '../config';
  3. import { toState } from '../redux';
  4. import { DEFAULT_SERVER_URL } from './constants';
  5. /**
  6. * Returns the effective value of a configuration/preference/setting by applying
  7. * a precedence among the values specified by JWT, URL, settings,
  8. * and config.
  9. *
  10. * @param {Object|Function} stateful - The redux state object or
  11. * {@code getState} function.
  12. * @param {string} propertyName - The name of the
  13. * configuration/preference/setting (property) to retrieve.
  14. * @param {{
  15. * config: boolean,
  16. * jwt: boolean,
  17. * settings: boolean,
  18. * urlParams: boolean
  19. * }} [sources] - A set/structure of {@code boolean} flags indicating the
  20. * configuration/preference/setting sources to consider/retrieve values from.
  21. * @returns {any}
  22. */
  23. export function getPropertyValue(
  24. stateful: Object | Function,
  25. propertyName: string,
  26. sources?: Object
  27. ) {
  28. // Default values don't play nicely with partial objects and we want to make
  29. // the function easy to use without exhaustively defining all flags:
  30. sources = { // eslint-disable-line no-param-reassign
  31. // Defaults:
  32. config: true,
  33. jwt: true,
  34. settings: true,
  35. urlParams: true,
  36. ...sources
  37. };
  38. // Precedence: jwt -> urlParams -> settings -> config.
  39. const state = toState(stateful);
  40. // jwt
  41. if (sources.jwt) {
  42. const value = state['features/base/jwt'][propertyName];
  43. if (typeof value !== 'undefined') {
  44. return value[propertyName];
  45. }
  46. }
  47. // urlParams
  48. if (sources.urlParams) {
  49. const urlParams
  50. = parseURLParams(state['features/base/connection'].locationURL);
  51. const value = urlParams[`config.${propertyName}`];
  52. if (typeof value !== 'undefined') {
  53. return value;
  54. }
  55. }
  56. // settings
  57. if (sources.settings) {
  58. const value = state['features/base/settings'][propertyName];
  59. if (typeof value !== 'undefined') {
  60. return value;
  61. }
  62. }
  63. // config
  64. if (sources.config) {
  65. const value = state['features/base/config'][propertyName];
  66. if (typeof value !== 'undefined') {
  67. return value;
  68. }
  69. }
  70. return undefined;
  71. }
  72. /**
  73. * Gets the currently configured server URL.
  74. *
  75. * @param {Object|Function} stateful - The redux state object or
  76. * {@code getState} function.
  77. * @returns {string} - The currently configured server URL.
  78. */
  79. export function getServerURL(stateful: Object | Function) {
  80. const state = toState(stateful);
  81. return state['features/base/settings'].serverURL || DEFAULT_SERVER_URL;
  82. }