browser-in-mem-storage
v1.0.1
Published
Browser in-memory storage
Downloads
5
Maintainers
Readme
Browser In-Mem Storage
Simple in-memory storage for storing sensitive informations like a token.
Installation
npm install browser-in-mem-storage
Usage
import BrowserInMemStorage from 'browser-in-mem-storage';
/**
* Initialize storage
*
* Should be invoked once at the root level of your app
*/
BrowserInMemStorage.initializeStorage();
// Accesses the in-mem storage object
console.log(BrowserInMemStorage.inMemStorage) // {}
import { clear, getItem, setItem, removeItem, logout } from 'browser-in-mem-storage';
/**
* Set an item to storage
* Takes a two arguments (key and value)
*/
setItem('name', 'Obiora C.N');
setItem('occupations', ['Frontend Engineer', 'Backend Engineer']);
console.log(BrowserInMemStorage.inMemStorage)
// { name: "Obiora C.N", occupations: ['Frontend Engineer', 'Backend Engineer'] }
/**
* Get an item from storage
* Takes an argument (key)
*/
const occupations = getItem('occupations');
console.log(occupations)
// ['Frontend Engineer', 'Backend Engineer']
/**
* Removes an item from storage
* Takes an argument (key)
*/
removeItem('occupations');
console.log(BrowserInMemStorage.inMemStorage)
// { name: "Obiora C.N" }
/**
* Clears all items from storage
* Usually invoked when you want to initiate a logout procedure
* Takes no argument
*/
clear();
console.log(BrowserInMemStorage.inMemStorage) // {}
/**
* Initiates the logout procedure
* Incase you don't want to clear storage during a logout
* You can remove the items you dont need then invoke the logout function
* The callback will be called when the logout method is invoked
* Takes a callback as an argument
*/
const handleLogoutProcedure = () => {
// remove auth token and reload app
removeItem('token');
};
logout(handleLogoutProcedure);
The in-mem storage functions can be accessed on the BrowserInMemStorage
class
Example:
BrowserInMemStorage.setItem('name', 'Obiora C.N');
BrowserInMemStorage.getItem('name');
BrowserInMemStorage.removeItem('name');
BrowserInMemStorage.clear();
BrowserInMemStorage.logout(() => {
// remove auth token and reload app
});