cachejax
Load data from your Baobab model before making a remote request
Motivation
- Say you're writing a JS app with the URL
/messages
. - When a user navigates to
/messages
, you need to load all the messages for the logged in user. - Say you have another route:
messages/:id
. - If you want to support linking in your application (and you should!), now you need to re-fetch all the messages in that route.
- This is wasteful and forfeits one of the nice things about 'rich client' JS apps: avoiding round trips to the server for data.
cachejax is a small wrapper around the axios ajax library that supports data caching with baobab. It decides whether to fetch data directly from the client (baobab) or make a remote request.
It's designed to be used with cerebral and was extracted from a cerebral project, but can be used with only baobab.
Install
npm install cachejax
Usage
Config
Supply a config
object mapping paths to ajax endpoints:
const config = currentUser: mapping: 'http://oath.dev/api/v1/me.json' root: false messages: mapping: 'http://app.com/api/messages/v3/messages/:id' // you can use express style routes root: 'message' batch: true
config
options:
root: Boolean || String // default: true mapping: String // required batch: Boolean // default: false;
- root - does the response JSON have a root key?
{user: {...}}
or{...}
or customize the key name'user'
- mapping - associate a Baobab path with a URL to fetch data
- batch - will you make batched requests to this endpoint?
Initialize
Pass config
and your baobab
instance to the Cachejax
constructor:
; const baobab = {};const cachejax = ;
Call
Use cachejax
anywhere you would normally make an http request:
cachejax ;
cachejax.get()
will check if data exists atbaobab.get('currentUser')
if not, it will fetch the data fromhttp://oath.dev/api/v1/me.json
get()
always returns a Promise whether data is found on the client or fetched from the server.- data is available at
response.data
. The full axios response object will, however, be returned when a remote request is made.
API
-
#### cachejax.get(path||url, params, config) => Promise
path
- a baobab path name to fetch dataurl
- a URL to fetch data fromparams
- url params, query params, or attributes to filter by in baobabconfig
- any overrides or customization toCachejax() base config
Promise
- returns athen()
able object whether fetched from AJAX or baobab
Examples:
let cachejax = ;-
Using a baobab path
'conversations'
:let config = conversations: mapping: 'http://app.com/api/v1/conversations'cachejax // => baobab.get('conversations') or GET /api/v1/conversations -
baobab path with URL param (express.js style routes):
let config = conversations: mapping: 'http://app.com/api/v1/conversations:id'cachejax // => baobab.get('conversations') or GET /api/v1/conversations/3 -
baobab path with query param:
let config = conversations: mapping: 'http://app.com/api/v1/conversations'cachejax -
Normal AJAX call:
cachejax;
-
#### cachejax.batch([data], mappingFunction) => [Promise]
[data]
- an array of data, e.g. user_idsmappingFunction
- a function mapping data to a Request (Promise)[Promise]
: an array of fulfilled Promises
Given the config:
messages:mapping: 'http://app.com/api/v3/messages/:id'root: 'message'batch: truecachejax will exectue each
get()
concurrently (delegating to axios.all):let ids = 353354cachejax -
#### cachejax.setAuthorization(token)
- Sets a bearer token
Authorization
header for all ajax requests
- Sets a bearer token
-
#### axios proxy methods
- cachejax.all()
- cachejax.delete()
- cachejax.head()
- cachejax.post()
- cachejax.put()
- cachejax.patch()