-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: modified and simplified storages
- Loading branch information
1 parent
53c4d41
commit bafe1de
Showing
16 changed files
with
141 additions
and
195 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
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 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,48 @@ | ||
import { Header } from '../util/headers'; | ||
import type { AxiosStorage, StaleStorageValue } from './types'; | ||
|
||
export function buildStorage({ | ||
set, | ||
find, | ||
remove | ||
}: Omit<AxiosStorage, 'get'>): AxiosStorage { | ||
return { | ||
set, | ||
find, | ||
remove, | ||
get: async (key) => { | ||
const value = await find(key); | ||
|
||
if (!value) { | ||
return { state: 'empty' }; | ||
} | ||
|
||
if ( | ||
value.state !== 'cached' || | ||
// Not cached or fresh value | ||
value.createdAt + value.ttl > Date.now() | ||
) { | ||
return value; | ||
} | ||
|
||
if ( | ||
value.data.headers && | ||
(Header.ETag in value.data.headers || | ||
Header.LastModified in value.data.headers || | ||
Header.XAxiosCacheEtag in value.data.headers || | ||
Header.XAxiosCacheLastModified in value.data.headers) | ||
) { | ||
const stale: StaleStorageValue = { | ||
data: value.data, | ||
state: 'stale', | ||
createdAt: value.createdAt | ||
}; | ||
await set(key, stale); | ||
return stale; | ||
} | ||
|
||
await remove(key); | ||
return { state: 'empty' }; | ||
} | ||
}; | ||
} |
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,20 +1,10 @@ | ||
import { AxiosStorage } from './storage'; | ||
import type { NotEmptyStorageValue, StorageValue } from './types'; | ||
|
||
export class MemoryAxiosStorage extends AxiosStorage { | ||
constructor(readonly storage: Record<string, StorageValue> = {}) { | ||
super(); | ||
} | ||
|
||
readonly find = async (key: string): Promise<StorageValue> => { | ||
return this.storage[key] || { state: 'empty' }; | ||
}; | ||
|
||
readonly set = async (key: string, value: NotEmptyStorageValue): Promise<void> => { | ||
this.storage[key] = value; | ||
}; | ||
|
||
readonly remove = async (key: string): Promise<void> => { | ||
delete this.storage[key]; | ||
}; | ||
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]) | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { buildStorage } from './build'; | ||
|
||
/** | ||
* @example | ||
* ```js | ||
* const fromLocalStorage = buildWebStorage(localStorage); | ||
* | ||
* const fromSessionStorage = buildWebStorage(sessionStorage); | ||
* | ||
* const myStorage = new Storage(); | ||
* const fromMyStorage = buildWebStorage(myStorage); | ||
* ``` | ||
*/ | ||
export function buildWebStorage(storage: Storage, prefix = '') { | ||
return buildStorage({ | ||
find: (key: string) => { | ||
const json = storage.getItem(prefix + key); | ||
return Promise.resolve(json ? JSON.parse(json) : undefined); | ||
}, | ||
|
||
set: (key: string, value: any) => | ||
Promise.resolve(void storage.setItem(prefix + key, JSON.stringify(value))), | ||
|
||
remove: (key: string) => Promise.resolve(void storage.removeItem(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
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 { MemoryAxiosStorage } from '../../src/storage/memory'; | ||
import { buildMemoryStorage } from '../../src/storage/memory'; | ||
import { testStorage } from './storages'; | ||
|
||
describe('tests common storages', () => { | ||
testStorage('memory', () => new MemoryAxiosStorage()); | ||
testStorage('memory', () => buildMemoryStorage()); | ||
}); |
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.
Oops, something went wrong.