Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ResourceAdapter<LP, CT, RT>

Adapter for a resource.

Responsible for adapting a RESTful collections & resources into a common type. For a resource, it's responsible for determining the id of a given resource with the id method. For a collection, it's responsible for upserting or removing a resource from the collection, or adapting a collection to a common "array of resources" representation. This enables CRS to work flexibly for a variety of RESTful representations.

Type parameters

  • LP

    List params

  • CT

    Collection type

  • RT

    Resource type

Hierarchy

  • ResourceAdapter

Implemented by

Index

Methods

  • all(collection: CT): RT[]
  • 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 RT[]

    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): string
  • Get the id for a given resource.

    Parameters

    • resource: RT

      Resource to get the id for.

    Returns string

    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