Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { getCookie, setCookie, removeCookie, hasCookieSupport } from '@analytics/cookie-utils'
import hasLocalStorageSupport from './hasLocalStorage'
import parse from './utils/parse'
import globalContext from './utils/globalContext'
// Constants
const LOCAL_STORAGE = 'localStorage'
const COOKIE = 'cookie'
const GLOBAL = 'global'
// Verify support
const hasStorage = hasLocalStorageSupport()
const hasCookies = hasCookieSupport()
/**
* Get storage item from localStorage, cookie, or window
* @param {string} key - key of item to get
* @param {object|string} [options] - storage options. If string location of where to get storage
* @param {string} [options.storage] - Define type of storage to pull from.
* @return {Any} the value of key
*/
export function getItem(key, options = {}) {
if (!key) return null
const storageType = getStorageType(options)
// Get value from all locations
if (storageType === 'all') return getAll(key)
/* 1. Try localStorage */
if (useLocal(storageType)) {
const value = localStorage.getItem(key)