From ff73ad5c690fb2bc44467cda76702e676f3e7d33 Mon Sep 17 00:00:00 2001 From: arthurfiorette Date: Mon, 10 Jan 2022 13:30:31 -0300 Subject: [PATCH] fix: await unawaited promises and eslint more severe --- .eslintrc | 8 +++++++- jest.config.js | 2 ++ package.json | 2 +- src/interceptors/request.ts | 4 +++- src/interceptors/response.ts | 4 ++-- src/storage/build.ts | 1 + src/storage/web-api.ts | 3 ++- src/util/key-generator.ts | 1 + test/interceptors/request.test.ts | 4 ++-- test/interceptors/util.test.ts | 2 +- test/storage/is-storage.test.ts | 3 ++- test/util/cache-predicate.test.ts | 6 +++--- 12 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.eslintrc b/.eslintrc index c010189..1bf60dd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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" } } diff --git a/jest.config.js b/jest.config.js index 1de8eed..dac52e0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,5 @@ +/* eslint-ignore */ + /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'ts-jest', diff --git a/package.json b/package.json index 5b8b83b..e33280a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index d4a2b70..254a127 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -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> => ({ config, data: cachedResponse.data, diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 219eaaf..e2ff68a 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -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 diff --git a/src/storage/build.ts b/src/storage/build.ts index f0e1c99..6b707f2 100644 --- a/src/storage/build.ts +++ b/src/storage/build.ts @@ -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 & { diff --git a/src/storage/web-api.ts b/src/storage/web-api.ts index 8f9a12a..b0f3466 100644 --- a/src/storage/web-api.ts +++ b/src/storage/web-api.ts @@ -1,3 +1,4 @@ +import type { StorageValue } from '..'; import { buildStorage } from './build'; /** @@ -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) diff --git a/src/util/key-generator.ts b/src/util/key-generator.ts index f1c7f29..3587151 100644 --- a/src/util/key-generator.ts +++ b/src/util/key-generator.ts @@ -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()) : '{}' }`; }; diff --git a/test/interceptors/request.test.ts b/test/interceptors/request.test.ts index 94b2d61..b1fd8e8 100644 --- a/test/interceptors/request.test.ts +++ b/test/interceptors/request.test.ts @@ -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'); @@ -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'); diff --git a/test/interceptors/util.test.ts b/test/interceptors/util.test.ts index 715afe2..3549b1f 100644 --- a/test/interceptors/util.test.ts +++ b/test/interceptors/util.test.ts @@ -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); diff --git a/test/storage/is-storage.test.ts b/test/storage/is-storage.test.ts index d8a5b84..690e886 100644 --- a/test/storage/is-storage.test.ts +++ b/test/storage/is-storage.test.ts @@ -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'; @@ -20,7 +21,7 @@ it('tests isStorage function', () => { it('tests setupCache without proper storage', () => { expect(() => mockAxios({ - storage: {} as any + storage: {} as AxiosStorage }) ).toThrowError(); }); diff --git a/test/util/cache-predicate.test.ts b/test/util/cache-predicate.test.ts index f3431e8..21bd784 100644 --- a/test/util/cache-predicate.test.ts +++ b/test/util/cache-predicate.test.ts @@ -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; @@ -232,6 +232,6 @@ describe('tests cache predicate object', () => { } }); - expect(result).resolves.toBeDefined(); + expect(result).toBeDefined(); }); });