Storage Service

class StorageService

This service is a wrapper for communicating with window.localStorage and window.sessionStorage, which manage long-term and short-term storage respectively.

It:

  1. Attaches a prefix to keys to avoid conflicts with keys set elsewhere.

  2. Clears storage and gets, sets and deletes keys when requested.

  3. Serialises and deserialises values in a safe manner.

It only accepts keys defined in Local Storage Key or Session Storage Key.

storage.service.ts
@Injectable({
  providedIn: 'root',
})
export class StorageService {
  private readonly _loggerService = inject(LoggerService);

  private readonly _prefix: string;

  public constructor() {
    // Buy to unlock
  }

  public clearLocalStorage(): void {
    // Buy to unlock
  }

  public clearSessionStorage(): void {
    // Buy to unlock
  }

  public getLocalStorageItem<T>(localStorageKey: LocalStorageKey): null | T {
    // Buy to unlock
  }

  public getSessionStorageItem<T>(
    sessionStorageKey: SessionStorageKey,
  ): null | T {
    // Buy to unlock
  }

  public removeLocalStorageItem(localStorageKey: LocalStorageKey): void {
    // Buy to unlock
  }

  public removeSessionStorageItem(sessionStorageKey: SessionStorageKey): void {
    // Buy to unlock
  }

  public setLocalStorageItem(
    localStorageKey: LocalStorageKey,
    value: unknown,
  ): void {
    // Buy to unlock
  }

  public setSessionStorageItem(
    sessionStorageKey: SessionStorageKey,
    value: unknown,
  ): void {
    // Buy to unlock
  }
}

Last updated