All Projects → noahgrant → Resourcerer

noahgrant / Resourcerer

Licence: mit
Declarative data-fetching and caching framework for REST APIs with React

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Resourcerer

Sierra
Sierra SCSS library
Stars: ✭ 852 (+2481.82%)
Mutual labels:  frontend
Astral
⊙ CSS franken-work ⊙
Stars: ✭ 14 (-57.58%)
Mutual labels:  frontend
Crossui
CrossUI is a free Cross-Browser Javascript framework with cutting-edge functionality for rich web application
Stars: ✭ 945 (+2763.64%)
Mutual labels:  frontend
Vue 2 Boilerplate
Vue 2 boilerplate for developing medium to large single page applications.
Stars: ✭ 866 (+2524.24%)
Mutual labels:  frontend
Itelios Frontend Challenge
Desafio de admissão para desenvolvedores front-end da Itelios
Stars: ✭ 14 (-57.58%)
Mutual labels:  frontend
Gold Miner
🥇掘金翻译计划,可能是世界最大最好的英译中技术社区,最懂读者和译者的翻译平台:
Stars: ✭ 29,872 (+90421.21%)
Mutual labels:  frontend
Vue Enterprise Boilerplate
An ever-evolving, very opinionated architecture and dev environment for new Vue SPA projects using Vue CLI.
Stars: ✭ 7,354 (+22184.85%)
Mutual labels:  frontend
Mber
Fast and minimal Ember.js CLI alternative, without broccoli.
Stars: ✭ 30 (-9.09%)
Mutual labels:  frontend
React Fab Fan
Floating action button fan built with react and react-motion
Stars: ✭ 14 (-57.58%)
Mutual labels:  frontend
Awesome Web Components
🤖 Awesome web components and snippets for every Front-End Developer
Stars: ✭ 28 (-15.15%)
Mutual labels:  frontend
Livingstyleguide
Easily create front-end style guides with Markdown and Sass/SCSS.
Stars: ✭ 874 (+2548.48%)
Mutual labels:  frontend
Falko
📈 Falko (Front-End): Platform for agile projects management 📊
Stars: ✭ 14 (-57.58%)
Mutual labels:  frontend
Frontend Dev Bookmarks
Manually curated collection of resources for frontend web developers.
Stars: ✭ 32,924 (+99669.7%)
Mutual labels:  frontend
Angular Developer Roadmap
Angular Developer Roadmap
Stars: ✭ 851 (+2478.79%)
Mutual labels:  frontend
App Tutorial
Tutorial app which is built in the tutorial
Stars: ✭ 29 (-12.12%)
Mutual labels:  frontend
Frontend React
Open sourced front end application for codecorgi.
Stars: ✭ 9 (-72.73%)
Mutual labels:  frontend
Composer Assets Plugin
Composer plugin for copying of frontend assets into public directory.
Stars: ✭ 20 (-39.39%)
Mutual labels:  frontend
Translations
Переводы от devSchacht
Stars: ✭ 959 (+2806.06%)
Mutual labels:  frontend
Vanilla Todo
A case study on viable techniques for vanilla web development.
Stars: ✭ 951 (+2781.82%)
Mutual labels:  frontend
Mock Server
Easy to use, no frills http mock server
Stars: ✭ 27 (-18.18%)
Mutual labels:  frontend
Resourcerer Icon

resourcerer

resourcerer is a library for declaratively fetching and caching your application's data. Its powerful useResources React hook or withResources higher-order React component (HOC) allows you to easily construct a component's data flow, including:

  • serial requests
  • prioritized rendering for critical data (enabling less critical or slower requests to not block interactivity)
  • delayed requests
  • prefetching
  • ...and more

Additional features include:

  • first-class loading and error state support
  • smart client-side caching
  • updating a component when a resource updates
  • ...and more

It employs a View-less, jQuery-less, Promise-interfaced fork of Backbone called Schmackbone for Model/Collection semantics (as well as its Events module). Getting started is easy:

  1. Define a model in your application:
// js/models/todos-collection.js
import {Collection} from 'resourcerer';

export default class TodosCollection extends Collection {
  url() {
    return '/todos';
  }
}
  1. Create a config file in your application and add your constructor to the ModelMap with a key:
// js/core/resourcerer-config.js
import {ModelMap} from 'resourcerer';
import TodosCollection from 'js/models/todos-collection';

