Created
January 31, 2017 19:58
-
-
Save mindvox/889afec8cec12fadfd08c511119bd742 to your computer and use it in GitHub Desktop.
Storage service for Angular2 Apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
@Injectable() | |
export class StorageService { | |
// STORAGE | |
static getItem(key: string): any { | |
return this.get(localStorage.getItem(key)); | |
} | |
static setItem(key: string, item: Object): void { | |
return this.set(key, JSON.stringify(item)); | |
} | |
static removeItem(key: string): void { | |
return this.remove(key); | |
} | |
// KEY/VALUE STORAGE | |
static get(key: string): any { | |
return localStorage.getItem(key); | |
} | |
static set(key: string, value: any): void { | |
return localStorage.setItem(key, value); | |
} | |
static remove(key: string): void { | |
return localStorage.removeItem(key); | |
} | |
// ACCOUNT | |
static hasAccount(): boolean { | |
return !!this.getItem('account'); | |
} | |
static getAccount(): void { | |
if (this.hasAccount()) { | |
return this.getItem('account'); | |
} | |
} | |
static setAccount(account: Object): void { | |
return this.setItem('account', account); | |
} | |
static removeAccount(): void { | |
return this.removeItem('account'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment