Skip to content

Commit

Permalink
refactor: more type and naming refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Oct 12, 2021
1 parent 891f1d0 commit b42d3f1
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 133 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ To you use this cache interceptor, you can apply to an existing instance or crea
one.

```js
import { applyCache } from 'axios-cache-interceptor';
import { useCache } from 'axios-cache-interceptor';

// Your axios instance
let axios;

// Return the same axios instance, but with a modified Typescript type.
axios = applyCache(axios, {
axios = useCache(axios, {
/* options here */
});
```
Expand Down
161 changes: 161 additions & 0 deletions src/cache/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import type {
AxiosDefaults,
AxiosInterceptorManager,
AxiosRequestConfig,
AxiosResponse
} from 'axios';
import type { CacheInstance, CacheProperties } from './cache';

/**
* @template R The type returned by this response
* @template D The type that the request body was
*/
export type CacheAxiosResponse<R, D> = AxiosResponse<R, D> & {
config: CacheRequestConfig<D>;

/**
* The id used for this request. if config specified an id, the id
* will be returned
*/
id: string;

/**
* A simple boolean to check whether this request was cached or not
*/
cached: boolean;
};

/**
* Options that can be overridden per request
*
* @template D The type for the request body
*/
export type CacheRequestConfig<D> = AxiosRequestConfig<D> & {
/**
* An id for this request, if this request is used in cache, only
* the last request made with this id will be returned.
*
* @default undefined
*/
id?: string;

/**
* All cache options for the request.
*
* False means ignore everything about cache, for this request.
*/
cache?: false | Partial<CacheProperties>;
};

