Skip to content

Instantly share code, notes, and snippets.

@karlbright
Created November 30, 2015 05:09
Show Gist options
  • Save karlbright/c036e10bbe93259a0c37 to your computer and use it in GitHub Desktop.
Save karlbright/c036e10bbe93259a0c37 to your computer and use it in GitHub Desktop.
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