All files / src/utility localStorage.js

78.38% Statements 29/37
62.5% Branches 10/16
87.5% Functions 7/8
78.38% Lines 29/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 7715x 15x   15x 15x 15x 15x                 1x 1x 1x 1x     1x       9x 9x 1x 1x     9x                             1x 1x           21x 19x     2x     2x 1x 1x     1x     7x 6x 6x     1x      
const isSupported = () => {
  const test = 'localStorage';
 
  try {
    localStorage.setItem(test, test);
    localStorage.removeItem(test);
    return true;
  } catch (e) {
    return false;
  }
};
 
export default {
  isSupported: isSupported(),
  getAuthData() {
    let auth = null;
    const authStorage = this.getItem('auth');
    Eif (authStorage) {
      auth = JSON.parse(authStorage);
    }
 
    return auth;
  },
  getAuthToken() {
    let token;
    const authStorage = this.getItem('auth');
    if (authStorage) {
      const auth = JSON.parse(authStorage);
      token = auth.access_token;
    }
 
    return token;
  },
  getAuthHeader() {
    let header;
    const authStorage = this.getItem('auth');
    if (authStorage) {
      const auth = JSON.parse(authStorage);
      if (auth.token_type === 'bearer') {
        header = `Bearer ${auth.access_token}`;
      }
    }
 
    return header;
  },
  getAllKeys() {
    Eif (this.isSupported) {
      return Object.keys(localStorage);
    }
 
    return null;
  },
  getItem(key) {
    if (this.isSupported) {
      return localStorage.getItem(key);
    }
 
    return null;
  },
  removeItem(key) {
    if (this.isSupported) {
      localStorage.removeItem(key);
      return this.getItem(key);
    }
 
    return null;
  },
  setItem(key, value) {
    if (this.isSupported) {
      localStorage.setItem(key, value);
      return this.getItem(key);
    }
 
    return null;
  },
};