All files / utils config.js

95% Statements 19/20
93.75% Branches 15/16
100% Functions 5/5
95% Lines 19/20
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 26 27 28 29 30 31 32 33 34 35 361x 1x   1x 7x 7x 1x 6x 6x 6x 6x   6x 5x 4x 4x 3x 3x   1x       1x     1x                  
let configFile = null;
let lastFilename = null;
 
const getConfig = (filename = 'storybook-config.json') => (
  new Promise((resolve, reject) => {
    if (lastFilename === filename && configFile) {
      resolve(configFile);
    } else Eif (window && window.parent) {
      lastFilename = filename;
      const url = window.parent.location;
      const location = `${url.protocol}//${url.hostname}:${url.port}/${filename}`;
 
      fetch(location).then((response) => {
        if (response.ok) {
          response.json().then((data) => {
            if (data && data.storybook && data.storybook.versions) {
              configFile = data.storybook.versions;
              resolve(configFile);
            } else {
              reject('Invalid config');
            }
          });
        } else {
          reject('Response not ok');
        }
      }).catch(() => {
        reject('Error getting config');
      });
    } else {
      reject('Window not found');
    }
  })
);
 
export default getConfig;