mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
chore: default prefix parameter for web storage (#425)
* chore: Added default prefix param for web storage * chore: Minor default prefix correction * test: add test Co-authored-by: arthurfiorette <arthur.fiorette@gmail.com>
This commit is contained in:
parent
028e1cdf56
commit
ee25b394da
@ -21,7 +21,7 @@ import type { StorageValue } from './types';
|
||||
* @param prefix The prefix to index the storage. Useful to prevent collision between
|
||||
* multiple places using the same storage.
|
||||
*/
|
||||
export function buildWebStorage(storage: Storage, prefix = '') {
|
||||
export function buildWebStorage(storage: Storage, prefix = 'axios-cache-') {
|
||||
return buildStorage({
|
||||
find: (key) => {
|
||||
const json = storage.getItem(prefix + key);
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
/** @jest-environment jsdom */
|
||||
|
||||
import { buildWebStorage } from '../../src/storage/web-api';
|
||||
import { mockAxios } from '../mocks/axios';
|
||||
import { testStorageQuota } from './quota';
|
||||
import { testStorage } from './storages';
|
||||
|
||||
@ -15,4 +16,21 @@ describe('tests web storages', () => {
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
});
|
||||
|
||||
it('Should use a key prefix by default', async () => {
|
||||
const storage = sessionStorage; // does not matter any implementation details
|
||||
|
||||
storage.setItem('test-key', '1');
|
||||
|
||||
const axios = mockAxios({
|
||||
storage: buildWebStorage(storage)
|
||||
});
|
||||
|
||||
await axios.get('url', {
|
||||
id: 'test-key'
|
||||
});
|
||||
|
||||
expect(storage.getItem('test-key')).toBe('1');
|
||||
expect(axios.storage.get('test-key')).not.toBe('1');
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user