PaackEng / paack-remotedata / Remote.Data

A datatype representing fetched data in five different states. Based on Kris's RemoteData.

For the same motivations that are presented in Kris' version, with the small addition of always using transport and custom errors, and aliasing GraphqlError instead of Http.Error.

Types


type RemoteData transportError customError object
    = NotAsked
    | Loading
    | Failure (Remote.Errors.RemoteError transportError customError)
    | Success object

Frequently when you're fetching data from an API, you want to represent five different states:

Based on Kris's RemoteData.


type alias GraphqlHttpData error object =
RemoteData (Graphql.Http.RawError () Graphql.Http.HttpError) error object

While RemoteData can model any type of errors, the most common one Paack has encountered is when fetching data from a Graphql query, and getting back a GraphqlError. Because of that, GraphqlHttpData is provided as a useful alias.

Based on Kris's WebData.

Update

fromResponse : Remote.Response.Response transportError customError object -> RemoteData transportError customError object

Convert a Response, probably produced from a query result, to a RemoteData value.

Based on Kris's fromResult.

Identity crisis

isSuccess : RemoteData transportError customError object -> Basics.Bool

True when Success _.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isError : RemoteData transportError customError object -> Basics.Bool

True when Failure _.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isCustomError : RemoteData transportError customError object -> Basics.Bool

True when Failure (isCustomError _).

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isTransportError : RemoteData transportError customError object -> Basics.Bool

True when Failure (Transport _).

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isLoading : RemoteData transportError customError object -> Basics.Bool

True when Loading _.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isNotAsked : RemoteData transportError customError object -> Basics.Bool

True when NotAsked.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

Common transformations

toError : RemoteData transportError customError object -> Maybe (Remote.Errors.RemoteError transportError customError)

Transforms Failure error into Just error, and anything else into Nothing.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

map : (a -> b) -> RemoteData transportError customError a -> RemoteData transportError customError b

Apply a function to a RemoteData.Success. If the data is Success, it will be converted. If the data is anything else, the same value will propagate through.

mapCustomError : (customError -> a) -> RemoteData transportError customError object -> RemoteData transportError a object

Transform a Failure (Custom a) value. If the data is Failure (Custom a), it will be converted. If the data is anything else, the same value will propagate through.

mapTransportError : (transportError -> a) -> RemoteData transportError customError object -> RemoteData a customError object

Transform a Failure (Transport a) value. If the data is Failure (Transport a), it will be converted. If the data is anything else, the same value will propagate through.

mapErrors : (Remote.Errors.RemoteError transportError customError -> a) -> RemoteData transportError customError b -> RemoteData a a b

Transform a Failure value. If the data is Failure, it will be converted. If the data is anything else, the same value will propagate through.

withDefault : object -> RemoteData transportError customError object -> object

If the data is Success return its value, but if the data is anything else then return a given default alternative.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

merge : object -> RemoteData object object object -> object

For doing pipes instead of switch-case.

someData
    |> RemoteData.map (always "Success")
    |> RemoteData.mapCustomError (always "Expected failure")
    |> RemoteData.mapTransportError (always "Network error")
    |> RemoteData.merge "Loading or never asked"