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
rxJs
AjaxRequest
- 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
Fail
type is alwaysResourceBad
(i.e. a union ofResourceBadFail
andResourceBadRejected
).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 thesuccessCodes
list; in this way it will always be returned asResourceBadRejected
.The
Done
type is a union ofRES.ResourceData<S, P>
:interface ResourceData<S, P> {
readonly status: S
readonly payload: P
}where
S
can be one of the status codes in thesuccessCodes
list.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
Fail
resource. In that case, the type ofDone
could be something like this:type Done = RES.ResourceData<200, A> | RES.ResourceData<404, B>
where
A
andB
are 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.