Options
All
  • Public
  • Public/Protected
  • All
Menu

create-restful-swr

A tool for managing an SWR cache for a REST collection and its associated resources.

Installing & Using

# npm
npm i create-restful-swr
# or yarn
yarn add create-result-swr

Once installed in your app, create a new RESTful SWR hook. The simplest version looks like this:

import { createRESTfulSWR, DefaultRestAdapter } from 'create-restful-swr';

const {
useSWRResource: useModel,
useSWRCollection: useModels,
} = createRESTfulSWR({
adapter: new DefaultRestAdapter('/models', 'id'),
useRestClient: createDefaultUseRestClient('/models'),
});

This assumes a few things about your REST models:

  • /models returns an array of resources
  • /models/:id returns a single resource
  • Each resource has a unique id property that identifies the resource

The 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 the DefaultRestAdapter 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. See ResourceAdapter 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. See RestClient for more info.

Index

Type aliases

CreateRESTfulSWRParams<RT, CT, LP, CP, UP>: { adapter: ResourceAdapter<LP, CT, RT>; useRestClient: useRestClientHook<RT, CT, LP, CP, UP> }

Parameters needed to create a new set of RESTful SWR hooks.

Type parameters

  • RT

    Resource type.

  • CT

    Collection type.

  • LP

    List params.

  • CP

    Create params.

  • UP

    Update params.

Type declaration

RESTfulSWR<RT, CT, LP, CP, UP, E>: { useSWRCollection: UseRESTfulSWRCollection<RT, CT, LP, CP, UP, E>; useSWRResource: UseRESTfulSWRResource<RT, CP, UP, E> }

Hooks created by createRESTfulSWR.

Type parameters

  • RT

    Resource type.

  • CT

    Collection type.

  • LP

    List params.

  • CP

    Create params.

  • UP

    Update params.

  • E = any

    SWR error.

Type declaration

RESTfulSWRCollection<RT, CT, LP, CP, UP, E>: { api: { create: any; list: any; partial: any; remove: any; update: any; view: any }; response: SWRResponse<CT, E> }

Result of RESTful SWR hook for managing a collection.

Type parameters

  • RT

    Resource type.

  • CT

    Collection type.

  • LP

    List params.

  • CP

    Create params.

  • UP

    Update params.

  • E = any

    SWR error.

Type declaration

  • api: { create: any; list: any; partial: any; remove: any; update: any; view: any }

    API for interacting with the collection.

    See RestClient for info about these methods.

    • create:function
      • create(params: CP): Promise<RT>
      • Parameters

        • params: CP

        Returns Promise<RT>

    • list:function
      • list(params?: LP): Promise<CT>
      • Parameters

        • Optional params: LP

        Returns Promise<CT>

    • partial:function
      • partial(id: string, params: Partial<UP>): Promise<RT>
      • Parameters

        • id: string
        • params: Partial<UP>

        Returns Promise<RT>

    • remove:function
      • remove(id: string): Promise<void>
      • Parameters

        • id: string

        Returns Promise<void>

    • update:function
      • update(id: string, params: UP): Promise<RT>
      • Parameters

        • id: string
        • params: UP

        Returns Promise<RT>

    • view:function
      • view(id: string): Promise<RT>
      • Parameters

        • id: string

        Returns Promise<RT>

  • response: SWRResponse<CT, E>

    SWR response.

    The is the result of calling useSWR for the collection.

RESTfulSWRResource<RT, CP, UP, E>: { api: { create: any; partial: any; remove: any; update: any; view: any }; response: SWRResponse<RT, E> }

Result of RESTful SWR hook for managing a resource.

Type parameters

  • RT

    Resource type.

  • CP

    Create params.

  • UP

    Update params.

  • E = any

    SWR error.

Type declaration

  • api: { create: any; partial: any; remove: any; update: any; view: any }

    API for interacting with the resource.

    See RestClient for info about these methods.

    • create:function
      • create(params: CP): Promise<RT>
      • Parameters

        • params: CP

        Returns Promise<RT>

    • partial:function
      • partial(params: Partial<UP>): Promise<RT>
      • Parameters

        • params: Partial<UP>

        Returns Promise<RT>

    • remove:function
      • remove(): Promise<void>
    • update:function
      • update(params: UP): Promise<RT>
      • Parameters

        • params: UP

        Returns Promise<RT>

    • view:function
      • view(): Promise<RT>
  • response: SWRResponse<RT, E>

    SWR response.

    The is the result of calling useSWR for the resource.

UseRESTfulSWRCollection<RT, CT, LP, CP, UP, E>: (params?: LP) => RESTfulSWRCollection<RT, CT, LP, CP, UP, E>

Type parameters

  • RT

    Resource type.

  • CT

    Collection type.

  • LP

    List params.

  • CP

    Create params.

  • UP

    Update params.

  • E = any

    SWR error.

Type declaration

UseRESTfulSWRResource<RT, CP, UP, E>: (id?: string) => RESTfulSWRResource<RT, CP, UP, E>

Type parameters

  • RT

    Resource type.

  • CP

    Create params.

  • UP

    Update params.

  • E = any

    SWR error.

Type declaration

useRestClientHook<RT, CT, LP, CP, UP>: () => RestClient<RT, CT, LP, CP, UP>

Type parameters

  • RT

    Record type

  • CT

    Collection type

  • LP

    List params

  • CP

    Create params

  • UP

    Update params

Type declaration

    • 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.

      Returns RestClient<RT, CT, LP, CP, UP>

Functions

  • createDefaultUseRestClient<RT, CT, LP, CP, UP>(collectionUrl: string): () => RestClient<RT, CT, LP, CP, UP>
  • Create a default useRestClient hook that returns the default Rest client.

    Type parameters

    • RT

    • CT

    • LP: Record<string, string>

    • CP

    • UP

    Parameters

    • collectionUrl: string

      Url of the collection.

    Returns () => RestClient<RT, CT, LP, CP, UP>

    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.

    Type parameters

    • RT

      Resource type

    • CT

      Collection type

    • LP

      List params

    • CP

      Create params

    • UP

      Update params

    Parameters

    Returns RESTfulSWR<RT, CT, LP, CP, UP, any>

    RESTful SWR hooks

  • defaultFilter<RT>(adapt: ResourceAdapter<any, any, RT>, id: string, collection: RT[]): { removed: boolean; result: RT[] }
  • Default filter function for removing a resource from a collection.

    Type parameters

    • RT

      Resource type.

    Parameters

    • adapt: ResourceAdapter<any, any, RT>

      Adapter for the resource.

    • id: string

      Id of resource to remove from the collection.

    • collection: RT[]

      Collection to remove resource from.

    Returns { removed: boolean; result: RT[] }

    Whether resource was removed & result of the filter.

    • removed: boolean
    • result: RT[]
  • defaultUpsert<RT>(adapt: ResourceAdapter<any, any, RT>, resource: RT, collection: RT[]): { inserted: boolean; result: RT[] }
  • Default upsert function for adding a resource to a collection.

    Type parameters

    • RT

      Resource type.

    Parameters

    • adapt: ResourceAdapter<any, any, RT>

      Adapter for the resource.

    • resource: RT

      Resource to upsert.

    • collection: RT[]

      Collection to upsert into.

    Returns { inserted: boolean; result: RT[] }

    Whether it was inserted & result of the upsert.

    • inserted: boolean
    • result: RT[]

Generated using TypeDoc