(FRP stands for Functional Reactive Programming.)
request-frp
is a package that provides pure wrappers around fetch
and XMLHttpRequest
so they can be used with types such as:
Observable
from RxJSTask
andEither
fromfp-ts
RemoteData
fromremote-data-ts
Primarily these wrappers are:
TaskEither.fromFetch
(usesfetch
)ObservableEither.fromFetch
(usesfetch
)ObservableRemoteData.fromFetch
(usesfetch
)ObservableRemoteData.fromAjax
(usesXMLHttpRequest
)
Unlike functions such as fetch
and response.json()
, these functions will never throw exceptions (that also includes things like promise rejections and Observable
errors). Instead, all errors are returned as objects e.g. Either.Left
or RemoteData.Failure
.
These functions are used heavily in production at Unsplash.
Additionally, this package provides a few convenience wrappers:
ObservableEither.fromFetchWithJsonResponse
: callingresponse.json()
is unsafe because it can reject, so this wrapper provides a pure/safe alternativeObservableEither.fromFetchWithJsonResponseDecoded
: decodes the response body using a givenio-ts
type
You can find examples in the ./src/examples
directory.
Unlike Task
s, Observable
s can emit more than once. For example, when used with RemoteData
this means they can emit Pending
(i.e. loading) and then they can emit again with Failure
or Success
. Example.
Observable
s also have built-in support for cancellation via subscriptions.
If you don't care about loading states (RemoteData.Pending
) and you're happy using things like AbortController
, TaskEither
should suffice.
ObservableRemoteData.fromAjax
includes information about the upload progress (inside RemoteData.Pending
), whereas ObservableRemoteData.fromFetch
does not.
- Test usage with
node-fetch
- Remove
io-ts
dependency (?) - Remove
content-type
dependency (?) - Use peer dependencies for fp-ts et al
- Publish to npm