// choose any string as its key, which becomes its ResourceKey
ModelMap.add({TODOS: TodosCollection});
// in your top level js file
import 'js/core/resourcerer-config;
  1. Use your preferred abstraction (useResources hook or withResources HOC) to request your models in any component:

    1. useResources

      import React from 'react';
      import {useResources} from 'resourcerer';
      
      const getResources = (props, {TODOS}) => ({[TODOS]: {}});
      
      function MyComponent(props) {
        var {
          isLoading,
          hasErrored,
          hasLoaded,
          todosCollection
        } = useResources(getResources, props);
        
        // when MyComponent is mounted, the todosCollection is fetched and available
        // as `todosCollection`!
        return (
          <div className='MyComponent'>
            {isLoading ? <Loader /> : null}
      
            {hasErrored ? <ErrorMessage /> : null}
      
            {hasLoaded ? (
              <ul>
                {todosCollection.map((todoModel) => (
                  <li key={todoModel.id}>{todoModel.get('name')}</li>
                ))}
              </ul>
            ) : null}
          </div>
        );
      }
      
    2. withResources

      import React from 'react';
      import {withResources} from 'resourcerer';
      
      @withResources((props, {TODOS}) => ({[TODOS]: {}}))
      class MyComponent extends React.Component {
        render() {
          // when MyComponent is mounted, the todosCollection is fetched and available
          // as `this.props.todosCollection`!
          return (
            <div className='MyComponent'>
              {this.props.isLoading ? <Loader /> : null}
      
              {this.props.hasErrored ? <ErrorMessage /> : null}
      
              {this.props.hasLoaded ? (
                <ul>
                  {this.props.todosCollection.map((todoModel) => (
                    <li key={todoModel.id}>{todoModel.get('name')}</li>
                  ))}
                </ul>
              ) : null}
            </div>
          );
        }
      }
      

There's a lot there, so let's unpack that a bit. There's also a lot more that we can do there, so let's also get into that. But first, some logistics:

Contents

  1. Installation
  2. Nomenclature
  3. Tutorial
    1. Intro
    2. Other Props Returned from the Hook/Passed from the HOC (Loading States)
    3. Requesting Prop-driven Data
    4. Changing Props
    5. Serial Requests
    6. Other Common Resource Config Options
      1. data
      2. options
      3. attributes
      4. noncritical
      5. forceFetch
      6. Custom Resource Names
      7. prefetches
      8. status
    7. Differences between useResources and withResources
    8. Caching Resources with ModelCache
    9. Declarative Cache Keys
    10. Prefetch on Hover
    11. Tracking Request Times
  4. Configuring resourcerer
  5. FAQs

Installation

$ npm i resourcerer

resourcerer depends on React >= 16 and Schmackbone (which itself lightly depends on Underscore and qs).

Note that Resourcerer uses ES2015 in its source and does no transpiling—including import/export (Local babel configuration is for testing, only). This means that if you're not babelifying your node_modules folder, you'll need to make an exception for this package, ie:

// webpack.config.js or similar
module: {
  rules: [{
    test: /\.jsx?$/,
    exclude: /node_modules\/(?!(resourcerer))/,
    use: {loader: 'babel-loader?cacheDirectory=true'}
  }]
}

Also note that it also uses legacy, Stage-1 decorators, so you'll need to use the babel decorators plugin with the {legacy: true} option:

// .babelrc
  "plugins": [
    ["@babel/proposal-decorators", {"legacy": true}],
    // ...
  ]

Nomenclature

  1. Props. Going forward in this tutorial, we'll try to describe behavior of both the useResources hook and the withResources HOC at once; we'll also rotate between the two in examples. Note that if we talking about a passed prop of, for example isLoading, that that corresponds to an isLoading property returned from the hook and a this.props.isLoading prop passed down from the HOC.

  2. ResourceKeys. These are the keys added to the ModelMap in the introduction that link to your model constructors. They are passed to the executor functions and are used to tell the hook or HOC which resources to request.

  3. Executor Function. The executor function is a function that both the hook and HOC accept that declaratively describes which resources to request and with what config options. It accepts props and ResourceKeys as arguments and may look like, as we'll explore in an example later:

    const getResources = (props, {USER}) => ({[USER]: {options: {userId: props.id}}});
    

    or

    const getResources = (props, {USER_TODOS}) => {
      const now = Date.now();
      
      return {
        [USER_TODOS]: {
          data: {
            limit: 20,
            end_time: now,
            start_time: now - props.timeRange,
            sort_field: props.sortField
          }
        }
      };
    };
    

    It returns an object whose keys represent the resources to fetch and whose values are resource configuration objects that we'll discuss later (and is highlighted below).

  4. Resource Configuration Object (resource config). In the object returned by our executor function, each entry has a key equal to one of the ResourceKeys and whose value we will refer to in this document as a Resource Configuration Object. It holds the declarative instructions that useResources and withResources will use to request the resource.

Tutorial

Okay, back to the initial example. Let's take a look at our useResources usage in the component:

// Note: in these docs, you will see a combination of `ResourceKeys` in the executor function as well as
// its more common destructured version, ie `@withResources((props, {TODOS}) => ({[TODOS]: {}}))`
const getResources = (props, ResourceKeys) => ({[ResourceKeys.TODOS]: {}});

export default function MyComponent(props) {
  var resources = useResources(getResources, props);
  
  // ...
}

You see that useResources takes an executor function that returns an object. The executor function takes two arguments: the current props, and an object of ResourceKeys. Where does ResourceKeys come from? From the ModelMap in the config file we added to earlier!

// js/core/resourcerer-config.js
import {ModelMap} from 'resourcerer';
import TodosCollection from 'js/models/todos-collection';

