import { Injectable } from '@angular/core'; const getLocalStorage = () => { return window.localStorage; }; import { ErrorLoggingService, } from './../index'; import { StorageInterface } from './storage.interface'; @Injectable() export class LocalStorageService implements StorageInterface { constructor( private errorLoggingService: ErrorLoggingService, ) { } public get( name: string, ): T { try { const value = getLocalStorage() .getItem(name); if (value) { return JSON.parse( value, ) as T; } } catch (e) { this._logError(e); } return null; } public set( name: string, data: T, ) { try { getLocalStorage() .setItem( name, JSON.stringify(data), ); } catch (e) { this._logError(e); } } public clear() { try { getLocalStorage().clear(); } catch (e) { this._logError(e); } } public removeItem(storageName: string) { try { getLocalStorage().removeItem(storageName); } catch (e) { this._logError(e); } } private _logError(error: any) { this.errorLoggingService.logError( error as Error, ); } }