Created
August 8, 2018 16:34
-
-
Save rista404/fd7a391728ba07f6125c59eb7877c3f4 to your computer and use it in GitHub Desktop.
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