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

api.ts

39 lines937 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, v: () => void) {
16 return new Promise(function (resolve) {
17 setTimeout(resolve.bind(null, v), t);
18 });
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[]>;
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>;