This repository has been archived by the owner on Dec 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add axiosStore * update changelog and package
- Loading branch information
Showing
7 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |