1.2.11 • Published 3 years ago

voyager-react v1.2.11

Weekly downloads
51
License
MIT
Repository
github
Last release
3 years ago

voyager-react

The easiest way in the entire interstellar space, to consume a REST API. State management included.

NPM JavaScript Style Guide

Voyager aims to be to REST what Apollo Client is to GraphQL. In this spirit, Voyager also implements state management and maintains a local cache of the data.

For example, after sending a POST request Voyager will automatically update any GET requests that should include the newly created entity. Similarly for PUT and DELETE requests.

Installation

yarn add voyager-react

or

npm install --save voyager-react

Usage

Wrap your application with the <VoyagerProvider> tag

const Main = () => {
  return (
    <VoyagerProvider
      auth="http://localhost:3000/auth"
      url="http://localhost:3000/api/v1"
    >
      <App/>
   </VoyagerProvider>
  )
}

The auth URL must point to a very simple authentication server. Example server coming soon. This URL is used to obtain an API token using the login (see useLogin hook - coming soon). The token shell be used in each subsequent request to the API.

The URL attribute must point the the REST API. This is an (almost) standard REST (example server coming soon).

API

useGet<T=any>(resource: string, options?: Partial<RequestOptions>): [RequestState<T>, GetFunction<T>]

This hook is used to generate a GET request. Filtering, sorting and pagination information will be appended as URL query parameters, using default values if not explicitly provided.

Parms

NameDescription
resourceThe top level REST resource
optionsAn object of various options such as filtering, sorting and pagination but also related to the cache mechanism and request execution.

RequestOptions

FieldTypeDescription
queryPartial\
lazybooleanWhen set to true this will cause the request not to execute instantly, but rather be executed by calling GetFunction in code
policy'cache-first', 'cache-and-network', 'network-first', 'no-cache'cache-first check in cache and if found do not do anything, if not found do network; cache-and-network check cache and then do network, regardless; netowrk-fist do network without checking cache, and cache the result; no-cahce do network and don`t cache result
strictSortingbooleanWhen checking the cache Voyager will not allow entities obtained using a different sort_by option to mix.
spawnFromCachebooleanWhen requesting a single entity by ID (for example endpoint /restaurants/123) setting this option to true will allow Voyager to firstly search for the entity by ID in the local cache and if it exists skip crating an actual request
skipUntilbooleanThe request will not be created until this condition is true

QueryParameters

FieldTypeDescription
selectstring[]An array of the fields to be returned for each entity.
page_sizenumberNumber of entities per page.
page_nonumberThe page number. Starts at 0.
sort_bySortHow to sort the entities in the database before in the response
filterFilterObjA set of constraints that the entities must meet in order to be part of the response

Sort

string, 'asc' | 'desc'

The first string must be a fiend available on the entities to be sorted. The second array items indicates the sorting order.

FilterObj

FieldTypeDescription
stringFilterThe keys in a FilerObject are not standard. Each key must be a field on the entities being sorted

Filter

[

'eq' | 'neq' | 'regex' | 'in' | 'gt' | 'gte' | 'lt' | 'lte',

string | number | null | Array<string | number | null>

]

A filter consists of an operation, on the first position and a right hand side operator on the second position. So, for example the following FilterObj will get all entries that have the type equal to "fast-food".

{
  type: ["eq", "fast-food"]
}

Return Value

RequestState

FieldTypeDescription
loadingbooleanIndicates weather the request is ongoing. If the request was not created yet (because of lazy option), then this will be false
calledbooleanIndicates weather the request was created.
dataTThe actual data returned from the API
metaMetaMetadata about the data in the response
errstringA string describing any possible error. Might be a networking error (such as the lack of connection) or a custom API error returned in message field of the body, with appropriate error HTTP status code.

Meta

FieldTypeDescription
totalnumberThe total number of entries that exist in the database for the given filtering
hasNextbooleanIndicates weather there is a next page of data
hasPrevbooleanIndicates weather there is a previous page of data

GetFunction\ = (params?: GetFunctionParams) => Promise\

GetFunctionParams

FieldTypeDescription
silent?booleanSetting this to true will cause the loading property of RequestState to not indicate when the request is ongoing. This is useful for updating the UI without the user seeing a loading UI (if any implemented)
policy?'cache-first', 'cache-and-network', 'network-first', 'no-cache'Allows to override the policy specified by RequestOptions. This is useful to force Voyager to fetch again cached data when it might have changed and there is no way for the cache mechanism to know this. Detailed use-case example coming soon.

Example

const Restaurants = () => {

  const [{loading, data, err}] = useGet("restaurants", {
    sort_by: ["rating", "desc"],
    query: {
      rating: ["gte", 4],
      location: ["in", ["Bucharest", "Iasi"]]
    }
  })

  if(loading) return null

  if(err) return <p>{err}</p>

  return (
    <div>
      {data.map(r => <RestaurantItem data={r}/>)}
    </div>
  )
}

usePost<T=any>(path: string): [RequestState<T>, HookRunFunction<T>]

usePut<T=any>(path: string): [RequestState<T>, HookRunFunction<T>]

useDelete<T=any>(path: string): [RequestState<T>, HookRunFunction<T>]

These three hooks map to the remaining three verbs in the REST API standard. Their usage is self explanatory.

The RequestState object has been previously described.

HookRunFunction\ = (params?: {id?: string; body?: object) => Promise\

NameTypeDescription
idstringUsed by usePut and useDelete hooks.
bodyobjectUsed by usePost to provide the entity to be created, and used by usePut to provide the fields to be updated.

Bonus

usePagination(path: string, options?: Partial<RequestOptions>): [RequestState<T>, GetFunction<T>, NextPageFunction, PrevPageFunction, SeCurrentPageFunction, GetCurrentPage, SetPageSizeFunction]

This hook works exactly the same as the useGet hook, only that it returns more variables. Besides the ones already described, the extra variables returned are functions designed to interact with the pagination of the data.

Not only this hook fetches the requested page, but it will also pre-fetch the next page.

License

MIT © Alexandru Niculae (alexnix)

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.9

3 years ago

1.2.10

3 years ago

1.2.11

3 years ago

1.2.3

3 years ago

1.2.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.1.2

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago