/
+data.js
35 lines (28 loc) 路 1012 Bytes
/
+data.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
// https://vike.dev/data
export { data }
// The node-fetch package (which only works on the server-side) can be used since
// this file always runs on the server-side, see https://vike.dev/data#server-side
import fetch from 'node-fetch'
// eslint-disable-next-line no-unused-vars
const data = async (pageContext) => {
await sleep(700) // Simulate slow network
const response = await fetch('https://brillout.github.io/star-wars/api/films.json')
const moviesData = await response.json()
// We remove data we don't need because the data is passed to the client; we should
// minimize what is sent over the network.
const movies = minimize(moviesData)
return {
movies,
// The page's <title>
title: `${movies.length} Star Wars Movies`
}
}
function minimize(movies) {
return movies.map((movie) => {
const { title, release_date, id } = movie
return { title, release_date, id }
})
}
function sleep(milliseconds) {
return new Promise((r) => setTimeout(r, milliseconds))
}