angular-safeguard
Note: This library was renamed from angular2-locker to angular-safeguard
Wrapper around sessionStorage, localStorage and cookies for angular. If both are unavailable will use an in memory storage.
Expiry is also implemented for all drivers not just cookies
Breaking Changes in 2.0:
With 2.0 this library supports AoT. As a result there was two options, one to create a storage type for each different storage, and two to change
set/get
and other methods to explicitly use a driver. The latter means we can still provide in-memory fallback without the hassle of you writing conditions everywhere. All methods on locker now have an extra param for storage type such asget(DRIVERS.SESSION, 'key')
Getting Started
$ npm i --save angular-safeguard
With Custom Config
// to set a single driver // to set fallback drivers in order of preference, pass in an Array of Driver
Methods
get
locker.get(DRIVERS.SESSION, 'myKey')
set
locker.setDRIVERS.SESSION, 'myKey', 'value'locker.setDRIVERS.SESSION, 'myKey', expiry.setHoursexpiry.getHours + 1 locker.setDRIVERS.SESSION, 'myKey', 'value', // will work with every driver type // You can also use set to pass options for cookies like maxAge and such
key
locker.setDRIVERS.COOKIES, 'key', 'value' locker.keyDRIVERS.COOKIES, 0 // 'key'
has
locker.has(DRIVERS.LOCAL, 'key')
setNamespace
locker.setNamespace'myName'locker.setNamespace // Resets to lockerConfig default
setSeparator
locker.setSeparator'myName'locker.setSeparator // Resets to lockerConfig default
remove
locker.remove(DRIVERS.SESSION, 'key')
clear
locker.clear(DRIVERS.SESSION)
Static Methods
DRIVERS
These are the types of drivers available. If you try to set it to a (single) driver that is unsupported it will fallback to the memory driver. To set fallback drivers, pass in an Array of drivers in the order or preference:
Again, if every driver in Array is unsupported, it will fall back to memory driver.
Types are available from import {DRIVERS} from 'angular-safeguard'
DRIVERS.SESSION
- Session CacheDRIVERS.LOCAL
- Local StorageDRIVERS.MEMORY
- Memory StorageDRIVERS.COOKIE
- Cookies
FAQ
Why is my data getting set to {data: myDataHere} instead of just myDataHere?: angular-safeguard provides expiry on more than just cookies, to do this it's necessary to create a bit of a more complex object so we can store expiry and more