From 79ce1926d21c45c537cd3c0c020e1ffb5992fc7c Mon Sep 17 00:00:00 2001 From: arthurfiorette Date: Fri, 7 Jan 2022 15:24:41 -0300 Subject: [PATCH] refactor: prefer unknown over any and removed find method from storages --- src/interceptors/util.ts | 2 +- src/storage/build.ts | 18 +++++++++++------- src/storage/types.ts | 12 +++--------- src/storage/web-api.ts | 9 +++------ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/interceptors/util.ts b/src/interceptors/util.ts index e7d8478..871502a 100644 --- a/src/interceptors/util.ts +++ b/src/interceptors/util.ts @@ -69,7 +69,7 @@ export function setupCacheData( 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; diff --git a/src/storage/build.ts b/src/storage/build.ts index 1b6e65b..f0e1c99 100644 --- a/src/storage/build.ts +++ b/src/storage/build.ts @@ -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 & { + /** + * 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; +}; + /** * 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 { +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); diff --git a/src/storage/types.ts b/src/storage/types.ts index 4d8c5ce..ac0e878 100644 --- a/src/storage/types.ts +++ b/src/storage/types.ts @@ -1,5 +1,7 @@ +import type { MaybePromise } from '../util/types'; + export type CachedResponse = { - data?: any; + data?: unknown; headers: Record; status: number; statusText: string; @@ -51,8 +53,6 @@ export type EmptyStorageValue = { state: 'empty'; }; -type MaybePromise = T | Promise | PromiseLike; - /** * A storage implementation that stores data in memory. * @@ -71,12 +71,6 @@ type MaybePromise = T | Promise | PromiseLike; * ``` */ 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; - /** * Sets a new value for the given key * diff --git a/src/storage/web-api.ts b/src/storage/web-api.ts index f2973bb..8f9a12a 100644 --- a/src/storage/web-api.ts +++ b/src/storage/web-api.ts @@ -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) }); }