Skip to content

Instantly share code, notes, and snippets.

@mindvox
Created January 31, 2017 19:58
Show Gist options
  • Save mindvox/889afec8cec12fadfd08c511119bd742 to your computer and use it in GitHub Desktop.
Save mindvox/889afec8cec12fadfd08c511119bd742 to your computer and use it in GitHub Desktop.
Storage service for Angular2 Apps
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