-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sifrr-storage): migrate to typescript (#261)
* feat(sifrr-storage): migrate to typescript * chore: add auto generated files * test: fixes * chore: fix cookie not found issue * Update .gitignore
- Loading branch information
1 parent
83fb4d1
commit 01d6f6c
Showing
39 changed files
with
761 additions
and
650 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ typings/ | |
.next | ||
dist | ||
.DS_Store | ||
.ts_cache | ||
|
||
|
||
# test yarns | ||
**/test/public/yarn.lock |
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import IndexedDB from './storages/indexeddb'; | ||
import WebSQL from './storages/websql'; | ||
import LocalStorage from './storages/localstorage'; | ||
import Cookies from './storages/cookies'; | ||
import JsonStorage from './storages/jsonstorage'; | ||
import { StorageOptions } from './storages/types'; | ||
import Storage from './storages/storage'; | ||
|
||
const storages: { [x: string]: typeof Storage } = { | ||
[IndexedDB.type]: IndexedDB, | ||
[WebSQL.type]: WebSQL, | ||
[LocalStorage.type]: LocalStorage, | ||
[Cookies.type]: Cookies, | ||
[JsonStorage.type]: JsonStorage | ||
}; | ||
|
||
type MainOptions = StorageOptions & { | ||
priority?: string[]; | ||
}; | ||
|
||
function getSupportedStoreFromPriority( | ||
priority: string[] = [], | ||
options: StorageOptions = {} | ||
): Storage { | ||
priority = priority.concat([ | ||
IndexedDB.type, | ||
WebSQL.type, | ||
LocalStorage.type, | ||
Cookies.type, | ||
JsonStorage.type | ||
]); | ||
for (let i = 0; i < priority.length; i++) { | ||
let store = storages[priority[i]]; | ||
if (store) { | ||
const storage = new store(options); | ||
if (storage.isSupported()) return storage; | ||
} | ||
} | ||
throw Error( | ||
'No compatible storage found. Available types: ' + Object.keys(storages).join(', ') + '.' | ||
); | ||
} | ||
|
||
function getStorage(options: string | MainOptions): Storage { | ||
const priority = typeof options === 'string' ? [options] : (options || {}).priority; | ||
const stOptions = typeof options === 'string' ? {} : options; | ||
|
||
return getSupportedStoreFromPriority(priority, stOptions); | ||
} | ||
|
||
export { | ||
IndexedDB, | ||
WebSQL, | ||
LocalStorage, | ||
Cookies, | ||
JsonStorage, | ||
getStorage, | ||
storages as availableStores | ||
}; |
Empty file.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Storage from './storage'; | ||
import { StorageOptions } from './types'; | ||
|
||
const date = new Date(0).toUTCString(); | ||
const equal = '%3D', | ||
equalRegex = new RegExp(equal, 'g'); | ||
|
||
class Cookies extends Storage { | ||
constructor(options: StorageOptions) { | ||
super(options); | ||
return (<typeof Cookies>this.constructor)._matchingInstance<Cookies>(this); | ||
} | ||
|
||
protected upsert(data: object) { | ||
for (let key in data) { | ||
this.setStore( | ||
`${this.tableName}/${key}=${(<typeof Storage>this.constructor) | ||
.stringify(data[key]) | ||
.replace(/=/g, equal)}; path=/` | ||
); | ||
} | ||
return true; | ||
} | ||
|
||
protected delete(keys: string[]) { | ||
keys.forEach((k: any) => this.setStore(`${this.tableName}/${k}=; expires=${date}; path=/`)); | ||
return true; | ||
} | ||
|
||
protected deleteAll() { | ||
return this.keys().then(this.delete.bind(this)); | ||
} | ||
|
||
protected getStore() { | ||
let result = document.cookie, | ||
ans = {}; | ||
result.split('; ').forEach(value => { | ||
let [k, v] = value.split('='); | ||
if (k.indexOf(this.tableName) === 0) | ||
ans[k.slice(this.tableName.length + 1)] = (<typeof Storage>this.constructor).parse( | ||
v.replace(equalRegex, '=') | ||
); | ||
}); | ||
return ans; | ||
} | ||
|
||
protected setStore(v: string) { | ||
document.cookie = v; | ||
} | ||
|
||
hasStore() { | ||
return typeof document.cookie !== 'undefined'; | ||
} | ||
|
||
static get type() { | ||
return 'cookies'; | ||
} | ||
} | ||
|
||
export default Cookies; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.