我们有一个很好的例子,说明如何在官方仓库https://github.com/vercel/swr/blob/master/examples/axios-typescript/libs/useRequest.ts上使用 axios 和 typescript 的 useSWR 钩子
import useSWR, { SWRConfiguration, SWRResponse } from 'swr'
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
export type GetRequest = AxiosRequestConfig | null
interface Return<Data, Error>
extends Pick<
SWRResponse<AxiosResponse<Data>, AxiosError<Error>>,
'isValidating' | 'revalidate' | 'error' | 'mutate'
> {
data: Data | undefined
response: AxiosResponse<Data> | undefined
}
export interface Config<Data = unknown, Error = unknown>
extends Omit<
SWRConfiguration<AxiosResponse<Data>, AxiosError<Error>>,
'initialData'
> {
initialData?: Data
}
export default function useRequest<Data = unknown, Error = unknown>(
request: GetRequest,
{ initialData, ...config }: Config<Data, Error> = {}
): Return<Data, Error> {
const { data: response, error, isValidating, revalidate, mutate } = useSWR<
AxiosResponse<Data>,
AxiosError<Error>
>(
request && JSON.stringify(request),
/**
* NOTE: Typescript thinks `request` can be `null` here, but the fetcher
* function is actually only called by `useSWR` when it isn't.
*/
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
() => axios(request!),
{
...config,
initialData: initialData && {
status: 200,
statusText: 'InitialData',
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
config: request!,
headers: {},
data: initialData
}
}
)
return {
data: response && response.data,
response,
error,
isValidating,
revalidate,
mutate
}
}
如何实现将 useSWRInfinite 与 axios 和 typescript 一起使用的示例?