/**
* Same as the AxiosInstance but with CacheRequestConfig as a config
* type and CacheAxiosResponse as response type.
*
* @see Axios
* @see CacheRequestConfig
* @see CacheInstance
*/
export interface AxiosCacheInstance extends CacheInstance {
/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
<R = unknown, D = any>(config: CacheRequestConfig<D>): Promise<
CacheAxiosResponse<R, D>
>;
/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
<R = unknown, D = any>(url: string, config?: CacheRequestConfig<D>): Promise<
CacheAxiosResponse<R, D>
>;

defaults: AxiosDefaults<any> & {
cache: CacheProperties;
};

interceptors: {
request: AxiosInterceptorManager<CacheRequestConfig<any>>;
response: AxiosInterceptorManager<CacheAxiosResponse<never, any>>;
};

/**
* @template D The type that the request body use
*/
getUri<D>(config?: CacheRequestConfig<D>): string;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
request<R = unknown, D = any>(
config: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
get<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
delete<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
head<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
options<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
post<R = unknown, D = any>(
url: string,
data?: D,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
put<R = unknown, D = any>(
url: string,
data?: D,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

/**
* @template R The type returned by this response
* @template D The type that the request body use
*/
patch<R = unknown, D = any>(
url: string,
data?: D,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;
}
117 changes: 2 additions & 115 deletions src/axios/types.ts → src/cache/cache.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type {
AxiosDefaults,
AxiosInterceptorManager,
AxiosPromise,
AxiosRequestConfig,
AxiosResponse,
Method
} from 'axios';
import type { Method } from 'axios';
import type { Deferred } from 'typed-core/dist/promises/deferred';
import type { HeaderInterpreter } from '../header/types';
import type { AxiosInterceptor } from '../interceptors/types';
import type { CachedResponse, CacheStorage } from '../storage/types';
import type { CachePredicate, KeyGenerator } from '../util/types';
import type { CacheUpdater } from '../util/update-cache';
import type { CacheAxiosResponse, CacheRequestConfig } from './axios';

export type CacheProperties = {
/**
Expand Down Expand Up @@ -65,47 +59,6 @@ export type CacheProperties = {
update: Record<string, CacheUpdater>;
};

/**
* @template R The type returned by this response
* @template D The type that the request body was
*/
export type CacheAxiosResponse<R, D> = AxiosResponse<R, D> & {
config: CacheRequestConfig<D>;

/**
* The id used for this request. if config specified an id, the id
* will be returned
*/
id: string;

/**
* A simple boolean to check whether this request was cached or not
*/
cached: boolean;
};

/**
* Options that can be overridden per request
*
* @template D The type for the request body
*/
export type CacheRequestConfig<D> = AxiosRequestConfig<D> & {
/**
* An id for this request, if this request is used in cache, only
* the last request made with this id will be returned.
*
* @default undefined
*/
id?: string;

/**
* All cache options for the request.
*
* False means ignore everything about cache, for this request.
*/
cache?: false | Partial<CacheProperties>;
};

export interface CacheInstance {
/**
* The storage to save the cache data.
Expand Down Expand Up @@ -146,69 +99,3 @@ export interface CacheInstance {
*/
responseInterceptor: AxiosInterceptor<CacheAxiosResponse<unknown, any>>;
}

/**
* Same as the AxiosInstance but with CacheRequestConfig as a config
* type and CacheAxiosResponse as response type.
*
* @see Axios
* @see CacheRequestConfig
* @see CacheInstance
*/
export interface AxiosCacheInstance extends CacheInstance {
<T>(config: CacheRequestConfig<T>): AxiosPromise;
<T>(url: string, config?: CacheRequestConfig<T>): AxiosPromise;

defaults: AxiosDefaults<any> & {
cache: CacheProperties;
};

interceptors: {
request: AxiosInterceptorManager<CacheRequestConfig<any>>;
response: AxiosInterceptorManager<CacheAxiosResponse<never, any>>;
};

getUri<T>(config?: CacheRequestConfig<T>): string;

request<R = unknown, D = any>(
config: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

get<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

delete<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

head<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

options<R = unknown, D = any>(
url: string,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

post<R = unknown, D = any>(
url: string,
data?: D,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

put<R = unknown, D = any>(
url: string,
data?: D,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;

patch<R = unknown, D = any>(
url: string,
data?: D,
config?: CacheRequestConfig<D>
): Promise<CacheAxiosResponse<R, D>>;
}
13 changes: 8 additions & 5 deletions src/axios/cache.ts → src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { CacheRequestInterceptor } from '../interceptors/request';
import { CacheResponseInterceptor } from '../interceptors/response';
import { MemoryStorage } from '../storage/memory';
import { defaultKeyGenerator } from '../util/key-generator';
import type { AxiosCacheInstance, CacheInstance, CacheProperties } from './types';
import type { AxiosCacheInstance } from './axios';
import type { CacheInstance, CacheProperties } from './cache';

/**
* Apply the caching interceptors for a already created axios instance.
Expand All @@ -13,7 +14,7 @@ import type { AxiosCacheInstance, CacheInstance, CacheProperties } from './types
* @param config The config for the caching interceptors
* @returns The same instance but with caching enabled
*/
export function applyCache(
export function useCache(
axios: AxiosInstance,
{
storage,
Expand All @@ -23,7 +24,7 @@ export function applyCache(
requestInterceptor,
responseInterceptor,
...cacheOptions
}: CreateCacheOptions['cache'] = {}
}: CacheOptions = {}
): AxiosCacheInstance {
const axiosCache = axios as AxiosCacheInstance;

Expand Down Expand Up @@ -68,10 +69,12 @@ export function createCache({
axios,
cache
}: CreateCacheOptions = {}): AxiosCacheInstance {
return applyCache(Axios.create(axios), cache);
return useCache(Axios.create(axios), cache);
}

export type CacheOptions = Partial<CacheInstance> & Partial<CacheProperties>;

export type CreateCacheOptions = {
axios?: Partial<AxiosRequestConfig>;
cache?: Partial<CacheInstance> & Partial<CacheProperties>;
cache?: CacheOptions;
};
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './axios/cache';
export * from './axios/types';
export * from './cache/axios';
export * from './cache/cache';
export * from './cache/create';
export * from './header/types';
export * from './interceptors/types';
export * from './storage/types';
Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
AxiosCacheInstance,
CacheAxiosResponse,
CacheRequestConfig
} from '../axios/types';
} from '../cache/axios';
import type {
CachedResponse,
CachedStorageValue,
Expand Down
7 changes: 2 additions & 5 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type { AxiosResponse } from 'axios';
import { extract } from 'typed-core/dist/core/object';
import type {
AxiosCacheInstance,
CacheAxiosResponse,
CacheProperties
} from '../axios/types';
import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios';
import type { CacheProperties } from '../cache/cache';
import type { CachedStorageValue } from '../storage/types';
import { checkPredicateObject } from '../util/cache-predicate';
import { updateCache } from '../util/update-cache';
Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface AxiosInterceptor<T> {
onRejected?(error: any): any;

/**
* Should apply this interceptor to an already provided axios instance
* Should apply this interceptor to an already provided axios instance. Does not call this method explicitly.
*/
use(): void;
}

0 comments on commit b42d3f1

Please sign in to comment.