forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A0-3946: Fix password saving function not working properly
- Loading branch information
Showing
5 changed files
with
73 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/extension-base/src/background/handlers/chromeStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { z } from 'zod'; | ||
|
||
// use namespaces to add typization for your storage data | ||
const namespaces = { 'password-cache': z.record(z.string(), z.object({ password: z.string(), expiresAt: z.number() })) } as const satisfies Record<string, z.ZodSchema>; | ||
|
||
export const setItem = async <N extends keyof typeof namespaces>(namespace: N, updater: (currData?: z.TypeOf<typeof namespaces[N]>) => z.TypeOf<typeof namespaces[N]>) => { | ||
const currentData = await getItem(namespace); | ||
|
||
await chrome.storage.session.set({ [namespace]: updater(currentData) }); | ||
}; | ||
|
||
export const getItem = async <N extends keyof typeof namespaces>(namespace: N): Promise<z.infer<typeof namespaces[N]> | undefined> => { | ||
const { [namespace]: cachedData } = await chrome.storage.session.get(namespace); | ||
|
||
try { | ||
return namespaces[namespace].parse(cachedData); | ||
} catch { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const removeItem = async <N extends keyof typeof namespaces>(namespace: N) => { | ||
await chrome.storage.session.remove(namespace); | ||
}; | ||
|
||
const chromeStorage = { | ||
getItem, | ||
setItem, | ||
removeItem | ||
}; | ||
|
||
export default chromeStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters