You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
898 B
TypeScript
33 lines
898 B
TypeScript
// https://vike.dev/data
|
|
|
|
import type { Movie, MovieDetails } from "../types.js";
|
|
import { useConfig } from "vike-react/useConfig";
|
|
|
|
export type Data = Awaited<ReturnType<typeof data>>;
|
|
|
|
export const data = async () => {
|
|
// https://vike.dev/useConfig
|
|
const config = useConfig();
|
|
|
|
const response = await fetch("https://brillout.github.io/star-wars/api/films.json");
|
|
const moviesData = (await response.json()) as MovieDetails[];
|
|
|
|
config({
|
|
// Set <title>
|
|
title: `${moviesData.length} Star Wars Movies`,
|
|
});
|
|
|
|
// 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;
|
|
};
|
|
|
|
function minimize(movies: MovieDetails[]): Movie[] {
|
|
return movies.map((movie) => {
|
|
const { title, release_date, id } = movie;
|
|
return { title, release_date, id };
|
|
});
|
|
}
|