Open History to see the last commit
1import movies from '../data/movies.json';
2β
3export type Movie = {
4 title: string;
5 year: number;
6};
7β
8export type MovieDetails = Movie & {
9 info: {
10 plot: string;
11 actors: string[];
12 };
13};
14β
15function delay(t: number, v: () => void) {
16 return new Promise(function (resolve) {
17 setTimeout(resolve.bind(null, v), t);
18 });
19}
20β
21export async function fetchMovies() {
22 console.log('fetchMovies');
23 return delay(200 + Math.floor(Math.random() * 2000), () =>
24 movies
25 .slice(0, 100)
26 .map((movie) => ({ title: movie.title, year: movie.year }))
27 ) as Promise<Movie[]>;
28}
29β
30export async function fetchMovie(title: string) {
31 console.log('fetchMovie', title);
32 return delay(200 + Math.floor(Math.random() * 2000), () => {
33 const result = movies.filter((item) => item.title === title);
34 if (result.length == 0) {
35 throw new Error('Movie not found');
36 }
37 return result[0];
38 }) as Promise<MovieDetails>;
39}