-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: better support for different storages * test: updated tests * feat: export AxiosStorage * style: fix linting
- Loading branch information
1 parent
76a8af7
commit b35ae3e
Showing
14 changed files
with
121 additions
and
141 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { AxiosStorage } from './storage'; | ||
import type { EmptyStorageValue, StorageValue } from './types'; | ||
|
||
export class BrowserAxiosStorage extends AxiosStorage { | ||
public static DEFAULT_KEY_PREFIX = 'a-c-i'; | ||
|
||
/** | ||
* @param storage any browser storage, like sessionStorage or localStorage | ||
* @param prefix the key prefix to use on all keys. | ||
*/ | ||
constructor( | ||
readonly storage: Storage, | ||
readonly prefix: string = BrowserAxiosStorage.DEFAULT_KEY_PREFIX | ||
) { | ||
super(); | ||
} | ||
|
||
public get = (key: string): StorageValue => { | ||
const prefixedKey = `${this.prefix}:${key}`; | ||
|
||
const json = this.storage.getItem(prefixedKey); | ||
|
||
if (!json) { | ||
return { state: 'empty' }; | ||
} | ||
|
||
const parsed = JSON.parse(json); | ||
|
||
if (!AxiosStorage.isValid(parsed)) { | ||
this.storage.removeItem(prefixedKey); | ||
return { state: 'empty' }; | ||
} | ||
|
||
return parsed; | ||
}; | ||
|
||
public set = (key: string, value: Exclude<StorageValue, EmptyStorageValue>): void => { | ||
return this.storage.setItem(`${this.prefix}:${key}`, JSON.stringify(value)); | ||
}; | ||
|
||
public remove = (key: string): void | Promise<void> => { | ||
return this.storage.removeItem(`${this.prefix}:${key}`); | ||
}; | ||
} |
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,29 +1,31 @@ | ||
import type { CacheStorage, StorageValue } from './types'; | ||
import { isCacheValid } from './util'; | ||
import { AxiosStorage } from './storage'; | ||
import type { CachedStorageValue, LoadingStorageValue, StorageValue } from './types'; | ||
|
||
export class MemoryStorage implements CacheStorage { | ||
private readonly storage: Map<string, StorageValue> = new Map(); | ||
export class MemoryAxiosStorage extends AxiosStorage { | ||
constructor(readonly storage: Record<string, StorageValue> = {}) { | ||
super(); | ||
} | ||
|
||
get = async (key: string): Promise<StorageValue> => { | ||
const value = this.storage.get(key); | ||
public get = (key: string): StorageValue => { | ||
const value = this.storage[key]; | ||
|
||
if (!value) { | ||
return { state: 'empty' }; | ||
} | ||
|
||
if (isCacheValid(value) === false) { | ||
if (!AxiosStorage.isValid(value)) { | ||
this.remove(key); | ||
return { state: 'empty' }; | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
set = async (key: string, value: StorageValue): Promise<void> => { | ||
this.storage.set(key, value); | ||
public set = (key: string, value: CachedStorageValue | LoadingStorageValue): void => { | ||
this.storage[key] = value; | ||
}; | ||
|
||
remove = async (key: string): Promise<void> => { | ||
this.storage.delete(key); | ||
public remove = (key: string): void => { | ||
delete this.storage[key]; | ||
}; | ||
} |
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,38 @@ | ||
import type { EmptyStorageValue, StorageValue } from './types'; | ||
|
||
export abstract class AxiosStorage { | ||
/** | ||
* Returns the cached value for the given key. Must handle cache | ||
* miss and staling by returning a new `StorageValue` with `empty` state. | ||
* | ||
* @see {AxiosStorage#isValid} | ||
*/ | ||
public abstract get: (key: string) => Promise<StorageValue> | StorageValue; | ||
|
||
/** | ||
* Sets a new value for the given key | ||
* | ||
* Use CacheStorage.remove(key) to define a key to 'empty' state. | ||
*/ | ||
public abstract set: ( | ||
key: string, | ||
value: Exclude<StorageValue, EmptyStorageValue> | ||
) => Promise<void> | void; | ||
|
||
/** | ||
* Removes the value for the given key | ||
*/ | ||
public abstract remove: (key: string) => Promise<void> | void; | ||
|
||
/** | ||
* Returns true if a storage value can still be used by checking his | ||
* createdAt and ttl values. | ||
*/ | ||
static isValid = (value?: StorageValue): boolean | 'unknown' => { | ||
if (value?.state === 'cached') { | ||
return value.createdAt + value.ttl > Date.now(); | ||
} | ||
|
||
return true; | ||
}; | ||
} |
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 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
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,6 +1,6 @@ | ||
import { MemoryStorage } from '../../src/storage/memory'; | ||
import { MemoryAxiosStorage } from '../../src/storage/memory'; | ||
import { testStorage } from './storages'; | ||
|
||
describe('tests common storages', () => { | ||
testStorage('memory', () => new MemoryStorage()); | ||
testStorage('memory', () => new MemoryAxiosStorage()); | ||
}); |
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