-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathstorage.ts
30 lines (23 loc) · 918 Bytes
/
storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { type PersistKey, type Storage } from '@logto/browser';
const keyPrefix = `logto`;
export class ChromeExtensionStorage implements Storage<PersistKey> {
constructor(public readonly appId: string) {}
getKey(item?: string) {
if (item === undefined) {
return `${keyPrefix}:${this.appId}`;
}
return `${keyPrefix}:${this.appId}:${item}`;
}
// eslint-disable-next-line @typescript-eslint/ban-types
async getItem(key: PersistKey): Promise<string | null> {
const storageKey = this.getKey(key);
const object = await chrome.storage.local.get(storageKey);
return object[storageKey] ? String(object[storageKey]) : null;
}
async setItem(key: PersistKey, value: string): Promise<void> {
await chrome.storage.local.set({ [this.getKey(key)]: value });
}
async removeItem(key: PersistKey): Promise<void> {
await chrome.storage.local.remove(this.getKey(key));
}
}