Skip to content

Commit

Permalink
refactor: made some methods static and public
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Dec 11, 2021
1 parent d00b607 commit b4ad24d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useCache(
): AxiosCacheInstance {
const axiosCache = axios as AxiosCacheInstance;

axiosCache.storage = storage || new MemoryAxiosStorage({});
axiosCache.storage = storage || new MemoryAxiosStorage();
axiosCache.generateKey = generateKey || defaultKeyGenerator;
axiosCache.waiting = waiting || {};
axiosCache.headerInterpreter = headerInterpreter || defaultHeaderInterpreter;
Expand Down
16 changes: 9 additions & 7 deletions src/interceptors/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export class CacheRequestInterceptor<D>
{
constructor(readonly axios: AxiosCacheInstance) {}

public use = (): void => {
readonly use = (): void => {
this.axios.interceptors.request.use(this.onFulfilled);
};

public onFulfilled = async (
readonly onFulfilled = async (
config: CacheRequestConfig<D>
): Promise<CacheRequestConfig<D>> => {
if (config.cache === false) {
Expand All @@ -36,7 +36,7 @@ export class CacheRequestInterceptor<D>

if (
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
!this.isMethodAllowed(config.method!, config.cache)
!CacheRequestInterceptor.isMethodAllowed(config.method!, config.cache)
) {
return config;
}
Expand Down Expand Up @@ -76,7 +76,7 @@ export class CacheRequestInterceptor<D>

if (cache.state === 'stale') {
//@ts-expect-error type infer couldn't resolve this
this.setRevalidationHeaders(cache, config);
CacheRequestInterceptor.setRevalidationHeaders(cache, config);
}

config.validateStatus = CacheRequestInterceptor.createValidateStatus(
Expand Down Expand Up @@ -126,7 +126,7 @@ export class CacheRequestInterceptor<D>
return config;
};

private isMethodAllowed = (
static readonly isMethodAllowed = (
method: Method,
properties: Partial<CacheProperties>
): boolean => {
Expand All @@ -141,7 +141,7 @@ export class CacheRequestInterceptor<D>
return false;
};

private setRevalidationHeaders = (
static readonly setRevalidationHeaders = <D>(
cache: StaleStorageValue,
config: CacheRequestConfig<D> & { cache: Partial<CacheProperties> }
): void => {
Expand Down Expand Up @@ -170,7 +170,9 @@ export class CacheRequestInterceptor<D>
* Creates a new validateStatus function that will use the one
* already used and also accept status code 304.
*/
static createValidateStatus = (oldValidate?: AxiosRequestConfig['validateStatus']) => {
static readonly createValidateStatus = (
oldValidate?: AxiosRequestConfig['validateStatus']
) => {
return (status: number): boolean => {
return oldValidate
? oldValidate(status) || status === 304
Expand Down
8 changes: 4 additions & 4 deletions src/storage/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AxiosStorage } from './storage';
import type { StorageValue } from './types';

export class BrowserAxiosStorage extends AxiosStorage {
public static DEFAULT_KEY_PREFIX = 'a-c-i';
public static readonly DEFAULT_KEY_PREFIX = 'a-c-i';

/**
* @param storage Any browser storage, like sessionStorage or localStorage
Expand All @@ -16,16 +16,16 @@ export class BrowserAxiosStorage extends AxiosStorage {
super();
}

public find = async (key: string): Promise<StorageValue> => {
readonly find = async (key: string): Promise<StorageValue> => {
const json = this.storage.getItem(`${this.prefix}:${key}`);
return json ? JSON.parse(json) : { state: 'empty' };
};

public set = async (key: string, value: NotEmptyStorageValue): Promise<void> => {
readonly set = async (key: string, value: NotEmptyStorageValue): Promise<void> => {
return this.storage.setItem(`${this.prefix}:${key}`, JSON.stringify(value));
};

public remove = async (key: string): Promise<void> => {
readonly remove = async (key: string): Promise<void> => {
return this.storage.removeItem(`${this.prefix}:${key}`);
};
}
6 changes: 3 additions & 3 deletions src/storage/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export class MemoryAxiosStorage extends AxiosStorage {
super();
}

public find = async (key: string): Promise<StorageValue> => {
readonly find = async (key: string): Promise<StorageValue> => {
return this.storage[key] || { state: 'empty' };
};

public set = async (key: string, value: NotEmptyStorageValue): Promise<void> => {
readonly set = async (key: string, value: NotEmptyStorageValue): Promise<void> => {
this.storage[key] = value;
};

public remove = async (key: string): Promise<void> => {
readonly remove = async (key: string): Promise<void> => {
delete this.storage[key];
};
}
10 changes: 5 additions & 5 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ export abstract class AxiosStorage {
* Returns the cached value for the given key. The get method is
* what takes care to invalidate the values.
*/
protected abstract find: (key: string) => Promise<StorageValue>;
protected abstract readonly find: (key: string) => Promise<StorageValue>;

/**
* Sets a new value for the given key
*
* Use CacheStorage.remove(key) to define a key to 'empty' state.
*/
public abstract set: (key: string, value: NotEmptyStorageValue) => Promise<void>;
abstract readonly set: (key: string, value: NotEmptyStorageValue) => Promise<void>;

/**
* Removes the value for the given key
*/
public abstract remove: (key: string) => Promise<void>;
abstract readonly remove: (key: string) => Promise<void>;

public get = async (key: string): Promise<StorageValue> => {
readonly get = async (key: string): Promise<StorageValue> => {
const value = await this.find(key);

if (
Expand Down Expand Up @@ -50,7 +50,7 @@ export abstract class AxiosStorage {
/**
* Returns true if a invalid cache should still be kept
*/
static keepIfStale = ({ data }: CachedStorageValue): boolean => {
static readonly keepIfStale = ({ data }: CachedStorageValue): boolean => {
if (data?.headers) {
return (
Header.ETag in data.headers ||
Expand Down

0 comments on commit b4ad24d

Please sign in to comment.