main
|
Last change
on this file since 66bf4fd 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
|
| Line | |
|---|
| 1 | // https://vike.dev/data
|
|---|
| 2 |
|
|---|
| 3 | import { useConfig } from "vike-react/useConfig";
|
|---|
| 4 | import type { Movie, MovieDetails } from "../types.js";
|
|---|
| 5 |
|
|---|
| 6 | export type Data = Awaited<ReturnType<typeof data>>;
|
|---|
| 7 |
|
|---|
| 8 | export 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 |
|
|---|
| 27 | function 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.