-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
38 lines (36 loc) · 1.04 KB
/
api.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
const cache = require('./cache');
const embeds = require('./embeds');
const getUrls = async ({ urls, no_cache = false }) => {
console.debug(`Fetching embeds for ${urls.join(', ')}`);
console.debug(`No Cache is ${no_cache}`);
if (no_cache) {
return embeds.get(urls).then(fetched => {
return fetched;
});
} else {
return cache.get(urls).then(({ cachedEmbeds, notCachedUrls }) => {
console.debug(
`Fetching ${notCachedUrls.join(', ')} URLs from embeds.rocks`
);
return embeds
.get(notCachedUrls)
.then(fetchedEmbeds => cache.save(fetchedEmbeds))
.then(savedEmbeds => {
const responseEmbeds = Object.assign({}, cachedEmbeds, savedEmbeds);
console.debug(
`Requested ${urls.length} URLs. Returning ${
Object.keys(responseEmbeds).length
} embeds.`
);
return responseEmbeds;
});
});
}
};
const deleteUrls = async ({ urls }) => {
return cache.remove(urls);
};
module.exports = {
getUrls,
deleteUrls
};