Created
November 30, 2015 05:09
-
-
Save karlbright/c036e10bbe93259a0c37 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import _fetch from 'isomorphic-fetch' | |
import checkStatus from '../src/utils/check-status' | |
import parseBody from '../src/utils/parse-body' | |
import jsonFile from 'jsonfile' | |
let cache = {} | |
const cachePath = __dirname + '/cache.json' | |
export default function fetch (url, ignoreCache = false) { | |
if (!ignoreCache) { | |
try { | |
cache = jsonFile.readFileSync(cachePath) | |
if (cache[url]) return Promise.resolve(cache[url]) | |
} catch (e) { | |
cache = {} | |
} | |
} | |
return _fetch(url).then(checkStatus).then(parseBody) | |
.then(response => { | |
if (!ignoreCache) { | |
cache[url] = response | |
jsonFile.writeFileSync(cachePath, cache) | |
} | |
return response | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment