-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ignore and fallback storage options
- Loading branch information
Showing
6 changed files
with
138 additions
and
71 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,4 @@ | ||
export type RetrievalMode = 'fail' | 'raw' | 'safe'; | ||
export * from './memoryStorage'; | ||
export * from './types'; | ||
|
||
export interface ITypedStorage<T> { | ||
length(): number; | ||
key<U extends keyof T>(index: number): U; | ||
setItem<U extends keyof T>(key: U, value: T[U]): void; | ||
getItem<U extends keyof T>(key: U): T[U] | null; | ||
removeItem<U extends keyof T>(key: U, retrievalMode: RetrievalMode): void; | ||
clear(): void; | ||
} | ||
|
||
export interface TypedStorageOptions { | ||
storage: 'localStorage' | 'sessionStorage'; | ||
} | ||
|
||
export default class TypedStorage<T> implements ITypedStorage<T> { | ||
private readonly storage: Storage; | ||
|
||
constructor(options: TypedStorageOptions = { storage: 'localStorage' }) { | ||
this.storage = typeof window !== 'undefined' ? window[options.storage] : global[options.storage]; | ||
|
||
if (!this.storage) { | ||
throw Error('Web Storage API not found.'); | ||
} | ||
} | ||
|
||
public length(): number { | ||
return this.storage?.length; | ||
} | ||
|
||
public key<U extends keyof T>(index: number): U { | ||
return this.storage?.key(index) as U; | ||
} | ||
|
||
public getItem<U extends keyof T>(key: U, retrievalMode: RetrievalMode = 'fail'): T[U] | null { | ||
const item = this.storage?.getItem(key.toString()); | ||
|
||
if (item == null) { | ||
return item; | ||
} | ||
|
||
try { | ||
return JSON.parse(item) as T[U]; | ||
} catch (error) { | ||
switch (retrievalMode) { | ||
case 'safe': | ||
return null; | ||
case 'raw': | ||
return (item as unknown) as T[U]; | ||
default: | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
public setItem<U extends keyof T>(key: U, value: T[U]): void { | ||
this.storage?.setItem(key.toString(), JSON.stringify(value)); | ||
} | ||
|
||
public removeItem<U extends keyof T>(key: U): void { | ||
this.storage?.removeItem(key.toString()); | ||
} | ||
|
||
public clear(): void { | ||
this.storage?.clear(); | ||
} | ||
} | ||
export { default } from './typedStorage'; |
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,58 @@ | ||
import { RetrievalMode, ITypedStorage, TypedStorageOptions } from './types'; | ||
|
||
export default class TypedStorage<T> implements ITypedStorage<T> { | ||
private readonly storage: Storage; | ||
|
||
constructor({ | ||
storage = 'localStorage', | ||
ignoreMissingStorage = false, | ||
fallbackStorage = undefined, | ||
}: TypedStorageOptions = {}) { | ||
this.storage = window?.[storage] || global[storage] || fallbackStorage; | ||
|
||
if (!this.storage && !ignoreMissingStorage) { | ||
throw Error('Web Storage API not found.'); | ||
} | ||
} | ||
|
||
public get length(): number { | ||
return this.storage?.length; | ||
} | ||
|
||
public key<U extends keyof T>(index: number): U { | ||
return this.storage?.key(index) as U; | ||
} | ||
|
||
public getItem<U extends keyof T>(key: U, retrievalMode: RetrievalMode = 'fail'): T[U] | null { | ||
const item = this.storage?.getItem(key.toString()); | ||
|
||
if (item == null) { | ||
return item; | ||
} | ||
|
||
try { | ||
return JSON.parse(item) as T[U]; | ||
} catch (error) { | ||
switch (retrievalMode) { | ||
case 'safe': | ||
return null; | ||
case 'raw': | ||
return (item as unknown) as T[U]; | ||
default: | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
public setItem<U extends keyof T>(key: U, value: T[U]): void { | ||
this.storage?.setItem(key.toString(), JSON.stringify(value)); | ||
} | ||
|
||
public removeItem<U extends keyof T>(key: U): void { | ||
this.storage?.removeItem(key.toString()); | ||
} | ||
|
||
public clear(): void { | ||
this.storage?.clear(); | ||
} | ||
} |
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,16 @@ | ||
export type RetrievalMode = 'fail' | 'raw' | 'safe'; | ||
|
||
export interface ITypedStorage<T> { | ||
length: number; | ||
key<U extends keyof T>(index: number): U; | ||
setItem<U extends keyof T>(key: U, value: T[U]): void; | ||
getItem<U extends keyof T>(key: U): T[U] | null; | ||
removeItem<U extends keyof T>(key: U, retrievalMode: RetrievalMode): void; | ||
clear(): void; | ||
} | ||
|
||
export interface TypedStorageOptions { | ||
storage?: 'localStorage' | 'sessionStorage'; | ||
fallbackStorage?: Storage; | ||
ignoreMissingStorage?: boolean; | ||
} |