source: pages/star-wars/index/+data.ts@ 8ee4553

main
Last change on this file since 8ee4553 was 8ee4553, checked in by Bati <no-reply@…>, 7 months ago

scaffold Vike app with Bati

  • Property mode set to 100644
File size: 898 bytes
RevLine 
[8ee4553]1// https://vike.dev/data
2
3import { useConfig } from "vike-react/useConfig";
4import type { Movie, MovieDetails } from "../types.js";
5
6export type Data = Awaited<ReturnType<typeof data>>;
7
8export async function data() {
9 // https://vike.dev/useConfig
10 const config = useConfig();
11
12 const response = await fetch("https://brillout.github.io/star-wars/api/films.json");
13 const moviesData = (await response.json()) as MovieDetails[];
14
15 config({
16 // Set <title>
17 title: `${moviesData.length} Star Wars Movies`,
18 });
19
20 // We remove data we don't need because the data is passed to the client; we should
21 // minimize what is sent over the network.
22 const movies = minimize(moviesData);
23
24 return { movies };
25}
26
27function minimize(movies: MovieDetails[]): Movie[] {
28 return movies.map((movie) => {
29 const { title, release_date, id } = movie;
30 return { title, release_date, id };
31 });
32}
Note: See TracBrowser for help on using the repository browser.