Created
August 8, 2018 16:34
Star
You must be signed in to star a gist
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
// @flow | |
import cache from 'memory-cache' | |
import fetch from 'isomorphic-fetch' | |
const TTL_MILISECONDS: number = 10 * 60 * 1000 // ten minutes | |
export default async function<T>(url: string, options?: Object): Promise<T> { | |
const cachedResponse = cache.get(url) | |
if (cachedResponse) return cachedResponse | |
// If there is no cached response, | |
// do the actual call and store the response | |
return fetch(url, options) | |
.then(response => response.json()) | |
.then(response => { | |
cache.put(url, response, TTL_MILISECONDS) | |
return response | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment