Parameters needed to create a new set of RESTful SWR hooks.
Resource type.
Collection type.
List params.
Create params.
Update params.
Adapter for this RESTful SWR hook.
See ResourceAdapter.
Rest client hook for this RESTful SWR hook.
See useRestClientHook.
Hooks created by createRESTfulSWR.
Resource type.
Collection type.
List params.
Create params.
Update params.
SWR error.
Result of RESTful SWR hook for managing a collection.
Resource type.
Collection type.
List params.
Create params.
Update params.
SWR error.
API for interacting with the collection.
See RestClient for info about these methods.
SWR response.
The is the result of calling useSWR
for the collection.
Result of RESTful SWR hook for managing a resource.
Resource type.
Create params.
Update params.
SWR error.
API for interacting with the resource.
See RestClient for info about these methods.
SWR response.
The is the result of calling useSWR
for the resource.
Resource type.
Collection type.
List params.
Create params.
Update params.
SWR error.
Hook for interacting with a collection.
Resource type.
Create params.
Update params.
SWR error.
Hook for interacting with a resource.
Record type
Collection type
List params
Create params
Update params
Hook that returns a Rest client.
This is always called in "hook position" so you can use dependent hooks to create teh Rest client, if needed.
See RestClient for info on what needs to be returned.
Create a default useRestClient hook that returns the default Rest client.
Url of the collection.
useRestClient hook to return the client.
Create RESTful SWR hooks for managing a collection & resource.
This is the core entrypoint of the package. The function returns two hooks,
useSWRResource
& useSWRCollection
, that work with SWR to keep the
resources in sync between the individual resources & the root collection.
Resource type
Collection type
List params
Create params
Update params
RESTful SWR hooks
Default filter function for removing a resource from a collection.
Resource type.
Adapter for the resource.
Id of resource to remove from the collection.
Collection to remove resource from.
Whether resource was removed & result of the filter.
Default upsert function for adding a resource to a collection.
Resource type.
Adapter for the resource.
Resource to upsert.
Collection to upsert into.
Whether it was inserted & result of the upsert.
Generated using TypeDoc
A tool for managing an SWR cache for a REST collection and its associated resources.
Installing & Using
Once installed in your app, create a new RESTful SWR hook. The simplest version looks like this:
This assumes a few things about your REST models:
/models
returns an array of resources/models/:id
returns a single resourceid
property that identifies the resourceThe
adapter
&useRestClient
can be configured to work with your specific REST API structure.Customizing
There are two required parameters to pass to
createRESTfulSWR
, each of which customizes the behavior of the hook.Customizing
adapter
The
adapter
is responsible for providing a consistent interface for interacting with the collection & resources. You can use theDefaultRestAdapter
if your customization requirements are minimal. The first param is the key prefix to use when caching the collection & resource. The second param is the primary key on the resource.The
DefaultRestAdapter
assumes that the collection is a simple array of resources. If your collection type is more complicated, you may need to implement your own adapter. SeeResourceAdapter
for more info.Customizing
useRestClient
Similar to
adapter
,useRestClient
is a hook for providing the underlying REST client. It's called in hook position so you can access any necessary Context or use other hooks to bootstrap the rest client.createDefaultUseRestClient
will create a default rest client hook which will use a thin, standard implementation of a REST client. If your REST API is more complicated than the standard implementation, you can create your own hook to return the rest client. SeeRestClient
for more info.