Skip to content

Commit 560d043

Browse files
authored
feat: add tsd, dump lru, fix typing (#1059)
1 parent 4320e4b commit 560d043

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
"prepare": "npm run clean && npm run build && husky install",
3131
"release": "standard-version",
3232
"pretest": "cp ./test/index.test.jsx ./test/index.test.tsx && cp ./test/index.test.ssr.jsx ./test/index.test.ssr.tsx",
33-
"test": "jest --no-cache"
33+
"test": "tsd && jest --no-cache"
3434
},
3535
"dependencies": {
3636
"@babel/runtime": "7.19.0",
3737
"dequal": "2.0.3",
38-
"lru-cache": "6.0.0"
38+
"lru-cache": "^7.14.0"
3939
},
4040
"peerDependencies": {
4141
"axios": ">=0.24.0",
@@ -52,7 +52,6 @@
5252
"@testing-library/react": "12.1.5",
5353
"@testing-library/react-hooks": "7.0.2",
5454
"@types/jest": "29.0.3",
55-
"@types/lru-cache": "7.10.10",
5655
"@types/node": "18.7.21",
5756
"@types/react": "18.0.21",
5857
"@types/react-dom": "18.0.6",
@@ -75,11 +74,15 @@
7574
"rimraf": "3.0.2",
7675
"standard-version": "9.5.0",
7776
"ts-jest": "26.5.6",
77+
"tsd": "^0.24.1",
7878
"typescript": "4.8.4"
7979
},
8080
"lint-staged": {
8181
"{src,test}/**/*.{js?(x),md}": [
8282
"eslint --fix"
8383
]
84+
},
85+
"tsd": {
86+
"directory": "test-d"
8487
}
8588
}

src/index.d.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ export interface ConfigureOptions {
3232
defaultOptions?: Options
3333
}
3434

35-
export type UseAxiosResult<TResponse = any, TBody = any, TError = any> = [
36-
ResponseValues<TResponse, TBody, TError>,
35+
export interface RefetchFunction<TBody, TResponse> {
3736
(
38-
config?: AxiosRequestConfig<TBody>,
37+
config?: AxiosRequestConfig<TBody> | string,
3938
options?: RefetchOptions
40-
) => AxiosPromise<TResponse>,
39+
): AxiosPromise<TResponse>
40+
(e: Event): AxiosPromise<TResponse>
41+
}
42+
43+
export type UseAxiosResult<TResponse = any, TBody = any, TError = any> = [
44+
ResponseValues<TResponse, TBody, TError>,
45+
RefetchFunction<TBody, TResponse>,
4146
() => void
4247
]
4348

src/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@ function configToObject(config) {
5959
}
6060

6161
export function makeUseAxios(configureOptions) {
62+
/**
63+
* @type {import('lru-cache')}
64+
*/
6265
let cache
6366
let axiosInstance
6467
let defaultOptions
6568

6669
const __ssrPromises = []
6770

6871
function resetConfigure() {
69-
cache = new LRU()
72+
cache = new LRU({ max: 500 })
7073
axiosInstance = StaticAxios
7174
defaultOptions = DEFAULT_OPTIONS
7275
}
@@ -103,7 +106,7 @@ export function makeUseAxios(configureOptions) {
103106
}
104107

105108
function clearCache() {
106-
cache.reset()
109+
cache.clear()
107110
}
108111

109112
return Object.assign(useAxios, {

test-d/index.test-d.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expectAssignable, expectType } from 'tsd'
2+
import { AxiosError, AxiosResponse } from 'axios'
3+
4+
import useAxios from '../src'
5+
6+
useAxios('')
7+
useAxios(
8+
{ url: '' },
9+
{ autoCancel: true, manual: true, ssr: true, useCache: true }
10+
)
11+
12+
const [{ data, loading, error, response }, refetch, cancel] = useAxios('')
13+
14+
expectType<any>(data)
15+
expectType<boolean>(loading)
16+
expectAssignable<AxiosError<any, any> | null>(error)
17+
expectAssignable<AxiosResponse | undefined>(response)
18+
expectAssignable<Function>(refetch)
19+
expectAssignable<Function>(cancel)
20+
21+
refetch('')
22+
refetch({ url: '' }, { useCache: true })
23+
refetch(new MouseEvent('click'))
24+
cancel()

test/index.test.jsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import defaultUseAxios, {
1212
makeUseAxios
1313
} from '../src'
1414
import { mockCancelToken } from './testUtils'
15+
import LRUCache from 'lru-cache'
1516

1617
jest.mock('axios')
1718

@@ -1151,13 +1152,15 @@ function standardTests(
11511152

11521153
describe('loadCache', () => {
11531154
it('should load cache', () => {
1154-
loadCache({ some: 'data' })
1155+
const cache = new LRUCache({ max: 1 })
1156+
1157+
loadCache(cache.dump())
11551158
})
11561159
})
11571160

11581161
describe('serializeCache', () => {
1159-
it('should serialize cache', () => {
1160-
serializeCache()
1162+
it('should serialize cache', async () => {
1163+
await serializeCache()
11611164
})
11621165
})
11631166
}

0 commit comments

Comments
 (0)