12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- class StorageAdapter {
- constructor(type) {
- if (type === "session") {
- this.storage = window.sessionStorage;
- }
- else if (type === "local") {
- this.storage = window.localStorage;
- }
- }
- setItem(key, value) {
- return this.storage.setItem(key, JSON.stringify(value));
- }
- getItem(key) {
- const item = this.storage.getItem(key);
- if (item) {
- try {
- return JSON.parse(item);
- }
- catch (error) {
- return item;
- }
- }
- return null;
- }
- removeItem(key) {
- return this.storage.removeItem(key);
- }
- getStorage() {
- return this.storage;
- }
- get length() {
- return this.storage.length;
- }
- clear() {
- return this.storage.clear();
- }
- key(index) {
- return this.storage.key(index);
- }
- }
- StorageAdapter.session = new StorageAdapter("session");
- StorageAdapter.local = new StorageAdapter("local");
- export { StorageAdapter };
|