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.
35 lines
1003 B
TypeScript
35 lines
1003 B
TypeScript
// https://vike.dev/data
|
|
|
|
import type { PageContextServer } from "vike/types";
|
|
import type { MovieDetails } from "../types.js";
|
|
import { useConfig } from "vike-react/useConfig";
|
|
|
|
export type Data = Awaited<ReturnType<typeof data>>;
|
|
|
|
export const data = async (pageContext: PageContextServer) => {
|
|
// https://vike.dev/useConfig
|
|
const config = useConfig();
|
|
|
|
const response = await fetch(
|
|
`https://brillout.github.io/star-wars/api/films/${pageContext.routeParams.id}.json`,
|
|
);
|
|
let movie = (await response.json()) as MovieDetails;
|
|
|
|
config({
|
|
// Set <title>
|
|
title: movie.title,
|
|
});
|
|
|
|
// We remove data we don't need because the data is passed to
|
|
// the client; we should minimize what is sent over the network.
|
|
movie = minimize(movie);
|
|
|
|
return movie;
|
|
};
|
|
|
|
function minimize(movie: MovieDetails): MovieDetails {
|
|
const { id, title, release_date, director, producer } = movie;
|
|
const minimizedMovie = { id, title, release_date, director, producer };
|
|
return minimizedMovie;
|
|
}
|