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.2KB

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