import { useState , useEffect } from 'react' ; import { Try } from '@/utils/fp/Try' ; import { FetchError } from '@/utils/fetch' ; export type Service < T > = ( ) => Promise < Try < T , FetchError > > ; interface FetchOption < T > { /**
     * Try fail callback
     */ fail ? : ( err : FetchError ) => void ; /**
     * Try success callback
     */ success ? : ( data : T ) => void ; } param service param options export default function useFetcher < T > ( service , options = { } ) { const [ state , setState ] = useState < T | null > ( null ) ; useEffect ( ( ) => { if ( service === null ) { return ; } service ( ) . then ( res => { res . fail ( err => { options . fail && options . fail ( err ) ; } ) . succeed ( data => { options . success && options . success ( data ) ; setState ( data ) ; } ) ; } ) ; } , [ ] ) ; return [ state , setState ] as [ typeof state , typeof setState ] ; }