2022-01-04 15:15:27 -03:00

35 lines
901 B
TypeScript

import { buildStorage } from './build';
import type { StorageValue } from './types';
/**
* 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) => data[key],
set: (key, value) => void (data[key] = value),
remove: (key) => void delete data[key]
});
return { ...storage, data };
}