diff --git a/package.json b/package.json index 4e7e918..dd01053 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@plasmohq/storage", - "version": "0.7.0", + "version": "0.8.0", "description": "Safely store data and share them across your extension", "type": "module", "main": "./dist/index.js", diff --git a/src/hook.ts b/src/hook.ts index 56c41ee..6010b0c 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -19,6 +19,7 @@ export const useStorage = ( | { key: string area?: StorageAreaName + isSecret?: boolean }, onInit?: ((v?: T) => T) | T ) => { @@ -33,7 +34,12 @@ export const useStorage = ( const isMounted = useRef(false) // Storage state - const storageRef = useRef(new Storage(isStringKey ? "sync" : rawKey.area)) + const storageRef = useRef( + new Storage({ + area: isStringKey ? "sync" : rawKey.area, + secretKeyList: !isStringKey && rawKey.isSecret ? [key] : [] + }) + ) useEffect(() => { isMounted.current = true diff --git a/src/index.ts b/src/index.ts index 829ce70..5c75e52 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,15 +38,15 @@ export class Storage { hasExtensionAPI = false - constructor( - storageArea = "sync" as StorageAreaName, - secretKeyList: string[] = [] - ) { + constructor({ + area = "sync" as StorageAreaName, + secretKeyList = [] as string[] + } = {}) { this.#secretSet = new Set(secretKeyList) - this.#area = storageArea + this.#area = area if (chrome?.storage) { - this.#client = chrome.storage[storageArea] + this.#client = chrome.storage[this.#area] this.hasExtensionAPI = true } } @@ -267,6 +267,8 @@ export class Storage { } } +export type StorageOptions = ConstructorParameters[0] + export * from "./hook" // https://stackoverflow.com/a/23329386/3151192