Skip to main content

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 always ResourceBad (i.e. a union of ResourceBadFail and ResourceBadRejected).

    When the original resource is Fail, it always returns a ResourceBadFail. 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 the successCodes list; in this way it will always be returned as ResourceBadRejected.

  • The Done type is a union of RES.ResourceData<S, P>:

    interface ResourceData<S, P> {
    readonly status: S
    readonly payload: P
    }

    where S can be one of the status codes in the successCodes 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 of Done could be something like this:

    type Done = RES.ResourceData<200, A> | RES.ResourceData<404, B>

    where A and B 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.