konobangu/apps/webui/src/infra/storage/web-storage.service.ts

44 lines
1.1 KiB
TypeScript

import { FeatureNotAvailablePlatformError } from '@/infra/platform/errors';
import { DOCUMENT } from '@/infra/platform/injection';
import { Injectable, inject } from '@outposts/injection-js';
@Injectable()
export class LocalStorageService {
document = inject(DOCUMENT);
storage = this.document.defaultView?.localStorage;
setItem(key: string, value: string) {
if (!this.storage) {
throw new FeatureNotAvailablePlatformError('local-storage');
}
this.storage.setItem(key, value);
}
getItem(key: string) {
if (!this.storage) {
throw new FeatureNotAvailablePlatformError('local-storage');
}
return this.storage.getItem(key);
}
}
@Injectable()
export class SessionStorageService {
document = inject(DOCUMENT);
storage = this.document.defaultView?.sessionStorage;
setItem(key: string, value: string) {
if (!this.storage) {
throw new FeatureNotAvailablePlatformError('session-storage');
}
this.storage.setItem(key, value);
}
getItem(key: string) {
if (!this.storage) {
throw new FeatureNotAvailablePlatformError('session-storage');
}
return this.storage.getItem(key);
}
}