UNPKG

use-query-rd

Version:

A drop in replacement for apollo client's `useQuery` hook with a return type that mimics Elm's RemoteData ADT

66 lines 2.84 kB
import { type ApolloError, type QueryResult } from '@apollo/client'; /** * @name RemoteData * @description A datatype representing fetched data. * @see https://package.elm-lang.org/packages/krisajenkins/remotedata/latest/ * @inspo http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html */ export type RemoteData<D> = Initialized | Pending | Failure | Success<D>; export declare enum Tags { Initialized = "Initialized", Pending = "Pending", Failure = "Failure", Success = "Success", _ = "_" } export interface Initialized { readonly tag: Tags.Initialized; } export interface Pending { readonly tag: Tags.Pending; } export interface Failure { readonly tag: Tags.Failure; error: Required<Pick<QueryResult, 'error'>>['error']; } export interface Success<A> { readonly tag: Tags.Success; data: A; } export declare const initialized: <T = never>() => RemoteData<T>; export declare const pending: <T = never>() => RemoteData<T>; export declare const failure: <T = never>(error: ApolloError) => RemoteData<T>; export declare const success: <D = never>(data: D) => RemoteData<D>; export declare const isInitialized: <D = never>(rd: RemoteData<D>) => rd is Initialized; export declare const isLoading: <D = never>(rd: RemoteData<D>) => rd is Pending; export declare const isFailure: <D = never>(rd: RemoteData<D>) => rd is Failure; export declare const isSuccess: <D = never>(rd: RemoteData<D>) => rd is Success<D>; export declare const fold: <T, D>(initialized: () => T, pending: () => T, failure: (error: ApolloError) => T, success: (data: D) => T) => (_: RemoteData<D>) => T; interface CompleteDataMatcher<T, D> { [Tags.Initialized]: () => T; [Tags.Pending]: () => T; [Tags.Failure]: (error: ApolloError) => T; [Tags.Success]: (data: D) => T; } interface PartialDataMatcher<T, D> { [Tags.Initialized]?: () => T; [Tags.Pending]?: () => T; [Tags.Failure]?: (error: ApolloError) => T; [Tags.Success]?: (data: D) => T; [Tags._]: () => T; } type Matcher<T, D> = CompleteDataMatcher<T, D> | PartialDataMatcher<T, D>; export declare const match: <T, D>(rd: RemoteData<D>, matcher: Matcher<T, D>) => T; export declare const map: <T, D>(f: (a: T) => D, fa: RemoteData<T>) => RemoteData<D>; /** * Combine two remote data sources with the given function. The result will succeed when (and if) both sources succeed. */ export declare const map2: <D, D2, D3>(f: (d: D) => (d2: D2) => D3, rd1: RemoteData<D>, rd2: RemoteData<D2>) => RemoteData<D3>; /** * Put the results of two RemoteData calls together. * @see https://github.com/krisajenkins/remotedata/blob/6.0.1/src/RemoteData.elm#L361 */ export declare const andMap: <RD1, RD2>(rd1: RemoteData<RD1>, rd2: RemoteData<(d: RD1) => RD2>) => RemoteData<RD2>; export {}; //# sourceMappingURL=rd.d.ts.map