Posterior is a small, powerful tool (~4kb, gzipped) that gives remote services intuitive, simple interfaces via declarative, hierarchical configurations.

posterior.min.js, posterior.js, https://unpkg.com/posterior
bower install posterior, npm install posterior, yarn add posterior
Build Status npm version npm

Quick Example

window.GitHub = new Posterior({
    url: 'https://api.github.com',
    load: function() {
        console.log('Requested:', this.cfg.url);
    },
    ESHA: {
        url: '/repos/esha/{0}',
        Version: {
            follows: 'tags_url',
            then: function(tags) {
                return tags[0].name;
            }
        }
    }
}, 'GitHub');

GitHub.ESHA.Version('posterior');
// Create a 'GitHub' function
  // base URL for all GitHub resources
  // will be called on XHR 'load' event
    // context will be the final configuration at load time

  // Create an ESHA sub-function that inherits from the GitHub one
    // appends to root URL, replaces {0} with first argument when called
    // Create a Version sub-function that inherits from the ESHA one
      // retrieve URL from 'tags_url' property of response to automatic ESHA call
      // then function to modify resolved value
        // tags is JSON object, already parsed for use



// 2nd argument gives name to instance for debugging output

// Calls the function with value to be inserted in URL

            

What Is This?

Posterior's simplifies configuration and encapsulation of "AJAX" calls. It takes a structured configuration object and converts it into a hierarchy of logically named functions. Those functions, when called, compose the desired XHR configuration and response handlers and return a Promise. That Promise will resolve (or reject) when all the configured request and response handling has completed.

To achieve better encapsulation, Posterior provides a number of high level features commonly needed for interacting with remote resources. Posterior functions can interpolate URL templates, wait for required dependencies to be resolved, throttle request rates, caching, follow URLS in linked resources, singleton resources, and automatic retry for failed requests. The intent of this encapsulation is to allow client-side developers to keep implementation details of their interactions with remote servers out of their client-side logic. All translation between the data the client requests/receives and the data the server expects/returns can be hidden behind a friendly, Promise-returning function.

Where Posterior really begins to shine is when the resources being configured are hierarchical, as it makes it trivial to for instances to inherit from and build upon each other by combining and composing properties and functions instead of simply overriding them. Posterior supports special syntax for controlling how properties with the same key are inherited and combined, but in most cases, these are unnecessary.

Configuration:

Composition

TODO: explanation and examples of how hierarchical composition works and can be controlled with syntax, Api.extend(), Api.config(cfg), etc