// after adding this key, resourcerer will add an identical key to the `ResourceKeys` object with a
// camelCased version as its value. `ResourceKeys.TODOS` can then be used in our executor functions to reference
// the Todos resource. The camelCased 'todos' string value will be the default prefix added to all todos-related
// props passed from the HOC to the wrapped component. That's why we have `props.todosCollection`!
ModelMap.add({TODOS: TodosCollection});

(We can also pass custom prefixes for our prop names in a component, but we'll get to that later.)

Back to the executor function. In the example above, you see it returns an object of {[ResourceKeys.TODOS]: {}}. In general, the object it should return is of type {string<ResourceKey>: object<Options>}, where Options is a generic map of config options, and can contain as many keys as resources you would like the component to request. In our initial example, the options object was empty. Further down, we'll go over the plethora of options and how to use them. For now let's take a look at some of the resource-related props this simple configuration provides our component.

Other Props Returned from the Hook/Passed from the HOC (Loading States)

Of course, in our initial example, the todos collection won’t get passed down immediately since, after all, the resource has to be fetched from the API. Some of the most significant and most common React UI states we utilize are whether a component’s critical resources have loaded entirely, whether any are still loading, or whether any have errored out. This is how we can appropriately cover our bases—i.e., we can ensure the component shows a loader while the resource is still in route, or if something goes wrong, we can ensure the component will still fail gracefully and not break the layout. To address these concerns, the useResources hook/withResources HOC gives you several loading state helper props. From our last example:

  • todosLoadingState (can be equal to any of the LoadingStates constants, and there will be one for each resource)
  • hasLoaded {boolean} - all critical resources have successfully completed and are ready to be used by the component
  • isLoading {boolean} - any of the critical resources are still in the process of being fetched
  • hasErrored {boolean} - any of the critical resource requests did not complete successfully

isLoading , hasLoaded , and hasErrored are not based on individual loading states, but are rather a collective loading state for the aforementioned-critical component resources. In the previous example, the todos resource is the only critical resource, so isLoading / hasLoaded / hasErrored are solely based on todosLoadingState. But we can also add a non-critical users resource, responsible, say, for only display users' names alongside their TODOs—a small piece of the overall component and not worth delaying render over. Here’s how we do that:

const getResources = (props, {TODOS, USER}) => ({
  [TODOS]: {},
  [USERS]: {noncritical: true}
});

function MyClassWithTodosAndAUsers(props) {
  var resources = useResources(getResources, props);
}

MyClassWithDecisionsAndAnalysts will now receive the following loading-related props, assuming we've assigned the USERS key a string value of 'users' in our config file:

  • todosLoadingState
  • usersLoadingState
  • isLoading
  • hasLoaded
  • hasErrored

In this case, isLoading , et al, are only representative of todosLoadingState and completely irrespective of usersLoadingState . This allow us an incredible amount of flexibility for rendering a component as quickly as possible.

Here’s how might use that to our advantage in MyClassWithTodosAndAUsers :

import * as resourcerer from 'resourcerer';

function MyClassWithTodosAndAUsers(props) {
  var {
    isLoading,
    hasErrored,
    hasLoaded,
    todosCollection,
    usersCollection,
    usersLoadingState
  } = useResources(getResources, props);
  
  var getUserName = (userId) => {
    // usersCollection guaranteed to have returned here
    var user = usersCollection.find(({id}) => id === userId);
            
    return (
      <span className='user-name'>
        {user && user.id || 'N/A'}
      </span>
    );
  };
          
  return (
    <div className='MyClassWithTodosAndUsers'>
      {isLoading ? <Loader /> : null}
          
      {hasLoaded ? (
        // at this point we are guaranteed all critical resources have returned.
        // before that, todosCollection is a Collection instance, just empty
        <ul>
          {todosCollection.map((todoModel) => (
            <li key={todoModel.id}>
              // pure function that accepts loading states as arguments
              {resourcerer.hasLoaded(usersLoadingState) ?
                getUserName(todoModel.get('userId')) :
                // if you're anti-loader, you could opt to render nothing and have the
                // user name simply appear in place after loading
                <Loader size={Loader.Sizes.SMALL} />}
              {todoModel.get('name')}
            </li>
          )}
        </ul>
      ) : null}
          
      {hasErrored ? <ErrorMessage /> : null}
    </div>
  );

Here's a real-life example from the Sift Console, where we load a customer's workflows without waiting for the workflow stats resource, which takes much longer. Instead, we gracefully show small loaders where the stats will eventually display, all-the-while keeping our console interactive:

Noncritical Resource Loading

And here's what it looks like when the stats endpoint returns:

Noncritical Resource Returned

There’s one other loading prop offered from the hook/HOC: hasInitiallyLoaded. This can be useful for showing a different UI for components that have already fetched the resource. An example might be a component with filters: as the initial resource is fetched, we may want to show a generic loader, but upon changing a filter (and re-fetching the resource), we may want to show a loader with an overlay over the previous version of the component. See the Advanced Topics docs for more.

Requesting Prop-driven Data

Let's say we wanted to request not the entire users collection, but just a specific user. Here's our config:

// js/core/resourcerer-config.js
import {ModelMap} from 'resourcerer';
import TodosCollection from 'js/models/todos-collection';
import UserModel from 'js/models/user-model';

ModelMap.add({
  TODOS: TodosCollection,
  USER: UserModel
});

And here's what our model might look like:

// js/models/user-model.js
import {Model} from 'resourcerer';

export default class UserModel extends Model {
  constructor(attributes, options={}) {
    this.userId = options.userId;
  }
  
  url() {
    return `/users/${this.userId}`;
  }
  
  static cacheFields = ['userId']
}

The cacheFields static property is important here, as we'll see in a second; it is a list of model properties that resourcerer will use to generate a cache key for the model. It will look for the userId property in the following places, in order:

  1. the options object it is initialized with
  2. the attributes object it is initialized with
  3. the data it gets passed in a fetch

All three of these come from what's returned from our executor function; it might look like this:

const getResources = (props, {USER}) => ({[USER]: {options: {userId: props.id}}}) 

// hook
function MyComponent(props) {
  var resources = useResources(getResources, props);
  
  // ...
}

// HOC
@withResources(getResources)
class MyComponentWithAUser extends React.Component {}

Assuming we have a props.id equal to 'noahgrant', this setup will put MyComponentWithAUser in a loading state until /users/noahgrant has returned.

...and here's the best part:

Let's say that props.id changes to a different user. MyComponentWithAUser will get put back into a loading state while the new endpoint is fetched, without us having to do anything! This works because our model has dictated that its models should be cached by a userId field, which is passed to it in the options property.

Changing Props

In general, there are two ways to change props.id as in the previous example:

  1. Change the url, which is the top-most state-carrying entity of any application. The url can be changed either by path parameter or query paramter, i.e. example.com/users/noahgrant -> example.com/users/fredsadaghiani, or example.com/users?id=noahgrant -> example.com/users?id=fredsadaghiani. In this case, each prop change is indexable, which is sometimes desirable, sometimes not.

  2. Change internal application state. For these cases, useResources/withResources make available another handy prop: setResourceState. setResourceState is a function that has the same method signature as the setState we all know and love. It sets internal hook/HOC state, which is then returned/passed down, respectively, overriding any initial prop, ie setResourceState({id: 'fredsadaghiani'}). This is not indexable.

    Note that setResourceState has some subtle discrepancies between the hook and the HOC; see Differences between useResources and withResources for more.

Serial Requests

In most situations, all resource requests should be parallelized; but that’s not always possible. Every so often, there may be a situation where one request depends on the result of another. For these cases, we have the dependsOn resource option and the provides resource option. These are probably best explained by example, so here is a simplified instance from the Sift Console, where we load a queue item that has info about a user, but we can't get further user information until we know what user id belongs to this queue item.

@withResources((props, {QUEUE_ITEM, USER}) => ({
  [USER]: {
    options: {userId: props.userId},
    dependsOn: ['userId']
  },
  [QUEUE_ITEM]: {
    attributes: {id: props.itemId}
    provides: {userId: getUserIdFromItem}
  }
}))
export default class QueueItemPage extends React.Component {}
    
function getUserIdFromItem(queueItemModel) {
  return queueItemModel.get('userId');
}

In this simplified example, only props.itemId is initially present at the url items/<itemId>, and since the UserModel depends on props.userId being present, that model won’t initially get fetched. Only the QueueItemModel gets fetched at first; it has the provides option, which is a map of <string: function>, where the string is the prop that it provides to the HOC wrapper, and the function is a private static ‘transform’ function—it takes its model as an argument and returns the value for the prop it provides.

So, in this case, getUserIdFromItem is the transform function, which takes the queueItemModel as an argument and returns the userId that will be assigned to props.userId (or, more accurately, will be set as state for the HOC wrapper’s state wrapper as described in the previous section). When the QueueItemModel resource returns, the transform function is invoked; at that point, props.userId exists, and the UserModel will be fetched. And we have serially requested our resources!

One thing to note here is that while the QUEUE_ITEM resource is being fetched, the user resource is in a PENDING state, which is a special state that does not contribute to overall component isLoading/hasErrored states (though it will keep the component from being hasLoaded). At this point, the QueueItemPage in the example above is in a LOADING state (isLoading === true) because QUEUE_ITEM is loading. When it returns with the user id, the USER resource is put into a LOADING state, and the component then remains isLoading === true until it returns, after which the component has successfully loaded. If the QUEUE_ITEM resource happened to error, for some reason, the USER resource would never get out of its PENDING state, and the component would then take on the ERROR state (hasErrored === true) of QUEUE_ITEM. For more on PENDING, see Thoughts on the PENDING State in the Advanced Topics document.

Finally, if a model is to provide more than a single prop, use an underscore instead of the prop name in the provides object. Instead of the transform function returning the prop value, it should then return an object of prop keys and values, which will get spread to the component:

const getResources = (props, {QUEUE_ITEM, USER}) => ({
  [USER]: {
    options: {state: props.activeState, userId: props.userId},
    // userModel depends on multiple props from queueItemModel
    dependsOn: ['activeState', 'userId']
  },
  [QUEUE_ITEM]: {
    attributes: {id: props.itemId}
    // use an underscore here to tell resourcerer to spread the resulting object
    provides: {_: getUserDataFromItem}
  }
});
  
export default function QueueItemPage(props) {
  // activeState and userId are internal state within `useResources` and returned
  var {
    activeState,
    userId,
    userModel,
    queueItemModel
  } = useResources(getResources, props);
}
    
function getUserDataFromItem(queueItemModel) {
  // transform function now returns an object of prop names/values instead of a simple prop value
  return {userId: queueItemModel.get('userId'), activeState: queueItemModel.get('state')};
}

Other Common Resource Config Options

data

The data option is passed directly to Schmackbone and sent either as stringified query params (GET requests) or as a body (POST/PUT). Its properties are also referenced when generating a cache key if they are listed in a model's static cacheFields property (See the cache key section for more). Let's imagine that we have a lot of users and a lot of todos per user. So many that we only want to fetch the todos over a time range selected from a dropdown, sorted by a field also selected by a dropdown. These are query parameters we'd want to pass in our data property:

  @withResources((props, ResourceKeys) => {
    const now = Date.now();
      
    return {
      [ResourceKeys.USER_TODOS]: {
        data: {
          limit: 20,
          end_time: now,
          start_time: now - props.timeRange,
          sort_field: props.sortField
        }
      }
    };
  })
  class UserTodos extends React.Component {}

Now, as the prop fields change, the data sent with the request changes as well (provided we set our cacheFields property accordingly):

https://example.com/users/noahgrant/todos?limit=20&end_time=1494611831024&start_time=1492019831024&sort_field=importance

options

As referenced previously, an options hash on a resource config will be passed directly as the second parameter to a model's constructor method. It will also be used in cache key generation if it has any fields specified in the model's static cacheFields property (See the cache key section for more). Continuing with our User Todos example, let's add an options property:

const getResources = (props, ResourceKeys) => {
  const now = Date.now();
      
  return {
    [ResourceKeys.USER_TODOS]: {
      data: {
        limit: 20,
        end_time: now,
        start_time: now - props.timeRange,
        sort_field: props.sortField
      },
      options: {userId: props.userId}
    }
  };
};

Here, the UserTodos collection will be instantiated with an options hash including the userId property, which it uses to construct its url. We'll also want to add the 'userId' string to the collection's static cacheFields array, because each cached collection should be specific to the user.

attributes

Pass in an attributes hash to initialize a Model instance with a body before initially fetching. This is passed directly to the model's constructor method along with the options property, and is typically less useful than providing the properties directly to the data property. Like data and options, the attributes object will also be used in cache key generation if it has any fields specified in the model's static cacheFields property (See the cache key section for more).

noncritical

As alluded to in the Other Props section, not all resources used by the component are needed for rendering. By adding a noncritical: true option, we:

  • De-prioritize fetching the resource until after all critical resources have been fetched
  • Remove the resource from consideration within the component-wide loading states (hasLoaded, isLoading, hasErrored), giving us the ability to render without waiting on those resources
  • Can set our own UI logic around displaying noncritical data based on their individual loading states, ie usersLoadingState, which can be passed to the pure helper methods, hasLoaded, hasErrored, and isLoading from resourcerer.

forceFetch

Sometimes you want the latest of a resource, bypassing whatever model has already been cached in your application. To accomplish this, simply pass a forceFetch: true in a resource's config. The force-fetched response will replace any prior model in the cache, but may itself get replaced by a subsequent forceFetch: true request for the resource.

  @withResources((props, ResourceKeys) => ({[ResourceKeys.LATEST_STATS]: {forceFetch: true}}))
  class MyComponentWithLatestStats extends React.Component {}

Custom Resource Names

Passing a modelKey: <ResourceKeys> option allows you to pass a custom name as the withResources key, which will become the base name for component-related props passed down to the component. For example, this configuration:

const getResources = (props, ResourceKeys) => ({myRadTodos: {modelKey: ResourceKeys.TODOS});

export default function MyComponentWithTodos {
  var {
    myRadTodosCollection,
    myRadTodosLoadingState,
    myRadTodosStatus,
    ...rest
  } = useResources(getResources, props);
}

would still fetch the todos resource, but the properties returned/props passed to the MyComponentWithTodos instance will be myRadTodosCollection, myRadTodosLoadingState, and myRadTodosStatus, etc, as shown. This also allows us to fetch the same resource type multiple times for a single component.

prefetches

This option is an array of props objects that represent what is different from the props in the original resource. For each array entry, a new resource configuration object will be calculated by merging the current props with the new props, and the resulting request is made. In contrast to the original resource, however, no props representing the prefetched requests are returned or passed down to any children (ie, there are no loading state props, no model props, etc). They are simply returned and kept in memory so that whenever they are requested, they are already available.

A great example of this is for pagination. Let's take our previous example and add a from property to go with our limit that is based on the value of a page prop (tracked either by url parameter or by setResourceState). We want to request the first page but also prefetch the following page because we think the user is likely to click on it:

const getResources = (props, ResourceKeys) => {
  const now = Date.now();
  const REQUESTS_PER_PAGE = 20;
      
  return {
    [ResourceKeys.USER_TODOS]: {
      data: {
        from: props.page * REQUESTS_PER_PAGE,
        limit: REQUESTS_PER_PAGE,
        end_time: now,
        start_time: now - props.timeRange,
        sort_field: props.sortField
      },
      options: {userId: props.userId},
      // this entry is how we expect the props to change. in this case, we want props.page to be
      // incremented. the resulting prefetched request will have a `from` value of 20, whereas the
      // original request will have a `from` value of 0. The `userTodosCollection` returned (hook) or
      // passed down as props (HOC) will be the latter.
      prefetches: [{page: props.page + 1}]
    }
  };
};

When the user clicks on a 'next' arrow that updates page state, the collection will already be in the cache, and it will get passed as the new userTodosCollection. Accordingly, the third page will then get prefetched (props.page equal to 2 and from equal to 40). Two important things to note here:

  1. Don't forget to add from to the cacheFields list!
  2. The prefetched model does not get components registered to it; therefore, it is immediately scheduled for removal after the specified cacheGracePeriod. If the user clicks the next arrow, it then becomes the 'active' model and the UserTodos component will get registered to it, clearing the removal timer (see the caching section).

If you're looking to optimistically prefetch resources when a user hovers, say, over a link, see the Prefetch on Hover section.

status

(withResources only)

Passing a status: true config option will pass props down to the component reflecting the resource’s status code. For example, if you pass the option to a TODOS resource that 404s, the wrapped component will have a prop called todosStatus that will be equal to 404.

@withResources((props, ResourceKeys) => ({
  [ResourceKeys.TODOS]: {measure: true, status: true}
}))
class MyComponentWithTodos extends React.Component {}

Note that in the useResources hook, which does not pollute any props object, statuses are returned by default; you can choose which ones you want to use in your component and ignore the rest.

Differences between useResources and withResources

The hook and HOC largely operate interchangeably, but do note a couple critical differences:

  1. The withResources HOC conveniently contains an ErrorBoundary with every instance, but such functionality does not yet exist in hooks. This is a definite advantage for the HOC right now, since, if we're already setting hasErrored clauses in our components to prepare for request errors, we can naturally gracefully degrade when an unexpected exception occurs. You'll need to manage this yourself with hooks until the equivalent functionality is released.

  2. The hooks's setResourceState function utilizes React's useState hook, which does not auto-merge updates like setState does. Be sure to manually merge all resource state!

    setResourceState((existingState) => ({
      ...existingState,
      timeRange: newTimeRange
    }));
    
  3. The hook does not accept a {status: true} option like the HOC does because it returns all statuses by default.

  4. With the executor function now inlined in your component, be extra careful to avoid this anti-pattern:

    function MyComponent({start_time, ...props}) {
      var {todosCollection} = useResources((_props, {TODOS}) => ({[TODOS]: {data: {start_time}}}), props);
      
      // ...
    

    The subtle problem with the above is that the start_time executor function parameter is relying on a value in the function component closure instead of the _props parameter object; props passed to the executor function can be current or previous but are not the same as what is in the closure, which will always be current. This will lead to confusing bugs, so instead either read directly from the props parameter passed to the executor function:

    function MyComponent(props) {
      var {todosCollection} = useResources(({start_time}, {TODOS}) => ({[TODOS]: {data: {start_time}}}), props);
      
      // ...
    

    or define your executor function outside of the component scope, as we've done throughout this tutorial (now you know why!):

    const getResources = ({start_time}, {TODOS}) => ({[TODOS]: {data: {start_time}}});
    
    function MyComponent(props) {
      var {todosCollection} = useResources(getResources, props);
      
      // ...
    

Caching Resources with ModelCache

resourcerer handles resource storage and caching, so that when multiple components request the same resource with the same parameters or the same body, they receive the same model in response. If multiple components request a resource still in-flight, only a single request is made, and each component awaits the return of the same resource. Fetched resources are stored in the ModelCache. Under most circumstances, you won’t need to interact with directly; but it’s still worth knowing a little bit about what it does.

The ModelCache is a simple module that contains a couple of Maps—one that is the actual cache {cacheKey<string>: model<Model|Collection>}, and one that is a component manifest, keeping track of all component instances that are using a given resource (unique by cache key). When a component unmounts, resourcerer will unregister the component instance from the component manifest; if a resource no longer has any component instances attached, it gets scheduled for cache removal. The timeout period for cache removal is two minutes by default (but can be changed, see Configuring resourcerer), to allow navigating back and forth between pages without requiring a refetch of all resources. After the timeout, if no other new component instances have requested the resource, it’s removed from the ModelCache. Any further requests for that resource then go back through the network.

Again, it’s unlikely that you’ll use ModelCache directly while using resourcerer, but it’s helpful to know a bit about what’s going on behind-the-scenes.

Declarative Cache Keys

As alluded to previously, resourcerer relies on the model classes themselves to tell it how it should be cached. This is accomplished via a static cacheFields array, where each entry can be either:

  1. A string, where each string is the name of a property that the model receives whose value should take part in the cache key. The model can receive this property either from the options hash, the attributes hash, or the data hash, in that order.

  2. A function, whose return value is an object of keys and values that should both contribute to the cache key.

Let's take a look at the USER_TODOS resource from above, where we want to request some top number of todos for a user sorted by some value over some time range. The resource declaration might look like this:

const getResources = (props, ResourceKeys) => {
  const now = Date.now();
      
  return {
    [ResourceKeys.USER_TODOS]: {
      data: {
        limit: props.limit,
        end_time: now,
        start_time: now - props.timeRange,
        sort_field: props.sortField
      },
      options: {userId: props.userId}
    }
  };
};

And our corresponding model definition might look like this:

export class UserTodosCollection extends Collection {
  constructor(models, options={}) {
    this.userId = options.userId;
  }
  
  url() {
    return `/users/${this.userId}/todos`;
  }
  // ...
  
  static cacheFields = [
    'limit',
    'userId',
    'sort_field',
     ({end_millis, start_millis}) => ({range: end_millis - start_millis})
  ]
};

We can see that limit and sort_field as specified in cacheFields are taken straight from the data object that Schmackbone transforms into url query parameters. userId is part of the /users/{userId}/todos path, so it can't be part of the data object, which is why it's stored as an instance property. But resourcerer will see its value within the options hash that is passed and use it for the cache key.

The time range is a little tougher to cache, though. We're less interested the spcecific end_time/start_time values to the millisecond— it does us little good to cache an endpoint tied to Date.now() when it will never be the same for the next request. We're much more interested in the difference between end_time and start_time. This is a great use-case for a function entry in cacheFields, which takes the data object passed an argument. In the case above, the returned object will contribute a key called range and a value equal to the time range to the cache key.

The generated cache key would be something like userTodos_limit=50_$range=86400000_sort_field=importance_userId=noah. Again, note that:

  • the userId value is taken from the options hash
  • the limit and sort_field values are taken from the data hash
  • the range value is taken from a function that takes start_millis/end_millis from the data hash into account.

Prefetch on Hover

You can use resourcerer's executor function to optimistically prefetch resources when a user hovers over an element. For example, if a user hovers over a link to their TODOS page, you may want to get a head start on fetching their TODOS resource so that perceived loading time goes down or gets eliminated entirely. We can do this with the top-level prefetch function:

import {prefetch} from 'resourcerer';

// here's our executor function just as we pass to useResources or withResources
const getTodos = (props, ResourceKeys) => {
  const now = Date.now();
      
  return {
    [ResourceKeys.USER_TODOS]: {
      data: {
        limit: props.limit,
        end_time: now,
        start_time: now - props.timeRange,
        sort_field: props.sortField
      },
      options: {userId: props.userId}
    }
  };
};

// in your component, call the prefetch method with the executor and an object that matches
// what you expect the props to look like when the resources are requested without prefetch.
// attach the result to an `onMouseEnter` prop
<a href='/todos' onMouseEnter={prefetch(getTodos, expectedProps)}>TODOS</a>

Note, as mentioned in the comment above, that expectedProps should take the form of props expected when the resource is actually needed. For example, maybe we're viewing a list of users, and so there is no props.userId in the component that uses prefetch. But for the user in the list with id 'noahgrant', we would pass it an expectedProps that includes {userId: 'noahgrant'} because we know that when we click on the link and navigate to that url, props.userId should be equal to 'noahgrant'.

Tracking Request Times

If you have a metrics aggregator and want to track API request times, you can do this by setting a measure static property on your model or collection. measure can either be a boolean or a function that returns a boolean. The function takes the resource config object as a parameter:

import {Model} from 'resourcerer';

class MyMeasuredModel extends Model {
  // either a boolean, which will track every request of this model instance
  static measure = true

  // or a function that returns a boolean, which will track instance requests based on a condition
  static measure = ({attributes={}}) => attributes.id === 'noahgrant'
}

When the static measure property is/returns true, resourcerer will record the time it takes for that resource to return and pass the data to the track configuration method that you can set up, sending it to your own app data aggregator. This allows you to see the effects of your endpoints from a user’s perspective.

Configuring resourcerer

The same config file used to add to ResourceKeys and ModelMap also allows you to set custom configuration properties for your own application:

import {ResourcesConfig} from 'resourcerer';

ResourcesConfig.set(configObj);

ResourcesConfig.set accepts an object with any of the following properties:

  • cacheGracePeriod (number in ms): the length of time a resource will be kept in the cache after being scheduled for removal (see the caching section for more). Default: 120000 (2 minutes).

  • errorBoundaryChild (JSX/React.Element): the element or component that should be rendered in the ErrorBoundary included in every withResources wrapping. By default, a caught error renders this child:

<div className='caught-error'>
  <p>An error occurred.</p>
</div>
  • log (function): method invoked when an error is caught by the ErrorBoundary. Takes the caught error as an argument. Use this hook to send caught errors to your error monitoring system. Default: noop.

  • prefilter (function): proxy for Schmackbone's ajaxPrefilter method, which is a great place to add custom request headers (like auth headers) or do custom error response handling. See Schmackbone's documentation for more. Default: the identity function.

  • queryParamsPropName (string): the name of the prop representing url query parameters that withResources will look for and flatten for its children. If your application already flattens query parameters, you can ignore this property. Otherwise, when a url search string of, for example, ?end_time=1558100000000&start_time=1555508000000 is turned into an object prop of {end_time: 1558100000000, start_time: 1555508000000}, withResources-wrapped components will see props.end_time and props.start_time, and useResources will return end_time and start_time for ease of use in your executor function. Default: 'urlParams'.

  • track (function): method invoked when measure: true is passed in a resource's config. Use this hook to send the measured data to your application analytics tracker. Default: noop. The method is invoked with two arguments:

    • the event string, 'API Fetch'
    • event data object with the following properties:
      • Resource (string): the name of the resource (taken from the entry in ResourceKeys)
      • data (object): data object supplied via the resource's config
      • options (object): options object supplied via the resource's config
      • duration (number): time in milliseconds between request and response

FAQs

  • Why?

    Yeah...isn't GraphQL the thing to use now? Why bother with a library for REST APIs—especially one that is based on a Backbone fork. It feels so....2012.

    GraphQL is awesome, but there are many reasons why you might not want to use it. Maybe you don't have the resources to ensure that all of your data can be accessed performantly; in that case, your single /graphql endpoint will only ever be as fast as your slowest data source. Maybe your existing REST API is working well and your eng org isn't going to prioritize any time to move away from it. Etc, etc, etc. resourcerer offers a way for your front-end team to quickly get up and running with declarative data fetching, request flows, and model caching.

  • Does resourcerer support SSR?

    There is no official documentation for its use in server-side rendering at this point. However, because passing models as props directly to a component bypasses fetching, it is likely that resourcerer can work nicely with an SSR setup that:

    1. passes instantiated models directly through the app before calling renderToString
    2. provides those models within a top-level <script> element that adds them directly to the ModelCache.
  • Does it support async rendering?

    Yes! withResources used to depend on componentWillReceiveProps, but as of v0.9 it has been updated to use componentDidUpdate instead. The upside is that it now supports async rendering; the downside, however, is that it requires an extra render before updating a resource (since componentDidUpdate gets called after render, setting any loading states will cause an additional subsequent render).

    useResources has always supported async rendering.

  • Can the withResources HOC be used with both function components and class components?

    Yes! The docs don't show it, but this is totally valid:

    const UserTodos = (props) => (
      <div className='MyClassWithTodosAndUsers'>
        {props.isLoading ? <Loader /> : null}
          
        {props.hasLoaded ? (
          <ul>
            {props.userTodosCollection.map((todoModel) => (
              <li key={todoModel.id}>
                {todoModel.get('name')}
              </li>
            )}
          </ul>
        ) : null}
          
        {props.hasErrored ? <ErrorMessage /> : null}
      </div>
    );
    
    export withResources((props, ResourceKeys) => {
      const now = Date.now();
      
      return {
        [ResourceKeys.USER_TODOS]: {
          data: {
            limit: 20,
            end_time: now,
            start_time: now - props.timeRange,
            sort_field: props.sortField
          },
          options: {userId: props.userId}
        }
      };
    })(UserTodos)
    

    There is one caveat, though—function components should not be wrapped in React.memo or they won't be updated when the resource updates.

  • Can resourcerer do anything other than GET requests?

    resourcerer only handles resource fetching (i.e. calling Schmackbone.Model.prototype.fetch). Note that this is not the same as only making GET requests; pass in a method: 'POST' property in a resource's config to turn the data property into a POST body, for example, when making a search request.

    For write operations, use Schmackbone Models' save and destroy methods directly:

    onClickSaveButton() {
      this.setState({isSaving: true});
    
      // any other mounted component in the application that uses this resource
      // will get re-rendered with the updated name as soon as this is called
      this.props.userTodoModel.save({name: 'Giving This Todo A New Name'})
          .then(() => notify('Todo save succeeded!'))
          .catch(() => notify('Todo save failed :/'))
          .then(() => this.setState({isSaving: false}));
    }
    
  • What about other data sources like websockets?

    resourcerer supports request/response-style semantics only. A similar package for declaratively linking message-pushing to React updates would be awesome—but it is not, at this point, part of this package.

  • How can we test components that use resourcerer?

    See the doc on testing components for more on that.

  • How big is the resourcerer package?

    5kB gzipped (excluding its Schmackbone, Underscore, and qs dependencies, which are an additional 17kB gzipped).

  • Semver?

    Yes. Releases will adhere to semver rules.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].