-
Notifications
You must be signed in to change notification settings - Fork 17
/
PolyfillDriver.ts
46 lines (33 loc) · 1.25 KB
/
PolyfillDriver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {Driver} from './Driver'
import {serializeDataToString, convertFromJSON, isExpired} from './helpers'
import {IStorageSetConfig, ICustomStorage, IStorage} from './metadata'
// This driver is a special driver to handle expiry and other options (similar to cookies)
// stores data as {data: myData, config: {expiry: new Date()}}
export class PollyfillDriver extends Driver {
constructor(public storage: IStorage) {
super(storage)
}
public set(key: string, data: any, config?: IStorageSetConfig): void {
this.storage.setItem(key, serializeDataToString({data, config}))
}
public get(key: string): any {
this._checkExpiry(key)
const data = convertFromJSON(this.storage.getItem(key))
return (data && data.data) ? data.data : data
}
public has(key: string): boolean {
this._checkExpiry(key)
const storage = <ICustomStorage>this.storage
return storage.hasItem ? storage.hasItem(key) : this.storage.hasOwnProperty(key)
}
public key(index = 0): string {
const key = this.storage.key(index)
this._checkExpiry(key)
return this.storage.key(index)
}
private _checkExpiry(key: string) {
const data = convertFromJSON(this.storage.getItem(key))
if (data && isExpired(data))
this.remove(key)
}
}