forked from polka_billy/porridger
Fixed useFetch and useUser typing
This commit is contained in:
@ -3,40 +3,31 @@ import { useEffect, useState } from 'react'
|
||||
import { SetState } from '../utils/types'
|
||||
import useSend from './useSend'
|
||||
|
||||
type UseFetchShared = {
|
||||
type UseFetchShared<T> = {
|
||||
loading: boolean,
|
||||
abort?: () => void,
|
||||
setData: SetState<T | undefined>
|
||||
}
|
||||
|
||||
type UseFetchSucced<T> = {
|
||||
error: null,
|
||||
data: T,
|
||||
} & UseFetchShared
|
||||
} & UseFetchShared<T>
|
||||
|
||||
type UseFetchErrored = {
|
||||
type UseFetchErrored<T> = {
|
||||
error: string,
|
||||
data: undefined
|
||||
} & UseFetchShared
|
||||
} & UseFetchShared<T>
|
||||
|
||||
const gotError = <T>(res: UseFetchErrored | UseFetchSucced<T>): res is UseFetchErrored => (
|
||||
const gotError = <T>(res: UseFetchErrored<T> | UseFetchSucced<T>): res is UseFetchErrored<T> => (
|
||||
typeof res.error === 'string'
|
||||
)
|
||||
|
||||
const fallbackError = <T>(res: UseFetchSucced<T> | UseFetchErrored) => (
|
||||
const fallbackError = <T>(res: UseFetchSucced<T> | UseFetchErrored<T>) => (
|
||||
gotError(res) ? res.error : res.data
|
||||
)
|
||||
|
||||
type UseFetchReturn<T> = ({
|
||||
error: null,
|
||||
data: T
|
||||
} | {
|
||||
error: string,
|
||||
data: undefined
|
||||
}) & {
|
||||
loading: boolean,
|
||||
setData: SetState<T | undefined>
|
||||
abort?: (() => void)
|
||||
}
|
||||
type UseFetchReturn<T> = UseFetchSucced<T> | UseFetchErrored<T>
|
||||
|
||||
function useFetch<R, T>(
|
||||
url: string,
|
||||
@ -70,7 +61,7 @@ function useFetch<R, T>(
|
||||
}
|
||||
}
|
||||
|
||||
export type { UseFetchErrored, UseFetchSucced }
|
||||
export type { UseFetchReturn }
|
||||
|
||||
export default useFetch
|
||||
|
||||
|
Reference in New Issue
Block a user