Skip to content

Commit

Permalink
fix: await unawaited promises and eslint more severe
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 10, 2022
1 parent c3661a6 commit ff73ad5
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 13 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:prettier/recommended"
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/await-thenable": "off"
},
"parserOptions": {
"ecmaVersion": "latest",
"project": "./tsconfig.json"
}
}
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-ignore */

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "jest --coverage",
"escheck": "es-check es5 ./dist/index.es5.min.js && es-check es6 ./dist/index.min.js",
"format": "prettier --write .",
"lint": "tsc --noEmit && eslint . --ext .ts",
"lint": "eslint . --ext .ts",
"version": "auto-changelog -p && git add CHANGELOG.md"
},
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion src/interceptors/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
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,
data: cachedResponse.data,
Expand Down
4 changes: 2 additions & 2 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ export function defaultResponseInterceptor(

// Update other entries before updating himself
if (cacheConfig?.update) {
updateCache(axios.storage, response, cacheConfig.update);
await updateCache(axios.storage, response, cacheConfig.update);
}

const deferred = axios.waiting[response.id];

// Resolve all other requests waiting for this response
await deferred?.resolve(newCache.data);
deferred?.resolve(newCache.data);
delete axios.waiting[response.id];

// Define this key as cache on the storage
Expand Down
1 change: 1 addition & 0 deletions src/storage/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { AxiosStorage, StaleStorageValue, StorageValue } from './types';
const storage = Symbol();

/** 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 type BuildStorage = Omit<AxiosStorage, 'get'> & {
Expand Down
3 changes: 2 additions & 1 deletion src/storage/web-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { StorageValue } from '..';
import { buildStorage } from './build';

/**
Expand All @@ -20,7 +21,7 @@ export function buildWebStorage(storage: Storage, prefix = '') {
return buildStorage({
find: (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)),
remove: (key) => void storage.removeItem(prefix + key)
Expand Down
1 change: 1 addition & 0 deletions src/util/key-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const defaultKeyGenerator: KeyGenerator = ({
baseURL + (baseURL && url ? '/' : '') + url
}::${
//params
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
params ? JSON.stringify(params, Object.keys(params).sort()) : '{}'
}`;
};
4 changes: 2 additions & 2 deletions test/interceptors/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('test request interceptor', () => {
});

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);

expect(cache.state).toBe('empty');
Expand All @@ -22,7 +22,7 @@ describe('test request interceptor', () => {
});

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);

expect(cache.state).toBe('cached');
Expand Down
2 changes: 1 addition & 1 deletion test/interceptors/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createValidateStatus, isMethodIn } from '../../src/interceptors/util';

describe('test util functions', () => {
it('tests validate-status function', async () => {
it('tests validate-status function', () => {
const def = createValidateStatus();
expect(def(200)).toBe(true);
expect(def(345)).toBe(false);
Expand Down
3 changes: 2 additions & 1 deletion test/storage/is-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Axios } from 'axios';
import type { AxiosStorage } from '../../src';
import { isStorage } from '../../src/storage/build';
import { buildMemoryStorage } from '../../src/storage/memory';
import { mockAxios } from '../mocks/axios';
Expand All @@ -20,7 +21,7 @@ it('tests isStorage function', () => {
it('tests setupCache without proper storage', () => {
expect(() =>
mockAxios({
storage: {} as any
storage: {} as AxiosStorage
})
).toThrowError();
});
6 changes: 3 additions & 3 deletions test/util/cache-predicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 result = axios.get<{ a: boolean; b: number }>('url', {
const result = await axios.get<{ a: boolean; b: number }>('url', {
cache: {
ttl: ({ data }) => {
return data.b;
Expand Down Expand Up @@ -232,6 +232,6 @@ describe('tests cache predicate object', () => {
}
});

expect(result).resolves.toBeDefined();
expect(result).toBeDefined();
});
});

0 comments on commit ff73ad5

Please sign in to comment.