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 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @flow
  2. import { parseURLParams } from '../config';
  3. import { toState } from '../redux';
  4. /**
  5. * Returns the effective value of a property by applying a precedence
  6. * between values in URL, config and profile.
  7. *
  8. * @param {Object|Function} stateful - The redux state object or function
  9. * to retreive the state.
  10. * @param {string} propertyName - The name of the property we need.
  11. * @param {{
  12. * ignoreJWT: boolean,
  13. * ignoreUrlParams: boolean,
  14. * ignoreProfile: boolean,
  15. * ignoreConfig: boolean
  16. * }} precedence - A structure of booleans to set which property sources
  17. * should be ignored.
  18. * @returns {any}
  19. */
  20. export function getPropertyValue(
  21. stateful: Object | Function,
  22. propertyName: string,
  23. precedence: Object = {
  24. ignoreJWT: false,
  25. ignoreUrlParams: false,
  26. ignoreProfile: false,
  27. ignoreConfig: false
  28. }
  29. ) {
  30. const state = toState(stateful);
  31. const jwt = state['features/base/jwt'];
  32. const urlParams
  33. = parseURLParams(state['features/base/connection'].locationURL);
  34. const profile = state['features/base/profile'];
  35. const config = state['features/base/config'];
  36. const urlParamName = `config.${propertyName}`;
  37. // Precedence: jwt -> urlParams -> profile -> config
  38. if (
  39. !precedence.ignoreJWT
  40. && typeof jwt[propertyName] !== 'undefined'
  41. ) {
  42. return jwt[propertyName];
  43. }
  44. if (
  45. !precedence.ignoreUrlParams
  46. && typeof urlParams[urlParamName] !== 'undefined'
  47. ) {
  48. return urlParams[urlParamName];
  49. }
  50. if (
  51. !precedence.ignoreProfile
  52. && typeof profile[propertyName] !== 'undefined'
  53. ) {
  54. return profile[propertyName];
  55. }
  56. if (!precedence.ignoreConfig) {
  57. return config[propertyName];
  58. }
  59. return undefined;
  60. }