Skip to content
This repository has been archived by the owner on Dec 23, 2019. It is now read-only.

Commit

Permalink
add axiosStore (#27)
Browse files Browse the repository at this point in the history
* add axiosStore

* update changelog and package
  • Loading branch information
cvburgess authored Feb 27, 2018
1 parent 3094433 commit 8b1632e
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.11.0

- Add axiosStore from usePF/axios-store-plugin

## 0.10.0

- Rename `perch-data`
Expand Down
97 changes: 97 additions & 0 deletions lib/axiosStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _store = require("store");

var _store2 = _interopRequireDefault(_store);

var _expire = require("store/plugins/expire");

var _expire2 = _interopRequireDefault(_expire);

var _shallowequal = require("shallowequal");

var _shallowequal2 = _interopRequireDefault(_shallowequal);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var SECOND = 1000;
var TEN_SECONDS_FROM_NOW = function TEN_SECONDS_FROM_NOW() {
return new Date().getTime() + 10 * SECOND;
};
var PLACEHOLDER = { loading: true };

_store2.default.addPlugin(_expire2.default);

var createPlaceholder = function createPlaceholder(cacheKey) {
return Promise.resolve(_store2.default.set(cacheKey, PLACEHOLDER, TEN_SECONDS_FROM_NOW()));
};

var axiosStore = function axiosStore(axiosInstance) {
var reqOrCache = function reqOrCache() {
for (var _len = arguments.length, arg = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
arg[_key - 1] = arguments[_key];
}

var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

var cacheKey = "axios__" + JSON.stringify(options);
var cachedData = _store2.default.get(cacheKey);
return cachedData ? Promise.resolve(_extends({}, cachedData, {
__cacheKey: cacheKey,
__fromCache: true
})) : createPlaceholder(cacheKey).then(function () {
return axiosInstance.get.apply(axiosInstance, arg);
}).then(function (_ref) {
var data = _ref.data;
return _extends({}, data, { __cacheKey: cacheKey });
}).catch(function (error) {
if ((0, _shallowequal2.default)(_store2.default.get(cacheKey), PLACEHOLDER)) _store2.default.remove(cacheKey);
throw error;
});
};

// Check that the arguments are in the correct format
var axiosWithCache = function axiosWithCache() {
for (var _len2 = arguments.length, arg = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
arg[_key2] = arguments[_key2];
}

if (arg.length === 1 && (arg[0].method === "get" || arg[0].method === undefined)) {
return reqOrCache.apply(undefined, [arg[0]].concat(arg));
}
return axiosInstance.apply(undefined, arg);
};

// Overwrite the get method to support cache
axiosWithCache.get = function () {
for (var _len3 = arguments.length, arg = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
arg[_key3] = arguments[_key3];
}

if (arg.length === 1) {
return reqOrCache.apply(undefined, [{ url: arg[0] }].concat(arg));
} else if (arg.length === 2) {
return reqOrCache.apply(undefined, [_extends({ url: arg[0] }, arg[1])].concat(arg));
}
return axiosInstance.get.apply(axiosInstance, arg);
};

// Do NOT attempt to cache these methods
var skipMethods = ["delete", "head", "options", "post", "put", "patch"];

skipMethods.forEach(function (method) {
axiosWithCache[method] = function () {
return axiosInstance[method].apply(axiosInstance, arguments);
};
});

return axiosWithCache;
};

exports.default = axiosStore;
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cache = exports.withData = undefined;
exports.axiosStore = exports.cache = exports.withData = undefined;

var _withData2 = require("./withData");

Expand All @@ -13,9 +13,14 @@ var _cache2 = require("./cache");

var _cache = _interopRequireWildcard(_cache2);

var _axiosStore2 = require("./axiosStore");

var _axiosStore3 = _interopRequireDefault(_axiosStore2);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.withData = _withData3.default;
exports.cache = _cache;
exports.cache = _cache;
exports.axiosStore = _axiosStore3.default;
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "perch-data",
"version": "0.10.0",
"version": "0.11.0",
"description": "Utilities for managing data. Inspired by react-apollo.",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -45,6 +45,7 @@
},
"dependencies": {
"immutability-helper": "^2.6.4",
"shallowequal": "^1.0.2",
"store": "^2.0.12"
}
}
65 changes: 65 additions & 0 deletions src/axiosStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import store from "store";
import expirePlugin from "store/plugins/expire";
import shallowEqual from "shallowequal";

const SECOND = 1000;
const TEN_SECONDS_FROM_NOW = () => new Date().getTime() + 10 * SECOND;
const PLACEHOLDER = { loading: true };

store.addPlugin(expirePlugin);

const createPlaceholder = cacheKey =>
Promise.resolve(store.set(cacheKey, PLACEHOLDER, TEN_SECONDS_FROM_NOW()));

const axiosStore = axiosInstance => {
const reqOrCache = (options = {}, ...arg) => {
const cacheKey = `axios__${JSON.stringify(options)}`;
const cachedData = store.get(cacheKey);
return cachedData
? Promise.resolve({
...cachedData,
__cacheKey: cacheKey,
__fromCache: true
})
: createPlaceholder(cacheKey)
.then(() => axiosInstance.get(...arg))
.then(({ data }) => ({ ...data, __cacheKey: cacheKey }))
.catch(error => {
if (shallowEqual(store.get(cacheKey), PLACEHOLDER))
store.remove(cacheKey);
throw error;
});
};

// Check that the arguments are in the correct format
const axiosWithCache = (...arg) => {
if (
arg.length === 1 &&
(arg[0].method === "get" || arg[0].method === undefined)
) {
return reqOrCache(arg[0], ...arg);
}
return axiosInstance(...arg);
};

// Overwrite the get method to support cache
axiosWithCache.get = (...arg) => {
if (arg.length === 1) {
return reqOrCache({ url: arg[0] }, ...arg);
} else if (arg.length === 2) {
return reqOrCache({ url: arg[0], ...arg[1] }, ...arg);
}
return axiosInstance.get(...arg);
};

// Do NOT attempt to cache these methods
const skipMethods = ["delete", "head", "options", "post", "put", "patch"];

skipMethods.forEach(method => {
axiosWithCache[method] = (...arg) => axiosInstance[method](...arg);
});

return axiosWithCache;
};

export default axiosStore;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export withData from "./withData";
export * as cache from "./cache";
export axiosStore from "./axiosStore";

0 comments on commit 8b1632e

Please sign in to comment.