/** * @since 1.0.0 */ import * as RD from '@jvlk/fp-ts-remote-data' import { useQuery } from '@tanstack/react-query' import * as E from 'fp-ts/Either' import { flow, pipe } from 'fp-ts/function' import * as O from 'fp-ts/Option' import * as TE from 'fp-ts/TaskEither' import * as t from 'io-ts' import decode from './decode' type OptionsWithDecoder = { queryKey: string[] queryFn: TE.TaskEither> decoder: t.Decoder } type OptionsWithoutDecoder = { queryKey: string[] queryFn: TE.TaskEither> } type Options = { queryKey: string[] queryFn: TE.TaskEither> decoder?: t.Decoder } /** * @since 1.0.0 */ export default function useRemoteDataQuery( options: OptionsWithDecoder ): RD.RemoteData export default function useRemoteDataQuery( options: OptionsWithoutDecoder ): RD.RemoteData export default function useRemoteDataQuery(options: Options) { const { isLoading, data } = useQuery(options) return isLoading ? RD.loading : data ? pipe( data, decode(options.decoder), E.foldW(RD.failure, flow(O.fold(() => RD.empty, RD.success))) ) : RD.empty }