mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
style: use @arthurfiorette/prettier-config
This commit is contained in:
parent
a43dfaba48
commit
dc5608578f
11
.prettierrc
11
.prettierrc
@ -1,11 +0,0 @@
|
||||
{
|
||||
"printWidth": 80,
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "none",
|
||||
"arrowParens": "always",
|
||||
"tsdoc": true,
|
||||
"jsdocSpaces": 1,
|
||||
"jsdocPrintWidth": 100,
|
||||
"jsdocSingleLineComment": false
|
||||
}
|
||||
8
.prettierrc.js
Normal file
8
.prettierrc.js
Normal file
@ -0,0 +1,8 @@
|
||||
// https://github.com/ArthurFiorette/prettier-config
|
||||
|
||||
module.exports = require('@arthurfiorette/prettier-config')({
|
||||
tsdoc: true,
|
||||
jsdocSpaces: 1,
|
||||
jsdocPrintWidth: 100,
|
||||
jsdocSingleLineComment: false
|
||||
});
|
||||
@ -98,7 +98,8 @@ yarn add axios axios-cache-interceptor
|
||||
|
||||
### Inspiration
|
||||
|
||||
This project is highly inspired by several projects, written entirely in typescript, supporting https headers and much more.
|
||||
This project is highly inspired by several projects, written entirely in typescript, supporting
|
||||
https headers and much more.
|
||||
|
||||
Take a look at some similar projects:
|
||||
|
||||
@ -116,6 +117,7 @@ Licensed under the **MIT**. See [`LICENSE`](LICENSE) for more informations.
|
||||
|
||||
### Contact
|
||||
|
||||
See my contact information on my [github profile](https://github.com/ArthurFiorette) or open a new issue.
|
||||
See my contact information on my [github profile](https://github.com/ArthurFiorette) or open a new
|
||||
issue.
|
||||
|
||||
<br />
|
||||
|
||||
@ -32,7 +32,11 @@
|
||||
"url": "https://github.com/ArthurFiorette/axios-cache-interceptor/issues"
|
||||
},
|
||||
"homepage": "https://github.com/ArthurFiorette/axios-cache-interceptor#readme",
|
||||
"dependencies": {
|
||||
"@tusbar/cache-control": "^0.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@arthurfiorette/prettier-config": "^1.0.5",
|
||||
"@types/node": "^16.7.10",
|
||||
"@typescript-eslint/eslint-plugin": "^4.30.0",
|
||||
"@typescript-eslint/parser": "^4.30.0",
|
||||
@ -44,8 +48,5 @@
|
||||
"prettier-plugin-jsdoc": "^0.3.23",
|
||||
"prettier-plugin-organize-imports": "^2.3.3",
|
||||
"typescript": "^4.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tusbar/cache-control": "^0.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,10 +7,7 @@ import { AxiosCacheInstance, CacheInstance, CacheRequestConfig } from './types';
|
||||
|
||||
type Options = CacheRequestConfig['cache'] & Partial<CacheInstance>;
|
||||
|
||||
export function createCache(
|
||||
axios: AxiosInstance,
|
||||
options: Options = {}
|
||||
): AxiosCacheInstance {
|
||||
export function createCache(axios: AxiosInstance, options: Options = {}): AxiosCacheInstance {
|
||||
const axiosCache = axios as AxiosCacheInstance;
|
||||
|
||||
axiosCache.storage = options.storage || new MemoryStorage();
|
||||
|
||||
@ -94,33 +94,17 @@ export interface AxiosCacheInstance extends AxiosInstance, CacheInstance {
|
||||
defaults: CacheRequestConfig;
|
||||
interceptors: {
|
||||
request: AxiosInterceptorManager<CacheRequestConfig>;
|
||||
response: AxiosInterceptorManager<
|
||||
AxiosResponse & { config: CacheRequestConfig }
|
||||
>;
|
||||
response: AxiosInterceptorManager<AxiosResponse & { config: CacheRequestConfig }>;
|
||||
};
|
||||
|
||||
getUri(config?: CacheRequestConfig): string;
|
||||
|
||||
request<T = any, R = AxiosResponse<T>>(
|
||||
config: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
request<T = any, R = AxiosResponse<T>>(config: CacheRequestConfig): Promise<R>;
|
||||
|
||||
get<T = any, R = AxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
delete<T = any, R = AxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
head<T = any, R = AxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
options<T = any, R = AxiosResponse<T>>(
|
||||
url: string,
|
||||
config?: CacheRequestConfig
|
||||
): Promise<R>;
|
||||
get<T = any, R = AxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
delete<T = any, R = AxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
head<T = any, R = AxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
options<T = any, R = AxiosResponse<T>>(url: string, config?: CacheRequestConfig): Promise<R>;
|
||||
post<T = any, R = AxiosResponse<T>>(
|
||||
url: string,
|
||||
data?: any,
|
||||
|
||||
@ -1,18 +1,11 @@
|
||||
import { AxiosCacheInstance } from '../axios/types';
|
||||
import {
|
||||
CACHED_RESPONSE_STATUS,
|
||||
CACHED_RESPONSE_STATUS_TEXT
|
||||
} from '../constants';
|
||||
import { CACHED_RESPONSE_STATUS, CACHED_RESPONSE_STATUS_TEXT } from '../constants';
|
||||
import { Deferred } from '../utils/deferred';
|
||||
|
||||
export function applyRequestInterceptor(axios: AxiosCacheInstance) {
|
||||
axios.interceptors.request.use(async (config) => {
|
||||
// Only cache specified methods
|
||||
if (
|
||||
config.cache?.methods?.some(
|
||||
(method) => (config.method || 'get').toLowerCase() == method
|
||||
)
|
||||
) {
|
||||
if (config.cache?.methods?.some((method) => (config.method || 'get').toLowerCase() == method)) {
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
@ -4,9 +4,7 @@ import { AxiosCacheInstance } from '../axios/types';
|
||||
export function applyResponseInterceptor(axios: AxiosCacheInstance) {
|
||||
axios.interceptors.response.use(async (response) => {
|
||||
// Update other entries before updating himself
|
||||
for (const [cacheKey, value] of Object.entries(
|
||||
response.config.cache?.update || {}
|
||||
)) {
|
||||
for (const [cacheKey, value] of Object.entries(response.config.cache?.update || {})) {
|
||||
if (value == 'delete') {
|
||||
await axios.storage.remove(cacheKey);
|
||||
continue;
|
||||
@ -56,9 +54,7 @@ export function applyResponseInterceptor(axios: AxiosCacheInstance) {
|
||||
} else {
|
||||
// If the cache expiration has not been set, use the default expiration.
|
||||
cache.expiration =
|
||||
cache.expiration ||
|
||||
response.config.cache?.maxAge ||
|
||||
axios.defaults.cache!.maxAge!;
|
||||
cache.expiration || response.config.cache?.maxAge || axios.defaults.cache!.maxAge!;
|
||||
}
|
||||
|
||||
const data = { body: response.data, headers: response.headers };
|
||||
|
||||
@ -3,16 +3,11 @@ import { CacheStorage, StorageValue } from './types';
|
||||
* A storage that uses any {@link Storage} as his storage.
|
||||
*/
|
||||
export abstract class WindowStorageWrapper implements CacheStorage {
|
||||
constructor(
|
||||
readonly storage: Storage,
|
||||
readonly prefix: string = 'axios-cache:'
|
||||
) {}
|
||||
constructor(readonly storage: Storage, readonly prefix: string = 'axios-cache:') {}
|
||||
|
||||
get = async (key: string): Promise<StorageValue> => {
|
||||
const json = this.storage.getItem(this.prefix + key);
|
||||
return json
|
||||
? JSON.parse(json)
|
||||
: { data: null, expiration: -1, state: 'empty' };
|
||||
return json ? JSON.parse(json) : { data: null, expiration: -1, state: 'empty' };
|
||||
};
|
||||
|
||||
set = async (key: string, value: StorageValue): Promise<void> => {
|
||||
|
||||
@ -36,14 +36,8 @@ export class Deferred<T> {
|
||||
* @returns A Promise for the completion of which ever callback is executed.
|
||||
*/
|
||||
public readonly then = <TResult1 = T, TResult2 = never>(
|
||||
onfulfilled?:
|
||||
| ((value: T) => TResult1 | PromiseLike<TResult1>)
|
||||
| undefined
|
||||
| null,
|
||||
onrejected?:
|
||||
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
|
||||
| undefined
|
||||
| null
|
||||
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
|
||||
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
|
||||
): Promise<TResult1 | TResult2> => {
|
||||
return this.promise.then(onfulfilled, onrejected);
|
||||
};
|
||||
@ -54,10 +48,7 @@ export class Deferred<T> {
|
||||
* @returns A Promise for the completion of the callback.
|
||||
*/
|
||||
public readonly catch = <TResult = never>(
|
||||
onrejected?:
|
||||
| ((reason: any) => TResult | PromiseLike<TResult>)
|
||||
| undefined
|
||||
| null
|
||||
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null
|
||||
): Promise<T | TResult> => {
|
||||
return this.promise.catch(onrejected);
|
||||
};
|
||||
@ -68,9 +59,7 @@ export class Deferred<T> {
|
||||
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
|
||||
* @returns A Promise for the completion of the callback.
|
||||
*/
|
||||
public readonly finally = (
|
||||
onfinally?: (() => void) | undefined | null
|
||||
): Promise<T> => {
|
||||
public readonly finally = (onfinally?: (() => void) | undefined | null): Promise<T> => {
|
||||
return this.promise.finally(onfinally);
|
||||
};
|
||||
}
|
||||
|
||||
@ -9,7 +9,5 @@ export function defaultKeyGenerator({
|
||||
}: CacheRequestConfig): string {
|
||||
return id
|
||||
? `id::${String(id)}`
|
||||
: `${method?.toLowerCase() || 'get'}::${baseURL}::${url}::${JSON.stringify(
|
||||
params || '{}'
|
||||
)}`;
|
||||
: `${method?.toLowerCase() || 'get'}::${baseURL}::${url}::${JSON.stringify(params || '{}')}`;
|
||||
}
|
||||
|
||||
@ -2,6 +2,11 @@
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@arthurfiorette/prettier-config@^1.0.5":
|
||||
version "1.0.5"
|
||||
resolved "https://registry.yarnpkg.com/@arthurfiorette/prettier-config/-/prettier-config-1.0.5.tgz#65447a3830768907f7a3806cbc1eca7c445f98d0"
|
||||
integrity sha512-ZVGhdM+LVt3T0IfceBlVECb6UsgFJstNBb3BtjXUv8kcVHGmxhj/PGzgBTMeS7cHQJaYQAtoB1qiKsgSc8wv1w==
|
||||
|
||||
"@babel/code-frame@7.12.11":
|
||||
version "7.12.11"
|
||||
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.11.tgz#f4ad435aa263db935b8f10f2c552d23fb716a63f"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user