cloudflare/vinext
Publicmirrored from https://github.com/cloudflare/vinextAvailable
examples/hackernews/lib/get-item.js
29lines · modecode
| 1 | import fetchData from './fetch-data' |
| 2 | |
| 3 | export default async function getItem(id) { |
| 4 | const val = await fetchData(`item/${id}`) |
| 5 | if (val) { |
| 6 | return transform(val) |
| 7 | } else { |
| 8 | return null |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | export function transform(val) { |
| 13 | if (val) { |
| 14 | return { |
| 15 | id: val.id, |
| 16 | url: val.url || '', |
| 17 | user: val.by, |
| 18 | // time is seconds since epoch, not ms |
| 19 | date: new Date(val.time * 1000).getTime() || 0, |
| 20 | // sometimes `kids` is `undefined` |
| 21 | comments: val.kids || [], |
| 22 | commentsCount: val.descendants || 0, |
| 23 | score: val.score, |
| 24 | title: val.title |
| 25 | } |
| 26 | } else { |
| 27 | return null |
| 28 | } |
| 29 | } |
| 30 | |