mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: prefer unknown over any and removed find method from storages
This commit is contained in:
parent
7c197390d7
commit
79ce1926d2
@ -69,7 +69,7 @@ export function setupCacheData<R, D>(
|
||||
if (response.status === 304 && cache) {
|
||||
// Set the cache information into the response object
|
||||
response.cached = true;
|
||||
response.data = cache.data;
|
||||
response.data = cache.data as R;
|
||||
response.status = cache.status;
|
||||
response.statusText = cache.statusText;
|
||||
|
||||
|
||||
@ -1,11 +1,20 @@
|
||||
import { Header } from '../util/headers';
|
||||
import type { AxiosStorage, StaleStorageValue } from './types';
|
||||
import type { MaybePromise } from '../util/types';
|
||||
import type { AxiosStorage, StaleStorageValue, StorageValue } from './types';
|
||||
|
||||
const storage = Symbol();
|
||||
|
||||
/** Returns true if the provided object was created from {@link buildStorage} function. */
|
||||
export const isStorage = (obj: any): obj is AxiosStorage => !!obj && !!obj[storage];
|
||||
|
||||
export type BuildStorage = Omit<AxiosStorage, 'get'> & {
|
||||
/**
|
||||
* Returns the value for the given key. This method does not have to make checks for
|
||||
* cache invalidation or etc. It just return what was previous saved, if present.
|
||||
*/
|
||||
find: (key: string) => MaybePromise<StorageValue | undefined>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds a custom storage.
|
||||
*
|
||||
@ -23,16 +32,11 @@ export const isStorage = (obj: any): obj is AxiosStorage => !!obj && !!obj[stora
|
||||
* const axios = setupCache(axios, { storage: myStorage });
|
||||
* ```
|
||||
*/
|
||||
export function buildStorage({
|
||||
set,
|
||||
find,
|
||||
remove
|
||||
}: Omit<AxiosStorage, 'get'>): AxiosStorage {
|
||||
export function buildStorage({ set, find, remove }: BuildStorage): AxiosStorage {
|
||||
return {
|
||||
//@ts-expect-error - we don't want to expose this
|
||||
[storage]: 1,
|
||||
set,
|
||||
find,
|
||||
remove,
|
||||
get: async (key) => {
|
||||
const value = await find(key);
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import type { MaybePromise } from '../util/types';
|
||||
|
||||
export type CachedResponse = {
|
||||
data?: any;
|
||||
data?: unknown;
|
||||
headers: Record<string, string>;
|
||||
status: number;
|
||||
statusText: string;
|
||||
@ -51,8 +53,6 @@ export type EmptyStorageValue = {
|
||||
state: 'empty';
|
||||
};
|
||||
|
||||
type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
|
||||
|
||||
/**
|
||||
* A storage implementation that stores data in memory.
|
||||
*
|
||||
@ -71,12 +71,6 @@ type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
|
||||
* ```
|
||||
*/
|
||||
export type AxiosStorage = {
|
||||
/**
|
||||
* Returns the value for the given key. This method does not have to make checks for
|
||||
* cache invalidation or etc. It just return what was previous saved, if present.
|
||||
*/
|
||||
find: (key: string) => MaybePromise<StorageValue | undefined>;
|
||||
|
||||
/**
|
||||
* Sets a new value for the given key
|
||||
*
|
||||
|
||||
@ -18,14 +18,11 @@ import { buildStorage } from './build';
|
||||
*/
|
||||
export function buildWebStorage(storage: Storage, prefix = '') {
|
||||
return buildStorage({
|
||||
find: (key: string) => {
|
||||
find: (key) => {
|
||||
const json = storage.getItem(prefix + key);
|
||||
return json ? JSON.parse(json) : undefined;
|
||||
},
|
||||
|
||||
set: (key: string, value: any) =>
|
||||
void storage.setItem(prefix + key, JSON.stringify(value)),
|
||||
|
||||
remove: (key: string) => void storage.removeItem(prefix + key)
|
||||
set: (key, value) => void storage.setItem(prefix + key, JSON.stringify(value)),
|
||||
remove: (key) => void storage.removeItem(prefix + key)
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user