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:
Dimitris-Rafail Katsampas 2022-12-12 00:00:32 +02:00 committed by GitHub
parent 028e1cdf56
commit ee25b394da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -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);

View File

@ -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');
});
});