mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
fix: await unawaited promises and eslint more severe
This commit is contained in:
parent
c3661a64e9
commit
ff73ad5c69
@ -7,9 +7,15 @@
|
|||||||
"eslint:recommended",
|
"eslint:recommended",
|
||||||
"plugin:@typescript-eslint/eslint-recommended",
|
"plugin:@typescript-eslint/eslint-recommended",
|
||||||
"plugin:@typescript-eslint/recommended",
|
"plugin:@typescript-eslint/recommended",
|
||||||
|
"plugin:@typescript-eslint/recommended-requiring-type-checking",
|
||||||
"plugin:prettier/recommended"
|
"plugin:prettier/recommended"
|
||||||
],
|
],
|
||||||
"rules": {
|
"rules": {
|
||||||
"@typescript-eslint/no-explicit-any": "off"
|
"@typescript-eslint/no-explicit-any": "off",
|
||||||
|
"@typescript-eslint/await-thenable": "off"
|
||||||
|
},
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": "latest",
|
||||||
|
"project": "./tsconfig.json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
/* eslint-ignore */
|
||||||
|
|
||||||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
||||||
module.exports = {
|
module.exports = {
|
||||||
preset: 'ts-jest',
|
preset: 'ts-jest',
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
"test": "jest --coverage",
|
"test": "jest --coverage",
|
||||||
"escheck": "es-check es5 ./dist/index.es5.min.js && es-check es6 ./dist/index.min.js",
|
"escheck": "es-check es5 ./dist/index.es5.min.js && es-check es6 ./dist/index.min.js",
|
||||||
"format": "prettier --write .",
|
"format": "prettier --write .",
|
||||||
"lint": "tsc --noEmit && eslint . --ext .ts",
|
"lint": "eslint . --ext .ts",
|
||||||
"version": "auto-changelog -p && git add CHANGELOG.md"
|
"version": "auto-changelog -p && git add CHANGELOG.md"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
@ -90,7 +90,9 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
|
|||||||
cachedResponse = cache.data;
|
cachedResponse = cache.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Even though the response interceptor receives this one from here, it has been configured to ignore cached responses: true
|
//Even though the response interceptor receives this one from here,
|
||||||
|
// it has been configured to ignore cached responses = true
|
||||||
|
// eslint-disable-next-line @typescript-eslint/require-await
|
||||||
config.adapter = async (): Promise<CacheAxiosResponse<unknown, unknown>> => ({
|
config.adapter = async (): Promise<CacheAxiosResponse<unknown, unknown>> => ({
|
||||||
config,
|
config,
|
||||||
data: cachedResponse.data,
|
data: cachedResponse.data,
|
||||||
|
|||||||
@ -109,13 +109,13 @@ export function defaultResponseInterceptor(
|
|||||||
|
|
||||||
// Update other entries before updating himself
|
// Update other entries before updating himself
|
||||||
if (cacheConfig?.update) {
|
if (cacheConfig?.update) {
|
||||||
updateCache(axios.storage, response, cacheConfig.update);
|
await updateCache(axios.storage, response, cacheConfig.update);
|
||||||
}
|
}
|
||||||
|
|
||||||
const deferred = axios.waiting[response.id];
|
const deferred = axios.waiting[response.id];
|
||||||
|
|
||||||
// Resolve all other requests waiting for this response
|
// Resolve all other requests waiting for this response
|
||||||
await deferred?.resolve(newCache.data);
|
deferred?.resolve(newCache.data);
|
||||||
delete axios.waiting[response.id];
|
delete axios.waiting[response.id];
|
||||||
|
|
||||||
// Define this key as cache on the storage
|
// Define this key as cache on the storage
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import type { AxiosStorage, StaleStorageValue, StorageValue } from './types';
|
|||||||
const storage = Symbol();
|
const storage = Symbol();
|
||||||
|
|
||||||
/** Returns true if the provided object was created from {@link buildStorage} function. */
|
/** Returns true if the provided object was created from {@link buildStorage} function. */
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||||
export const isStorage = (obj: any): obj is AxiosStorage => !!obj && !!obj[storage];
|
export const isStorage = (obj: any): obj is AxiosStorage => !!obj && !!obj[storage];
|
||||||
|
|
||||||
export type BuildStorage = Omit<AxiosStorage, 'get'> & {
|
export type BuildStorage = Omit<AxiosStorage, 'get'> & {
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import type { StorageValue } from '..';
|
||||||
import { buildStorage } from './build';
|
import { buildStorage } from './build';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -20,7 +21,7 @@ export function buildWebStorage(storage: Storage, prefix = '') {
|
|||||||
return buildStorage({
|
return buildStorage({
|
||||||
find: (key) => {
|
find: (key) => {
|
||||||
const json = storage.getItem(prefix + key);
|
const json = storage.getItem(prefix + key);
|
||||||
return json ? JSON.parse(json) : undefined;
|
return json ? (JSON.parse(json) as StorageValue) : undefined;
|
||||||
},
|
},
|
||||||
set: (key, value) => void storage.setItem(prefix + key, JSON.stringify(value)),
|
set: (key, value) => void storage.setItem(prefix + key, JSON.stringify(value)),
|
||||||
remove: (key) => void storage.removeItem(prefix + key)
|
remove: (key) => void storage.removeItem(prefix + key)
|
||||||
|
|||||||
@ -26,6 +26,7 @@ export const defaultKeyGenerator: KeyGenerator = ({
|
|||||||
baseURL + (baseURL && url ? '/' : '') + url
|
baseURL + (baseURL && url ? '/' : '') + url
|
||||||
}::${
|
}::${
|
||||||
//params
|
//params
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||||
params ? JSON.stringify(params, Object.keys(params).sort()) : '{}'
|
params ? JSON.stringify(params, Object.keys(params).sort()) : '{}'
|
||||||
}`;
|
}`;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -9,7 +9,7 @@ describe('test request interceptor', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const response = await axios.get('');
|
const response = await axios.get('');
|
||||||
const cacheKey = await axios.generateKey(response.config);
|
const cacheKey = axios.generateKey(response.config);
|
||||||
const cache = await axios.storage.get(cacheKey);
|
const cache = await axios.storage.get(cacheKey);
|
||||||
|
|
||||||
expect(cache.state).toBe('empty');
|
expect(cache.state).toBe('empty');
|
||||||
@ -22,7 +22,7 @@ describe('test request interceptor', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const response = await axios.get('');
|
const response = await axios.get('');
|
||||||
const cacheKey = await axios.generateKey(response.config);
|
const cacheKey = axios.generateKey(response.config);
|
||||||
const cache = await axios.storage.get(cacheKey);
|
const cache = await axios.storage.get(cacheKey);
|
||||||
|
|
||||||
expect(cache.state).toBe('cached');
|
expect(cache.state).toBe('cached');
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { createValidateStatus, isMethodIn } from '../../src/interceptors/util';
|
import { createValidateStatus, isMethodIn } from '../../src/interceptors/util';
|
||||||
|
|
||||||
describe('test util functions', () => {
|
describe('test util functions', () => {
|
||||||
it('tests validate-status function', async () => {
|
it('tests validate-status function', () => {
|
||||||
const def = createValidateStatus();
|
const def = createValidateStatus();
|
||||||
expect(def(200)).toBe(true);
|
expect(def(200)).toBe(true);
|
||||||
expect(def(345)).toBe(false);
|
expect(def(345)).toBe(false);
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { Axios } from 'axios';
|
import { Axios } from 'axios';
|
||||||
|
import type { AxiosStorage } from '../../src';
|
||||||
import { isStorage } from '../../src/storage/build';
|
import { isStorage } from '../../src/storage/build';
|
||||||
import { buildMemoryStorage } from '../../src/storage/memory';
|
import { buildMemoryStorage } from '../../src/storage/memory';
|
||||||
import { mockAxios } from '../mocks/axios';
|
import { mockAxios } from '../mocks/axios';
|
||||||
@ -20,7 +21,7 @@ it('tests isStorage function', () => {
|
|||||||
it('tests setupCache without proper storage', () => {
|
it('tests setupCache without proper storage', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
mockAxios({
|
mockAxios({
|
||||||
storage: {} as any
|
storage: {} as AxiosStorage
|
||||||
})
|
})
|
||||||
).toThrowError();
|
).toThrowError();
|
||||||
});
|
});
|
||||||
|
|||||||
@ -198,10 +198,10 @@ describe('tests cache predicate object', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tests generics and typescript types', () => {
|
it('tests generics and typescript types', async () => {
|
||||||
const axios = mockAxios();
|
const axios = mockAxios();
|
||||||
|
|
||||||
const result = axios.get<{ a: boolean; b: number }>('url', {
|
const result = await axios.get<{ a: boolean; b: number }>('url', {
|
||||||
cache: {
|
cache: {
|
||||||
ttl: ({ data }) => {
|
ttl: ({ data }) => {
|
||||||
return data.b;
|
return data.b;
|
||||||
@ -232,6 +232,6 @@ describe('tests cache predicate object', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).resolves.toBeDefined();
|
expect(result).toBeDefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user