storage/src/browser.ts

24 lines
582 B
TypeScript

import { KeyValueStorage } from "./basic";
/**
* Key-value store using localStorage
*/
export class BrowserLocalStorage implements KeyValueStorage {
constructor() {
if (typeof localStorage == "undefined" || !localStorage) {
throw new Error("localStorage not available");
}
}
async get(key: string): Promise<string | null> {
return localStorage.getItem(key);
}
async set(key: string, value: string | null): Promise<void> {
if (value === null) {
localStorage.removeItem(key);
} else {
localStorage.setItem(key, value);
}
}
}