Comparing version 1.2.1 to 1.2.2
@@ -1,2 +0,18 @@ | ||
export { useLocalStorage, useSessionStorage } from './useStorage'; | ||
//# sourceMappingURL=index.d.ts.map | ||
declare const useLocalStorage: <T = StoredData>(key: StorageKey, initialValue?: T | undefined, version?: Version | undefined) => { | ||
mergeState: (state: Record<string, unknown>) => void; | ||
resetValue: () => void; | ||
setValue: (value: T) => void; | ||
timestamp?: Date | undefined; | ||
value?: T | undefined; | ||
version?: Version | undefined; | ||
}; | ||
declare const useSessionStorage: <T = StoredData>(key: StorageKey, initialValue?: T | undefined, version?: Version | undefined) => { | ||
mergeState: (state: Record<string, unknown>) => void; | ||
resetValue: () => void; | ||
setValue: (value: T) => void; | ||
timestamp?: Date | undefined; | ||
value?: T | undefined; | ||
version?: Version | undefined; | ||
}; | ||
export { useLocalStorage, useSessionStorage }; |
@@ -1,7 +0,152 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useSessionStorage = exports.useLocalStorage = void 0; | ||
var useStorage_1 = require("./useStorage"); | ||
Object.defineProperty(exports, "useLocalStorage", { enumerable: true, get: function () { return useStorage_1.useLocalStorage; } }); | ||
Object.defineProperty(exports, "useSessionStorage", { enumerable: true, get: function () { return useStorage_1.useSessionStorage; } }); | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
useLocalStorage: () => useLocalStorage, | ||
useSessionStorage: () => useSessionStorage | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/useStorage.ts | ||
var import_react = require("react"); | ||
// src/utils.ts | ||
var isServerSide = () => typeof window === "undefined"; | ||
var isObject = (obj) => { | ||
return !!obj && obj.constructor === Object; | ||
}; | ||
var noop = () => void 0; | ||
var safeGetStorageValue = (storageType, key) => { | ||
if (isServerSide()) | ||
return {}; | ||
try { | ||
const value = window[storageType].getItem(key); | ||
let parsedValue; | ||
if (value) { | ||
const { data, version, ts } = JSON.parse(value); | ||
const timestamp = new Date(ts); | ||
const parsedVersion = typeof version !== "undefined" ? isNaN(Number(version)) ? version : Number(version) : void 0; | ||
parsedValue = { | ||
data, | ||
ts: timestamp, | ||
version: parsedVersion | ||
}; | ||
} | ||
return parsedValue ? parsedValue : {}; | ||
} catch (error) { | ||
console.error(error); | ||
return {}; | ||
} | ||
}; | ||
var safeSetStorageValue = (storageType, key, value, ts, version) => { | ||
if (isServerSide()) | ||
return; | ||
const timestamp = ts || new Date(); | ||
try { | ||
const storedValue = JSON.stringify({ | ||
data: value, | ||
ts: timestamp, | ||
version | ||
}); | ||
window[storageType].setItem(key, storedValue); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
// src/useStorage.ts | ||
var useStorage = (storageType, key, initialValue, version) => { | ||
const ssr = isServerSide(); | ||
const [storedValue, setStoredValue] = (0, import_react.useState)(() => { | ||
if (ssr) | ||
return { data: initialValue }; | ||
const item = safeGetStorageValue(storageType, key); | ||
const hasVersion = typeof version !== "undefined"; | ||
const storedVersionMismatch = hasVersion && (typeof item.version === "undefined" || item.version !== version); | ||
if (storedVersionMismatch) { | ||
window[storageType].removeItem(key); | ||
return { data: initialValue }; | ||
} | ||
return (item == null ? void 0 : item.data) ? item : { data: initialValue }; | ||
}); | ||
const mergeState = (0, import_react.useCallback)((state) => { | ||
const ts = new Date(); | ||
const currentState = isObject(storedValue.data) ? storedValue.data : {}; | ||
const updatedState = Object.assign(currentState, state); | ||
setStoredValue({ | ||
data: updatedState, | ||
ts, | ||
version | ||
}); | ||
safeSetStorageValue(storageType, key, updatedState, ts, version); | ||
}, [key, storageType, storedValue.data, version]); | ||
const resetValue = (0, import_react.useCallback)(() => { | ||
setStoredValue({}); | ||
window[storageType].removeItem(key); | ||
}, [key, storageType]); | ||
const setValue = (0, import_react.useCallback)((value) => { | ||
const ts = new Date(); | ||
setStoredValue({ | ||
data: value, | ||
ts, | ||
version | ||
}); | ||
safeSetStorageValue(storageType, key, value, ts, version); | ||
}, [key, storageType, version]); | ||
(0, import_react.useEffect)(() => { | ||
if (ssr) | ||
return; | ||
const storageEventHandler = (e) => { | ||
const { isTrusted, key: eventKey, newValue } = e; | ||
if (isTrusted && eventKey === key) { | ||
if (newValue) { | ||
const structuredValue = JSON.parse(newValue); | ||
setStoredValue(structuredValue); | ||
} else { | ||
setStoredValue({}); | ||
} | ||
} | ||
}; | ||
window.addEventListener("storage", storageEventHandler); | ||
return () => { | ||
window.removeEventListener("storage", storageEventHandler); | ||
}; | ||
}, [key, ssr]); | ||
if (ssr) { | ||
return { | ||
mergeState: noop, | ||
resetValue: noop, | ||
setValue: noop, | ||
value: initialValue, | ||
version | ||
}; | ||
} | ||
return { | ||
mergeState, | ||
resetValue, | ||
setValue, | ||
timestamp: storedValue == null ? void 0 : storedValue.ts, | ||
value: storedValue == null ? void 0 : storedValue.data, | ||
version: storedValue == null ? void 0 : storedValue.version | ||
}; | ||
}; | ||
var useLocalStorage = (key, initialValue, version) => useStorage("localStorage", key, initialValue, version); | ||
var useSessionStorage = (key, initialValue, version) => useStorage("sessionStorage", key, initialValue, version); | ||
//# sourceMappingURL=index.js.map |
@@ -8,3 +8,3 @@ { | ||
"license": "MIT", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"main": "dist/index.js", | ||
@@ -35,4 +35,3 @@ "types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"build:types": "tsc --emitDeclarationOnly", | ||
"build": "tsup --dts", | ||
"clean": "rm -rf dist", | ||
@@ -43,27 +42,31 @@ "dev": "tsc --watch", | ||
"prepare": "npm run build && husky install", | ||
"test": "jest --coverage" | ||
"test": "jest --coverage", | ||
"types": "tsc -p tsconfig.json --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@testing-library/react-hooks": "7.0.2", | ||
"@types/jest": "27.0.2", | ||
"@types/react": "17.0.24", | ||
"@typescript-eslint/eslint-plugin": "4.32.0", | ||
"@typescript-eslint/parser": "4.32.0", | ||
"eslint": "7.32.0", | ||
"eslint-config-prettier": "8.3.0", | ||
"@testing-library/react-hooks": "8.0.0", | ||
"@types/jest": "27.5.0", | ||
"@types/react": "18.0.9", | ||
"@typescript-eslint/eslint-plugin": "5.23.0", | ||
"@typescript-eslint/parser": "5.23.0", | ||
"eslint": "8.15.0", | ||
"eslint-config-prettier": "8.5.0", | ||
"eslint-plugin-prettier": "4.0.0", | ||
"eslint-plugin-react-hooks": "4.2.0", | ||
"husky": "7.0.2", | ||
"jest": "27.2.4", | ||
"jest-localstorage-mock": "2.4.18", | ||
"lint-staged": "11.1.2", | ||
"eslint-plugin-react-hooks": "4.5.0", | ||
"husky": "8.0.1", | ||
"jest": "28.1.0", | ||
"jest-environment-jsdom": "28.1.0", | ||
"jest-localstorage-mock": "2.4.21", | ||
"lint-staged": "12.4.1", | ||
"mockdate": "3.0.5", | ||
"prettier": "2.4.1", | ||
"react-test-renderer": "17.0.2", | ||
"ts-jest": "27.0.5", | ||
"typescript": "4.4.3" | ||
"prettier": "2.6.2", | ||
"react": "18.1.0", | ||
"react-test-renderer": "18.1.0", | ||
"ts-jest": "28.0.2", | ||
"tsup": "5.12.7", | ||
"typescript": "4.6.4" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.8.0 || ^17" | ||
"react": ">=16.8.0" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23812
21
6
164
1