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:
953 bytes
|
| Rev | Line | |
|---|
| [8ee4553] | 1 | // https://vike.dev/data
|
|---|
| 2 |
|
|---|
| 3 | import type { PageContextServer } from "vike/types";
|
|---|
| 4 | import { useConfig } from "vike-react/useConfig";
|
|---|
| 5 | import type { MovieDetails } from "../types.js";
|
|---|
| 6 |
|
|---|
| 7 | export type Data = Awaited<ReturnType<typeof data>>;
|
|---|
| 8 |
|
|---|
| 9 | export async function data(pageContext: PageContextServer) {
|
|---|
| 10 | // https://vike.dev/useConfig
|
|---|
| 11 | const config = useConfig();
|
|---|
| 12 |
|
|---|
| 13 | const response = await fetch(`https://brillout.github.io/star-wars/api/films/${pageContext.routeParams.id}.json`);
|
|---|
| 14 | let movie = (await response.json()) as MovieDetails;
|
|---|
| 15 |
|
|---|
| 16 | config({
|
|---|
| 17 | // Set <title>
|
|---|
| 18 | title: movie.title,
|
|---|
| 19 | });
|
|---|
| 20 |
|
|---|
| 21 | // We remove data we don't need because the data is passed to
|
|---|
| 22 | // the client; we should minimize what is sent over the network.
|
|---|
| 23 | movie = minimize(movie);
|
|---|
| 24 |
|
|---|
| 25 | return { movie };
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | function minimize(movie: MovieDetails): MovieDetails {
|
|---|
| 29 | const { id, title, release_date, director, producer } = movie;
|
|---|
| 30 | return { id, title, release_date, director, producer };
|
|---|
| 31 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.