mirror of
https://github.com/simoneb/axios-hooks.git
synced 2025-12-08 21:25:56 +00:00
feat: add tsd, dump lru, fix typing (#1059)
This commit is contained in:
parent
4320e4b102
commit
560d043cda
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
13
src/index.d.ts
vendored
13
src/index.d.ts
vendored
@ -32,12 +32,17 @@ export interface ConfigureOptions {
|
||||
defaultOptions?: Options
|
||||
}
|
||||
|
||||
export interface RefetchFunction<TBody, TResponse> {
|
||||
(
|
||||
config?: AxiosRequestConfig<TBody> | string,
|
||||
options?: RefetchOptions
|
||||
): AxiosPromise<TResponse>
|
||||
(e: Event): AxiosPromise<TResponse>
|
||||
}
|
||||
|
||||
export type UseAxiosResult<TResponse = any, TBody = any, TError = any> = [
|
||||
ResponseValues<TResponse, TBody, TError>,
|
||||
(
|
||||
config?: AxiosRequestConfig<TBody>,
|
||||
options?: RefetchOptions
|
||||
) => AxiosPromise<TResponse>,
|
||||
RefetchFunction<TBody, TResponse>,
|
||||
() => void
|
||||
]
|
||||
|
||||
|
||||
@ -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, {
|
||||
|
||||
24
test-d/index.test-d.ts
Normal file
24
test-d/index.test-d.ts
Normal file
@ -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<any>(data)
|
||||
expectType<boolean>(loading)
|
||||
expectAssignable<AxiosError<any, any> | null>(error)
|
||||
expectAssignable<AxiosResponse | undefined>(response)
|
||||
expectAssignable<Function>(refetch)
|
||||
expectAssignable<Function>(cancel)
|
||||
|
||||
refetch('')
|
||||
refetch({ url: '' }, { useCache: true })
|
||||
refetch(new MouseEvent('click'))
|
||||
cancel()
|
||||
@ -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()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user