🚀 Simple Remote Data
Simple Remote Data (SRD) is a fully static land compliant implementation of the Remote Data type in TypeScript - built with Higer Kinded Types (HKT's) inspired by fp-ts and Elm Remote Data.
The idea for using HKT's in TypeScript is based on Lightweight higher-kinded polymorphism.
Static Land Compliant
Install
With yarn
yarn add srd
or if you prefer npm
npm i srd
SRD supports CJS, UMD and ESM bundle outputs.
Examples
React Example
The following is a common use case in React. Fetching data async, showing it on screen and handling initial, loading, and error states.
Without SRD
, we would need something like this:
That's a lot of code for something we do very often, and highly error prone if we forget to unset/set some state values.
Here's the SRD way - using only 1 state variable instead of 4, preventing any impossible states:
That's it! Very easy to use, and 90% of the time that's everything you will need.
Typescript React Example
SRD works even better with Typescript! Declare your RD type once and have typescript powerfully infer it everywhere! Like magic!
Documentation
SRD
comes with many of the Static Land functions that we all know and love. Here is a breakdown of all the supported algebras and utilities:
Setoid
For comparing 2 SRD's to see if they are the same type.
*Note: This only compares the data types and not the inner value. So
Success(5) != Failure(5)
butSuccess(5) == Success(80)
.
equals :: (RD e a, RD e b) -> boolean
SRD.equalssuccess5, notAsked // false
Functor
Allowing the type to be mapped
over by the function provided.
map :: (a -> b, RD e a) -> RD e b
SRD.mapdouble, rd1 // success(8)SRD.mapdouble, rd2 // loading()
Bifunctor
Allowing the type to be bimapped
over by the functions provided. Common usecase is for when you need to map
and mapFailure
in one shot.
bimap :: (e -> b, a -> c, RD e a) -> RD b c
SRD.bimapformatErr, double, rd1 // success(8)SRD.bimapformatErr, double, rd2 // failure('Something went wrong: 404 not found')
Apply
Apply a function wrapped in a SRD to a value wrapped in a SRD.
ap :: (RD e (a -> b), RD e a) -> RD e b
SRD.apsuccessdouble, rd1) // success(8)SRD.apsuccessdouble, rd2) // failure('404 not found')
Applicative
Always returns a success
with whatever value is passed within.
of :: a -> RD e a
SRD.of4 // success(4)
Alt
Provide a default value to be returned when an SRD
is not a success
type.
alt :: (RD e a, RD e a) -> RD e a
SRD.altsuccess4, notAsked // success(4)SRD.altsuccess50, success4 // success(4)SRD.altloading, notAsked // loading()SRD.altloading, success4 // success(4)
Chain
Similar to map
but the callback must return another SRD
.
chain :: (a -> RD e b, RD e a) -> RD e b
SRD.chainsuccessx * 2, success4 // success(8)SRD.chainsuccessx * 2, notAsked // notAsked()SRD.chainfailure'failed', success4 // failure('failed')
Match
Provide a mapper object for each SRD type and whichever type the SRD is - that function will run.
data Matcher e a :: { notAsked :: () -> c , loading :: () -> c , failure :: e -> c , success :: a -> c } match :: (Matcher e a -> c, RD e a) -> c
SRD.match, success4 // My data is 4
Map Failure
Similar to map
but instead of running the callback on a success
, it calls it on a failure
.
mapFailure :: (e -> b, RD e a) -> RD b a
SRD.mapFailure`hello `, success4 // success(4)SRD.mapFailure`hello `, failure'bob' // failure('hello bob')
Map2
Similar to map
but takes 2 SRD's
instead of one, and if both are a success, the provided callback will be called.
map2 :: (a b -> c, RD e a, RD e b) -> RD e c
SRD.map2x + y, success4, success8 // success(12)SRD.map2x + y, failure'bob', success8 // failure('bob')SRD.map2x + y, success8, failure'bob' // failure('bob')
Map3
Similar to map2
but takes 3 SRD's
instead of two, and if all three are a success, the provided callback will be called.
map3 :: (a b c -> d, RD e a, RD e b, RD e c) -> RD e d
SRD.map3add3, success4, success8, success10 // success(22)SRD.map3add3, failure'bob', success8, notAsked // failure('bob')SRD.map3add3, success8, loading, failure'bob' // loading()
Unwrap
Similar to alt
, but unwraps the SRD from it's type and runs the callback on it. If the SRD is a success the inner value is passed to the callback and returned, any other value the default is returned.
unwrap :: (b, a -> b, RD e a) -> b
SRD.unwrap6, double, success8 // 16SRD.unwrap6, double, notAsked // 6SRD.unwrap6, double, loading // 6
Unpack
Similar to unwrap
, but takes a default thunk instead of a default value.
unpack :: (() -> b, a -> b, RD e a) -> b
SRD.unpack6, double, success8 // 16SRD.unpack6, double, notAsked // 6SRD.unpack6, double, loading // 6
WithDefault
Takes a default value and an SRD. If the SRD is a success then the inner value is returned, otherwise the default value is returned.
withDefault :: (a, RD e a) -> a
SRD.withDefault4, success8 // 8SRD.withDefault4, notAsked // 4SRD.withDefault4, loading // 4