Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@rest-hooks/core

data-client7.3kApache-2.05.2.1TypeScript support: included

High performance reactive framework for async data.

data, cache, normalizr, normalization, flux, fetch, hook, networking, async, typescript, async, data fetching, data cache, api client, api, normalized cache, swr, GraphQL, query, ios, android, web, middleware, websocket, sse, declarative, scalable

readme

🛌🎣 Rest hooks

CircleCI Coverage Status npm downloads bundle size npm version PRs Welcome

Reducer/flux normalized, framework-agnostic data store. Includes managers/middleware, global referential equality guarantees, automatic expiry policies, data normalization. Consumes TypeScript Standard Endpoints

Framework Implementations

Sample React Hook suspense implementation

function useSuspense(endpoint, ...args)
  const state = useCacheState();
  const controller = useController();

  const key = args[0] !== null ? endpoint.key(...args) : '';
  const cacheResults = key && state.results[key];
  const meta = state.meta[key];

  // Compute denormalized value
  const { data, expiryStatus, expiresAt } = useMemo(() => {
    return controller.getResponse(endpoint, ...args, state);
  }, [
    cacheResults,
    state.indexes,
    state.entities,
    state.entityMeta,
    meta,
    key,
  ]);

  const error = controller.getError(endpoint, ...args, state);

  // If we are hard invalid we must fetch regardless of triggering or staleness
  const forceFetch = expiryStatus === ExpiryStatus.Invalid;

  const maybePromise = useMemo(() => {
    // null params mean don't do anything
    if ((Date.now() <= expiresAt && !forceFetch) || !key) return;

    return controller.fetch(endpoint, ...args);
    // we need to check against serialized params, since params can change frequently
  }, [expiresAt, controller, key, forceFetch, state.lastReset]);

  // fully "valid" data will not suspend even if it is not fresh
  if (expiryStatus !== ExpiryStatus.Valid && maybePromise) {
    throw maybePromise;
  }

  if (error) throw error;

  return data;
}