|
1 | 1 | import { getCache } from "../cache"; |
2 | | -import { fetchLatestVersion } from "./pypi"; |
| 2 | +import { fetchLibrary } from "./pypi"; |
3 | 3 |
|
4 | 4 | const ONE_HOUR = 60 * 60 * 1000; |
| 5 | +const CACHE_PREFIX = "v1"; |
5 | 6 |
|
6 | | -export const getVersion = async (library: string) => { |
| 7 | +const getCacheKey = (library: string) => `${CACHE_PREFIX}-${library}`; |
| 8 | + |
| 9 | +export const getInfo = async ( |
| 10 | + library: string, |
| 11 | +): Promise<{ version: string; summary: string }> => { |
7 | 12 | const cache = getCache(); |
| 13 | + const key = getCacheKey(library); |
8 | 14 |
|
9 | | - const info = cache.get(library, null); |
| 15 | + let info = cache.get(key, null); |
10 | 16 |
|
11 | 17 | if (info) { |
12 | | - const { version, date } = JSON.parse(info); |
13 | | - const parsedDate = new Date(date); |
| 18 | + const data = JSON.parse(info); |
| 19 | + const parsedDate = new Date(data.date); |
14 | 20 |
|
15 | 21 | if (new Date().getTime() - parsedDate.getTime() < ONE_HOUR) { |
16 | | - console.log("Not fetching new value for", library); |
| 22 | + console.info("Not fetching new value for", library); |
17 | 23 |
|
18 | | - return version; |
| 24 | + return data.info; |
19 | 25 | } |
20 | 26 | } |
21 | 27 |
|
22 | | - const version = await fetchLatestVersion(library); |
| 28 | + const data = await fetchLibrary(library); |
| 29 | + |
| 30 | + info = { |
| 31 | + version: data.info.version as string, |
| 32 | + summary: data.info.summary as string, |
| 33 | + }; |
23 | 34 |
|
24 | | - await cache.put( |
25 | | - library, |
26 | | - JSON.stringify({ |
27 | | - version, |
28 | | - date: new Date(), |
29 | | - }), |
30 | | - ); |
| 35 | + await cache.put(key, JSON.stringify({ info, date: new Date() })); |
31 | 36 |
|
32 | | - return version; |
| 37 | + return info; |
33 | 38 | }; |
0 commit comments