From 560d043cdab09c15b3ccf58e3d40ef07f5e682f8 Mon Sep 17 00:00:00 2001 From: Simone Busoli Date: Wed, 28 Sep 2022 19:09:10 +0200 Subject: [PATCH] feat: add tsd, dump lru, fix typing (#1059) --- package.json | 9 ++++++--- src/index.d.ts | 13 +++++++++---- src/index.js | 7 +++++-- test-d/index.test-d.ts | 24 ++++++++++++++++++++++++ test/index.test.jsx | 9 ++++++--- 5 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 test-d/index.test-d.ts diff --git a/package.json b/package.json index 3b634f1..321599e 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,12 @@ "prepare": "npm run clean && npm run build && husky install", "release": "standard-version", "pretest": "cp ./test/index.test.jsx ./test/index.test.tsx && cp ./test/index.test.ssr.jsx ./test/index.test.ssr.tsx", - "test": "jest --no-cache" + "test": "tsd && jest --no-cache" }, "dependencies": { "@babel/runtime": "7.19.0", "dequal": "2.0.3", - "lru-cache": "6.0.0" + "lru-cache": "^7.14.0" }, "peerDependencies": { "axios": ">=0.24.0", @@ -52,7 +52,6 @@ "@testing-library/react": "12.1.5", "@testing-library/react-hooks": "7.0.2", "@types/jest": "29.0.3", - "@types/lru-cache": "7.10.10", "@types/node": "18.7.21", "@types/react": "18.0.21", "@types/react-dom": "18.0.6", @@ -75,11 +74,15 @@ "rimraf": "3.0.2", "standard-version": "9.5.0", "ts-jest": "26.5.6", + "tsd": "^0.24.1", "typescript": "4.8.4" }, "lint-staged": { "{src,test}/**/*.{js?(x),md}": [ "eslint --fix" ] + }, + "tsd": { + "directory": "test-d" } } diff --git a/src/index.d.ts b/src/index.d.ts index e95dfa9..7c41741 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -32,12 +32,17 @@ export interface ConfigureOptions { defaultOptions?: Options } +export interface RefetchFunction { + ( + config?: AxiosRequestConfig | string, + options?: RefetchOptions + ): AxiosPromise + (e: Event): AxiosPromise +} + export type UseAxiosResult = [ ResponseValues, - ( - config?: AxiosRequestConfig, - options?: RefetchOptions - ) => AxiosPromise, + RefetchFunction, () => void ] diff --git a/src/index.js b/src/index.js index 656d400..473ded8 100644 --- a/src/index.js +++ b/src/index.js @@ -59,6 +59,9 @@ function configToObject(config) { } export function makeUseAxios(configureOptions) { + /** + * @type {import('lru-cache')} + */ let cache let axiosInstance let defaultOptions @@ -66,7 +69,7 @@ export function makeUseAxios(configureOptions) { const __ssrPromises = [] function resetConfigure() { - cache = new LRU() + cache = new LRU({ max: 500 }) axiosInstance = StaticAxios defaultOptions = DEFAULT_OPTIONS } @@ -103,7 +106,7 @@ export function makeUseAxios(configureOptions) { } function clearCache() { - cache.reset() + cache.clear() } return Object.assign(useAxios, { diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts new file mode 100644 index 0000000..339bab0 --- /dev/null +++ b/test-d/index.test-d.ts @@ -0,0 +1,24 @@ +import { expectAssignable, expectType } from 'tsd' +import { AxiosError, AxiosResponse } from 'axios' + +import useAxios from '../src' + +useAxios('') +useAxios( + { url: '' }, + { autoCancel: true, manual: true, ssr: true, useCache: true } +) + +const [{ data, loading, error, response }, refetch, cancel] = useAxios('') + +expectType(data) +expectType(loading) +expectAssignable | null>(error) +expectAssignable(response) +expectAssignable(refetch) +expectAssignable(cancel) + +refetch('') +refetch({ url: '' }, { useCache: true }) +refetch(new MouseEvent('click')) +cancel() diff --git a/test/index.test.jsx b/test/index.test.jsx index 680f879..9782fe3 100644 --- a/test/index.test.jsx +++ b/test/index.test.jsx @@ -12,6 +12,7 @@ import defaultUseAxios, { makeUseAxios } from '../src' import { mockCancelToken } from './testUtils' +import LRUCache from 'lru-cache' jest.mock('axios') @@ -1151,13 +1152,15 @@ function standardTests( describe('loadCache', () => { it('should load cache', () => { - loadCache({ some: 'data' }) + const cache = new LRUCache({ max: 1 }) + + loadCache(cache.dump()) }) }) describe('serializeCache', () => { - it('should serialize cache', () => { - serializeCache() + it('should serialize cache', async () => { + await serializeCache() }) }) }