diff --git a/README.md b/README.md index f0d12af..1156d94 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ withData({ #### Cache control options -By default, every entry is cached for one minute. You can overwrite this by passing an array (instead of a function) as the entry's value, with the first item being the action (function that returns a promise) and the second being an object with any of the following properties: +By default, every entry is cached for one second. You can overwrite this by passing an array (instead of a function) as the entry's value, with the first item being the action (function that returns a promise) and the second being an object with any of the following properties: - `maxAge: Number` - sets the time (in seconds) that the data should be cached - `noCache: Boolean` - skips the cache lookup and forces the action to be run @@ -175,33 +175,3 @@ const Notifications = ({ data: { notifications } }) => ( Currently `withData` assumes that your error will be [formatted like an Axios error](https://github.com/axios/axios#handling-errors). This was explicitly added to filter out any errors from the child component that were not caused by data fetching. In the future, this may become more generic and support other formats. - -### Configuration - -This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) to handle global defaults. You can specify a default `maxAge`, `noCache`, and `pollInterval`. - -#### via `package.json` file - -The following code will disable all caching. - -```json -... - "withdata": { - "maxAge": 0, - "noCache": true - } -... -``` - -#### via `withdata.config.js` file - -The following code will disable all caching (same as above). - -```js - const config = { - "maxAge": 0, - "noCache": true - }; - - export default config; -``` diff --git a/lib/cache.js b/lib/cache.js index f89b146..0f96a8b 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -19,10 +19,6 @@ var _observe = require("store/plugins/observe"); var _observe2 = _interopRequireDefault(_observe); -var _cosmiconfig = require("cosmiconfig"); - -var _cosmiconfig2 = _interopRequireDefault(_cosmiconfig); - function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } @@ -31,18 +27,13 @@ var SECOND = 1000; _store2.default.addPlugin([_expire2.default, _observe2.default]); -var explorer = (0, _cosmiconfig2.default)("withdata", { sync: true }); -var defaultConfig = explorer.load(process.cwd()) || {}; +var defaultConfig = { maxAge: 1 }; var set = exports.set = function set(key, value) { var maxAge = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultConfig.maxAge; - if (maxAge) { - var expiresAt = new Date().getTime() + maxAge * SECOND; - _store2.default.set(key, value, expiresAt); - } else { - _store2.default.set(key, value); - } + var expiresAt = new Date().getTime() + maxAge * SECOND; + _store2.default.set(key, value, expiresAt); }; var observeData = function observeData(defaultKey, dataFn, onNext, onError) { diff --git a/package-lock.json b/package-lock.json index 82525a9..d3e0daf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -88,6 +88,7 @@ "version": "1.0.9", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz", "integrity": "sha1-c9g7wmP4bpf4zE9rrhsOkKfSLIY=", + "dev": true, "requires": { "sprintf-js": "1.0.3" } @@ -1274,27 +1275,6 @@ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", "dev": true }, - "cosmiconfig": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-3.1.0.tgz", - "integrity": "sha512-zedsBhLSbPBms+kE7AH4vHg6JsKDz6epSv2/+5XHs8ILHlgDciSJfSWf8sX9aQ52Jb7KI7VswUTsLpR/G0cr2Q==", - "requires": { - "is-directory": "0.3.1", - "js-yaml": "3.10.0", - "parse-json": "3.0.0", - "require-from-string": "2.0.1" - }, - "dependencies": { - "parse-json": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-3.0.0.tgz", - "integrity": "sha1-+m9HsY4jgm6tMvJj50TQ4ehH+xM=", - "requires": { - "error-ex": "1.3.1" - } - } - } - }, "d": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/d/-/d-1.0.0.tgz", @@ -1394,6 +1374,7 @@ "version": "1.3.1", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.1.tgz", "integrity": "sha1-+FWobOYa3E6GIcPNoh56dhLDqNw=", + "dev": true, "requires": { "is-arrayish": "0.2.1" } @@ -1688,7 +1669,8 @@ "esprima": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz", - "integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw==" + "integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw==", + "dev": true }, "esquery": { "version": "1.0.0", @@ -3003,7 +2985,8 @@ "is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", - "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=" + "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", + "dev": true }, "is-binary-path": { "version": "1.0.1", @@ -3051,11 +3034,6 @@ "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", "dev": true }, - "is-directory": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/is-directory/-/is-directory-0.3.1.tgz", - "integrity": "sha1-YTObbyR1/Hcv2cnYP1yFddwVSuE=" - }, "is-dotfile": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/is-dotfile/-/is-dotfile-1.0.3.tgz", @@ -3244,6 +3222,7 @@ "version": "3.10.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.10.0.tgz", "integrity": "sha512-O2v52ffjLa9VeM43J4XocZE//WT9N0IiwDa3KSHH7Tu8CtH+1qM8SIZvnsTh6v+4yFy5KUY3BHUVwjpfAWsjIA==", + "dev": true, "requires": { "argparse": "1.0.9", "esprima": "4.0.0" @@ -3950,11 +3929,6 @@ "is-finite": "1.0.2" } }, - "require-from-string": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.1.tgz", - "integrity": "sha1-xUUjPp19pmFunVmt+zn8n1iGdv8=" - }, "require-uncached": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/require-uncached/-/require-uncached-1.0.3.tgz", @@ -4101,7 +4075,8 @@ "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", + "dev": true }, "store": { "version": "2.0.12", diff --git a/package.json b/package.json index d6fe96a..5a35a15 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "react": ">=15.0.0" }, "dependencies": { - "cosmiconfig": "^3.1.0", "immutability-helper": "^2.4.0", "store": "^2.0.12" } diff --git a/src/cache.js b/src/cache.js index 70c57aa..98425df 100644 --- a/src/cache.js +++ b/src/cache.js @@ -1,22 +1,16 @@ import store from "store"; import expirePlugin from "store/plugins/expire"; import observePlugin from "store/plugins/observe"; -import cosmiconfig from "cosmiconfig"; const SECOND = 1000; store.addPlugin([expirePlugin, observePlugin]); -const explorer = cosmiconfig("withdata", { sync: true }); -const defaultConfig = explorer.load(process.cwd()) || {}; +const defaultConfig = { maxAge: 1 }; export const set = (key, value, maxAge = defaultConfig.maxAge) => { - if (maxAge) { - const expiresAt = new Date().getTime() + maxAge * SECOND; - store.set(key, value, expiresAt); - } else { - store.set(key, value); - } + const expiresAt = new Date().getTime() + maxAge * SECOND; + store.set(key, value, expiresAt); }; export const observeData = (