-
-
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: added data property in memory storages
- Loading branch information
1 parent
e776f01
commit 500ac71
Showing
1 changed file
with
29 additions
and
5 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 |
---|---|---|
@@ -1,10 +1,34 @@ | ||
import { buildStorage } from './build'; | ||
import type { StorageValue } from './types'; | ||
|
||
export function buildMemoryStorage(obj: Record<string, StorageValue> = {}) { | ||
return buildStorage({ | ||
find: (key) => Promise.resolve(obj[key]), | ||
set: (key, value) => Promise.resolve(void (obj[key] = value)), | ||
remove: (key) => Promise.resolve(void delete obj[key]) | ||
/** | ||
* Creates a simple in-memory storage. This means that if you need to persist data between | ||
* page or server reloads, this will not help. | ||
* | ||
* This is the storage used by default. | ||
* | ||
* If you need to modify it's data, you can do by the `data` property. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* const memoryStorage = buildMemoryStorage(); | ||
* | ||
* setupCache(axios, { storage: memoryStorage }); | ||
* | ||
* // Simple example to force delete the request cache | ||
* | ||
* const { id } = axios.get('url'); | ||
* | ||
* delete memoryStorage.data[id]; | ||
* ``` | ||
*/ | ||
export function buildMemoryStorage() { | ||
const data: Record<string, StorageValue> = {}; | ||
const storage = buildStorage({ | ||
find: (key) => Promise.resolve(data[key]), | ||
set: (key, value) => Promise.resolve(void (data[key] = value)), | ||
remove: (key) => Promise.resolve(void delete data[key]) | ||
}); | ||
return { ...storage, data }; | ||
} |