Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

api.ts

44 lines888 Bytes

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) {
16 return new Promise(function (resolve) {
17 setTimeout(resolve, t);
18 });
20​
21export async function fetchMovies() {
22 console.log('fetchMovies');
23​
24 await delay(200 + Math.floor(Math.random() * 2000));
25​
26 return movies
27 .slice(0, 100)
28 .map((movie) => ({ title: movie.title, year: movie.year })) as Promise<
29 Movie[]
30 >;
32​
33export async function fetchMovie(title: string) {
34 console.log('fetchMovie', title);
35​
36 await delay(200 + Math.floor(Math.random() * 2000));
37​
38 const result = movies.filter((item) => item.title === title);
39​
40 if (result.length == 0) {
41 throw new Error('Movie not found');
42 }
43 return result[0] as Promise<MovieDetails>;