Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DefaultRestAdapter<LP, RK, CT, RT>

Default implementation of a REST adapter.

cacheKey & idKey are used for basic examples, where the collection is an array of resources & the resources have a property (idKey) on them as their unique identifier. cacheKey can be any unique string but is typically the collection url.

Type parameters

  • LP

    List params.

  • RK: string

    Resource key.

  • CT: RT[]

    Collection type.

  • RT: { [ K in RK]: string }

    Resource type.

Hierarchy

  • DefaultRestAdapter

Implements

Index

Constructors

  • new DefaultRestAdapter<LP, RK, CT, RT>(cacheKey: string, idKey: RK): DefaultRestAdapter<LP, RK, CT, RT>
  • Construct a configured rest adapter.

    Type parameters

    • LP

    • RK: string

    • CT: RT[]

    • RT: { [ K in string]: string }

    Parameters

    • cacheKey: string

      SWR cache key prefix.

    • idKey: RK

      Property on the resource with unique identifier.

    Returns DefaultRestAdapter<LP, RK, CT, RT>

Methods

  • all(collection: CT): CT
  • Map the collection type into an array of resources.

    This list is used to push the resources into the cache after loading from the collection, so we need to map it down to a list so we can cache everything.

    Parameters

    • collection: CT

      Collection to read from.

    Returns CT

    Array of resources.

  • collectionKey(params?: LP): Key
  • Create the SWR key for the given collection params.

    Parameters

    • Optional params: LP

      List parameters to filter the API by.

    Returns Key

    SWR key for the collection.

  • id(resource: RT): RT[RK]
  • Get the id for a given resource.

    Parameters

    • resource: RT

      Resource to get the id for.

    Returns RT[RK]

    Id of the resource.

  • remove(id: string, collection: CT): CT
  • Remove a resource from a collection.

    This can use defaultFilter to remove a resource from an array of resources, using the adapter to match the id.

    Parameters

    • id: string

      Id of the resource to remove.

    • collection: CT

      Collection to remove resource from.

    Returns CT

    Collection with resource removed.

  • resourceKey(resource: string | RT): Key
  • Create the SWR key for the given resource or resource id.

    The function has to handle both cases because there are methods like delete which don't have resources to act on but are given ids. We need to be able to create the cache key for both cases.

    Parameters

    • resource: string | RT

      Resource to create the key for.

    Returns Key

    SWR key for the resource.

  • upsert(resource: RT, collection: CT): CT
  • Update or insert ("upsert") a resource in a collection.

    This can use defaultUpsert to pull the array of resources and run it through the default logic for upserting a resource, using the adapter to match the id.

    Parameters

    • resource: RT

      Resource to upsert into the collection.

    • collection: CT

      Collection to upsert into.

    Returns CT

    Upserted collection.

Generated using TypeDoc