import { FunctionN, Lazy, Predicate } from 'fp-ts/lib/function'; import { Monad2 } from 'fp-ts/lib/Monad'; import { Foldable2 } from 'fp-ts/lib/Foldable'; import { Alt2 } from 'fp-ts/lib/Alt'; import { Extend2 } from 'fp-ts/lib/Extend'; import { Traversable2 } from 'fp-ts/lib/Traversable'; import { Bifunctor2 } from 'fp-ts/lib/Bifunctor'; import { Option } from 'fp-ts/lib/Option'; import { Either } from 'fp-ts/lib/Either'; import { Eq } from 'fp-ts/lib/Eq'; import { Alternative2 } from 'fp-ts/lib/Alternative'; import { Ord } from 'fp-ts/lib/Ord'; import { Semigroup } from 'fp-ts/lib/Semigroup'; import { Monoid } from 'fp-ts/lib/Monoid'; import { Show } from 'fp-ts/lib/Show'; export declare const URI = "RemoteData"; export declare type URI = typeof URI; declare module 'fp-ts/lib/HKT' { interface URItoKind2 { RemoteData: RemoteData; } } export declare type RemoteProgress = { readonly loaded: number; readonly total: Option; }; export declare type RemoteInitial = { readonly _tag: 'RemoteInitial'; }; export declare type RemotePending = { readonly _tag: 'RemotePending'; readonly progress: Option; }; export declare type RemoteFailure = { readonly _tag: 'RemoteFailure'; readonly error: E; }; export declare type RemoteSuccess = { readonly _tag: 'RemoteSuccess'; readonly value: A; }; /** * Represents a value of one of four possible types (a disjoint union) * * An instance of {@link RemoteData} is either an instance of {@link RemoteInitial}, {@link RemotePending}, {@link RemoteFailure} or {@link RemoteSuccess} * * A common use of {@link RemoteData} is as an alternative to `Either` or `Option` supporting initial and pending states (fits best with [RXJS]{@link https://github.com/ReactiveX/rxjs/}). * * Note: {@link RemoteInitial}, {@link RemotePending} and {@link RemoteFailure} are commonly called "Left" part in jsDoc. * * @see https://medium.com/@gcanti/slaying-a-ui-antipattern-with-flow-5eed0cfb627b * */ export declare type RemoteData = RemoteInitial | RemotePending | RemoteFailure | RemoteSuccess; export declare const failure: (error: E) => RemoteData; export declare const success: (value: A) => RemoteData; export declare const pending: RemoteData; export declare const progress: (progress: RemoteProgress) => RemoteData; export declare const initial: RemoteData; /** * Returns true only if {@link RemoteData} is {@link RemoteFailure} */ export declare const isFailure: (data: RemoteData) => data is RemoteFailure; /** * Returns true only if {@link RemoteData} is {@link RemoteSuccess} */ export declare const isSuccess: (data: RemoteData) => data is RemoteSuccess; /** * Returns true only if {@link RemoteData} is {@link RemotePending} */ export declare const isPending: (data: RemoteData) => data is RemotePending; /** * Returns true only if {@link RemoteData} is {@link RemoteInitial} */ export declare const isInitial: (data: RemoteData) => data is RemoteInitial; /** * Takes a default value as an argument. * If this {@link RemoteData} is "Left" part it will return default value. * If this {@link RemoteData} is {@link RemoteSuccess} it will return it's value ("wrapped" value, not default value) * * Note: Default value should be the same type as {@link RemoteData} (internal) value, if you want to pass different type as default, use {@link fold}. * * @example * getOrElse(() => 999)(some(1)) // 1 * getOrElseValue(() => 999)(initial) // 999 */ export declare const getOrElse: (f: Lazy) => (ma: RemoteData) => A; /** * Needed for "unwrap" value from {@link RemoteData} "container". * It applies a function to each case in the data structure. * * @example * const onInitial = () => "it's initial" * const onPending = () => "it's pending" * const onFailure = (err) => "it's failure" * const onSuccess = (data) => `${data + 1}` * const f = fold(onInitial, onPending, onFailure, onSuccess) * * f(initial) // "it's initial" * f(pending) // "it's pending" * f(failure(new Error('error text'))) // "it's failure" * f(success(21)) // '22' */ export declare const fold: (onInitial: () => B, onPending: (progress: Option) => B, onFailure: (error: E) => B, onSuccess: (value: A) => B) => (ma: RemoteData) => B; /** * A more concise way to "unwrap" values from {@link RemoteData} "container". * It uses fold in its implementation, collapsing `onInitial` and `onPending` on the `onNone` handler. * When fold's `onInitial` returns, `onNode` is called with `none`. * * @example * const onNone = (progressOption) => "no data to show" * const onFailure = (err) => "sorry, the request failed" * const onSuccess = (data) => `result is: ${data + 1}` * const f = fold(onInitial, onPending, onFailure, onSuccess) * * f(initial) // "no data to show" * f(pending) // "no data to show" * f(failure(new Error('error text'))) // "sorry, the request failed" * f(success(21)) // "result is: 22" */ export declare const fold3: (onNone: (progress: Option) => R, onFailure: (e: E) => R, onSuccess: (a: A) => R) => (fa: RemoteData) => R; /** * One more way to fold (unwrap) value from {@link RemoteData}. * `Left` part will return `null`. * {@link RemoteSuccess} will return value. * * For example: * * `success(2).toNullable() will return 2` * * `initial.toNullable() will return null` * * `pending.toNullable() will return null` * * `failure(new Error('error text)).toNullable() will return null` * */ export declare const toNullable: (ma: RemoteData) => A | null; export declare const toUndefined: (ma: RemoteData) => A | undefined; export declare function fromOption(option: Option, error: Lazy): RemoteData; /** * Convert {@link RemoteData} to {@link Option} * `Left` part will be converted to {@link None}. * {@link RemoteSuccess} will be converted to {@link Some}. * * @example * toOption(success(2)) // some(2) * toOption(initial) // none * toOption(pending) // none * toOption(failure(new Error('error text'))) // none */ export declare function toOption(data: RemoteData): Option; /** * Creates {@link RemoteData} from {@link Either} */ export declare const fromEither: (ea: Either) => RemoteData; /** * Convert {@link RemoteData} to `Either`. * `Left` part will be converted to `Left`. * Since {@link RemoteInitial} and {@link RemotePending} do not have `L` values, * you must provide a value of type `L` that will be used to construct * the `Left` for those two cases. * {@link RemoteSuccess} will be converted to `Right`. * * @example: * const f = toEither( * () => new Error('Data not fetched'), * () => new Error('Data is fetching') * ) * f(success(2)) // right(2) * f(initial) // right(Error('Data not fetched')) * f(pending) // right(Error('Data is fetching')) * f(failure(new Error('error text'))) // right(Error('error text')) */ export declare function toEither(onInitial: () => E, onPending: () => E): (data: RemoteData) => Either; export declare function fromPredicate(predicate: Predicate, whenFalse: FunctionN<[A], L>): FunctionN<[A], RemoteData>; /** * Create {@link RemoteData} from {@link ProgressEvent} * @param event */ export declare function fromProgressEvent(event: ProgressEvent): RemoteData; /** * Compare values and returns `true` if they are identical, otherwise returns `false`. * `Left` part will return `false`. * {@link RemoteSuccess} will call {@link Eq.equals}. * * If you want to compare {@link RemoteData}'s values better use {@link getEq} or {@link getOrd} helpers. * */ export declare function elem(E: Eq): (a: A, fa: RemoteData) => boolean; /** * Takes a predicate and apply it to {@link RemoteSuccess} value. * `Left` part will return `false`. */ export declare function exists(p: Predicate): (fa: RemoteData) => boolean; /** * Maps this RemoteFailure error into RemoteSuccess if passed function `f` return {@link Some} value, otherwise returns self */ export declare function recover(f: (error: E) => Option): (fa: RemoteData) => RemoteData; /** * Recovers {@link RemoteFailure} also mapping {@link RemoteSuccess} case * @see {@link recover} */ export declare function recoverMap(f: (error: E) => Option, g: (value: A) => B): (fa: RemoteData) => RemoteData; export declare const remoteData: Monad2 & Foldable2 & Traversable2 & Bifunctor2 & Alt2 & Extend2 & Alternative2; export declare const getEq: (EE: Eq, EA: Eq) => Eq>; export declare const getOrd: (OE: Ord, OA: Ord) => Ord>; export declare const getSemigroup: (SE: Semigroup, SA: Semigroup) => Semigroup>; export declare const getMonoid: (SL: Semigroup, SA: Semigroup) => Monoid>; export declare const getShow: (SE: Show, SA: Show) => Show>; declare const alt: (that: Lazy>) => (fa: RemoteData) => RemoteData, ap: (fa: RemoteData) => (fab: RemoteData B>) => RemoteData, apFirst: (fb: RemoteData) => (fa: RemoteData) => RemoteData, apSecond: (fb: RemoteData) => (fa: RemoteData) => RemoteData, bimap: (f: (e: E) => G, g: (a: A) => B) => (fa: RemoteData) => RemoteData, chain: (f: (a: A) => RemoteData) => (ma: RemoteData) => RemoteData, chainFirst: (f: (a: A) => RemoteData) => (ma: RemoteData) => RemoteData, duplicate: (wa: RemoteData) => RemoteData>, extend: (f: (wa: RemoteData) => B) => (wa: RemoteData) => RemoteData, flatten: (mma: RemoteData>) => RemoteData, foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: RemoteData) => M, map: (f: (a: A) => B) => (fa: RemoteData) => RemoteData, mapLeft: (f: (e: E) => G) => (fa: RemoteData) => RemoteData, reduce: (b: B, f: (b: B, a: A) => B) => (fa: RemoteData) => B, reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: RemoteData) => B; export { alt, ap, apFirst, apSecond, bimap, chain, chainFirst, duplicate, extend, flatten, foldMap, map, mapLeft, reduce, reduceRight, }; export declare function combine(a: RemoteData): RemoteData; export declare function combine(a: RemoteData, b: RemoteData): RemoteData; export declare function combine(a: RemoteData, b: RemoteData, c: RemoteData): RemoteData; export declare function combine(a: RemoteData, b: RemoteData, c: RemoteData, d: RemoteData): RemoteData; export declare function combine(a: RemoteData, b: RemoteData, c: RemoteData, d: RemoteData, e: RemoteData): RemoteData; export declare function combine(a: RemoteData, b: RemoteData, c: RemoteData, d: RemoteData, e: RemoteData, f: RemoteData): RemoteData;