From 4161c46aa5bf69dc36592655ca31cf14d7ddbe74 Mon Sep 17 00:00:00 2001 From: Nicolae Rusan <528313+nicolaerusan@users.noreply.github.com> Date: Thu, 14 Jul 2022 20:55:55 -0400 Subject: [PATCH 1/3] Refactoring the Storage constructor() to take in an object instead --- src/index.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 829ce70..5723ae3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,11 @@ export type StorageCallbackMap = Record const hasWindow = typeof window !== "undefined" +export type StorageConfig = { + storageArea: StorageAreaName, + secretKeyList: string[] +} + /** * https://docs.plasmo.com/framework-api/storage */ @@ -38,10 +43,12 @@ export class Storage { hasExtensionAPI = false - constructor( - storageArea = "sync" as StorageAreaName, - secretKeyList: string[] = [] - ) { + constructor(config: StorageConfig = { + storageArea: "sync" as StorageAreaName, + secretKeyList: [] + }) { + const {storageArea, secretKeyList} = config; + this.#secretSet = new Set(secretKeyList) this.#area = storageArea From ca44cde2e89475597709589e553fa1e8240466ab Mon Sep 17 00:00:00 2001 From: Louis <6723574+louisgv@users.noreply.github.com> Date: Fri, 15 Jul 2022 10:55:12 -0400 Subject: [PATCH 2/3] use infer typing, fix up param to align with hoook --- src/hook.ts | 8 +++++++- src/index.ts | 21 ++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) 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 5723ae3..5c75e52 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,11 +17,6 @@ export type StorageCallbackMap = Record const hasWindow = typeof window !== "undefined" -export type StorageConfig = { - storageArea: StorageAreaName, - secretKeyList: string[] -} - /** * https://docs.plasmo.com/framework-api/storage */ @@ -43,17 +38,15 @@ export class Storage { hasExtensionAPI = false - constructor(config: StorageConfig = { - storageArea: "sync" as StorageAreaName, - secretKeyList: [] - }) { - const {storageArea, secretKeyList} = config; - + 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 } } @@ -274,6 +267,8 @@ export class Storage { } } +export type StorageOptions = ConstructorParameters[0] + export * from "./hook" // https://stackoverflow.com/a/23329386/3151192 From 91a534a54696060deca6987f6ade547db6b7934a Mon Sep 17 00:00:00 2001 From: Louis <6723574+louisgv@users.noreply.github.com> Date: Fri, 15 Jul 2022 10:56:15 -0400 Subject: [PATCH 3/3] Bump minor version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",