-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (45 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import got from 'got'
/*
* Returns `got` wrapped with cache to be used as normal got
*/
export default function gotCached(config) {
if (!config.cache) throw Error('cache is a required option')
const { cache } = config
function setCache(key, value, options) {
if(options.json) value = JSON.stringify(value)
cache.set(key, value)
}
function getCache(key, options) {
return cache.get(key)
.then(value => {
if (!value) return null
if (options.json) value = JSON.parse(value)
return Promise.resolve({
status: 200,
body: value
})
})
}
function cachingGot(url, options) {
return got(url, options).then(response => {
setCache(url, response.body, options)
return Promise.resolve(response)
})
}
function cachedGot(url, options = {}) {
// return plain got for non-GET requests
if (options.method && options.method !== 'GET') {
return got(url, options)
}
return getCache(url, options)
.then(cached => {
// return the cached result if it exist
if(cached) return cached
// return got response after setting cache
return cachingGot(url, options)
})
}
return Object.assign(cachedGot, got, {
get: (url, options) => cachedGot(url, options)
})
}