All files / src/utilities clientEnvVars.jsx

100% Statements 10/10
100% Branches 1/1
100% Functions 3/3
100% Lines 9/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25              1x   1x 3x 3x 3x   8x   3x 3x     3x        
/*
 * This file takes in process environment config and returns an array of variable that
 * are prefixed with SPARTACUS_ in a JSON.stringify format
 * Inspired by Razzle build-time varaibles
 * https://github.com/jaredpalmer/razzle/blob/86a6057c240b496fdf372f142e108c12cf7250f8/packages/razzle/config/env.js#L56-L93
 */
 
const SPARTACUS = /^SPARTACUS_/i;
 
const getClientEnvVars = (envConfig = { parsed: {} }) => {
  const { parsed } = envConfig;
  const envVars = Object.keys(parsed);
  const clientEnvVars = {};
 
  const prefixedEnvVars = envVars.filter(key => SPARTACUS.test(key));
 
  prefixedEnvVars.forEach(variable => {
    clientEnvVars[variable] = JSON.stringify(parsed[variable]);
  });
 
  return clientEnvVars;
};
 
export default getClientEnvVars;