Fetch factory & cache
import * as RESFF from 'mutoid/http/resourceFetchFactory'
fetchFactory
This function helps to build a ReaderObservableResource from an endpoint request, decoders and a list of success codes.
Where:
- EndpointRequest is an extension of
rxJsAjaxRequest - decoders is a lazy map, the same that you could use in fromAjax
- successCodes a list of status codes
The main differences from fromAjax are the following:
The
Failtype is alwaysResourceBad(i.e. a union ofResourceBadFailandResourceBadRejected).When the original resource is
Fail, it always returns aResourceBadFail. Otherwise, if you want to catch a bad request (e.g. in a form post), you can add a 4** status code to your decoders map and omit it from thesuccessCodeslist; in this way it will always be returned asResourceBadRejected.The
Donetype is a union ofRES.ResourceData<S, P>:interface ResourceData<S, P> {
readonly status: S
readonly payload: P
}where
Scan be one of the status codes in thesuccessCodeslist.The list of success codes is useful when, for example, an API returns 404 for an empty list or a not existing resource but you don't want to receive a
Failresource. In that case, the type ofDonecould be something like this:type Done = RES.ResourceData<200, A> | RES.ResourceData<404, B>where
AandBare inferred from the decoders map.
fetchCacheableFactory
This function has the same behavior of the fetchFactory function, but the EndpointRequest in case of GET can accept appCacheTtl.
In order to manage the cache you have to inject a CachePool service as a dependency.
If you want you can develop your own service. Otherwise, you can use one of these adapters.