Options
All
  • Public
  • Public/Protected
  • All
Menu

@roebuk/remote-data

Index

Type aliases

Failed

Failed<E>: { error: E; type: RDType.Failed }

Type parameters

  • E

Type declaration

  • Readonly error: E
  • Readonly type: RDType.Failed

Loading

Loading: { type: RDType.Loading }

Type declaration

  • Readonly type: RDType.Loading

NotAsked

NotAsked: { type: RDType.NotAsked }

Type declaration

  • Readonly type: RDType.NotAsked

RemoteData

RemoteData<E, T>: NotAsked | Loading | Success<T> | Failed<E>

Type parameters

  • E

  • T

Success

Success<T>: { data: T; type: RDType.Success }

Type parameters

  • T

Type declaration

  • Readonly data: T
  • Readonly type: RDType.Success

Functions

Failed

  • Constructs a new Failed<E> variant

    example
    RemoteData.Failed("Unexpected token '<'")
    

    Type parameters

    • E

    • T

    Parameters

    • e: E

      Your errored value

    Returns RemoteData<E, T>

    RemoteData Failed object

Loading

  • Constructs a new Loading variant

    example
    RemoteData.Loading()
    

    Type parameters

    • E

    • T

    Returns RemoteData<E, T>

    RemoteData Loading object

NotAsked

  • Constructs a new NotAsked variant

    example
    RemoteData.NotAsked()
    

    Type parameters

    • E

    • T

    Returns RemoteData<E, T>

    RemoteData NotAsked object

Success

  • Constructs a new Success<T> variant

    example
    RemoteData.Success([1,2,3])
    

    Type parameters

    • E

    • T

    Parameters

    • t: T

      Your remote data, typically some parsed JSON

    Returns RemoteData<E, T>

    RemoteData Failed object

andThen

isFailure

  • Returns true if the variant is of type Failure

    example
    RemoteData.isFailure(remoteData)
    

    Type parameters

    • E

    • T

    Parameters

    Returns boolean

    Wether or not the RemoteData is in a Failed state.

isLoading

  • Returns true if the variant is of type Loading

    example
    RemoteData.isLoading()
    

    Type parameters

    • E

    • T

    Parameters

    Returns boolean

    Wether or not the RemoteData is in a Loading state.

isNotAsked

  • Returns true if the variant is of type NotAsked

    example
    RemoteData.isNotAsked()
    

    Type parameters

    • E

    • T

    Parameters

    Returns boolean

    Wether or not the RemoteData is in a NotAsked state.

isSuccess

  • Returns true if the variant is of type Success

    example
    RemoteData.isSuccess(remoteData)
    

    Type parameters

    • E

    • T

    Parameters

    Returns boolean

    Wether or not the RemoteData is in a successful state.

map

  • Transforms the value within a Success variant.

    example
    const data = RemoteData.Success(1)
    RemoteData.map(num => num + 1, data) // Success(2)

    Type parameters

    • E

    • T

    • U

    Parameters

    • fn: (d: T) => U

      A function that is passed the unwrapped success value and returns a new

        • (d: T): U
        • Parameters

          • d: T

          Returns U

    • rd: RemoteData<E, T>

    Returns RemoteData<E, U>

    Returns RemoteData with a modified Success value.

mapBoth

  • mapBoth<E, F, T, U>(mapSuccess: (d: T) => U, mapErr: (e: E) => F, rd: RemoteData<E, T>): RemoteData<F, U>
  • Transforms the value within a Success or Failed variant.

    Type parameters

    • E

    • F

    • T

    • U

    Parameters

    • mapSuccess: (d: T) => U

      A function that takes a Success value

        • (d: T): U
        • Parameters

          • d: T

          Returns U

    • mapErr: (e: E) => F

      A function maps over the Failed value

        • (e: E): F
        • Parameters

          • e: E

          Returns F

    • rd: RemoteData<E, T>

    Returns RemoteData<F, U>

    RemoteData with the adjusted values

mapError

  • Transforms the value within a Failed variant.

    example
    const data = RemoteData.Failed(new Error('Network Error'))
    RemoteData.mapError(err => err.message, data) // Failed<String>

    Type parameters

    • E

    • F

    • T

    Parameters

    • fn: (e: E) => F

      A function that maps over the Failed value

        • (e: E): F
        • Parameters

          • e: E

          Returns F

    • rd: RemoteData<E, T>

    Returns RemoteData<F, T>

    Returns RemoteData with a modified Failed value.

match

  • match<E, T, R>(matcher: Matcher<E, T, R>, rd: RemoteData<E, T>): R
  • "Pattern matches" over a RemoteData type and run the function of the matching variant. All functions much return the same type. This may change if pattern matching gets adopted into the language.

    example
    const data = RemoteData.Success([1,2,3,4])
    RemoteData.match({
    notAsked: () => 'Not Asked',
    loading: () => 'Loading',
    success: data => `Got ${data.length} items`,
    failed: err => `The error was ${err.message}`
    }, data);

    Type parameters

    • E

    • T

    • R

    Parameters

    Returns R

    A way of extracting the value. All functions much return the same type.

unwrap

  • unwrap<E, T, U>(defaultValue: U, fn: (x: T) => U, rd: RemoteData<E, T>): U
  • Take a default value, a function and a RemoteData. Return the default value if the RemoteData is something other than Success<T>. If the RemoteData is Success<T>, apply the function on T and return the U.

    example
    const success = RemoteData.Success([1,2,3,4])
    RemoteData.unwrap(
    () => [],
    data => [...data, 5],
    success);

    Type parameters

    • E

    • T

    • U

    Parameters

    • defaultValue: U
    • fn: (x: T) => U
        • (x: T): U
        • Parameters

          • x: T

          Returns U

    • rd: RemoteData<E, T>

    Returns U

withDefault

  • withDefault<E, T>(defaultValue: T, rd: RemoteData<E, T>): T
  • Extracts the value our of the Success variant or returns the default.

    example
    const data = RemoteData.Success([1,2,3,4])
    RemoteData.withDefault([], data)

    Type parameters

    • E

    • T

    Parameters

    Returns T

    Return the Success value, or the default.

Generated using TypeDoc