From 0902ad9e469f09b3a96043ae81888e997b80d433 Mon Sep 17 00:00:00 2001 From: fominmaksim Date: Mon, 22 Nov 2021 18:58:43 +0200 Subject: [PATCH] feat: created api typing --- src/services/api.ts | 20 +++++++++++++---- src/services/types.ts | 52 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/services/types.ts diff --git a/src/services/api.ts b/src/services/api.ts index 5f175e0..7201507 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -1,23 +1,35 @@ import axios from 'axios'; +import { ComingSoon, Top250Movies } from './types'; + const moviesApi = axios.create({ baseURL: 'https://imdb-api.com/en/API/', }); export const top250Movies = async () => { try { - const res = await moviesApi.get('/'); - console.warn(res); + const res = await moviesApi.get(`Top250Movies/${process.env.REACT_APP_API_KEY}`); + if (res.data.errorMessage) { + throw new Error(res.data.errorMessage); + } else { + return res.data.items; + } } catch (e) { console.warn(e); + return false; } }; export const comingSoon = async () => { try { - const res = await moviesApi.get('/'); - console.warn(res); + const res = await moviesApi.get(`ComingSoon/${process.env.REACT_APP_API_KEY}`); + if (res.data.errorMessage) { + throw new Error(res.data.errorMessage); + } else { + return res.data.items; + } } catch (e) { console.warn(e); + return false; } }; diff --git a/src/services/types.ts b/src/services/types.ts new file mode 100644 index 0000000..0d6e292 --- /dev/null +++ b/src/services/types.ts @@ -0,0 +1,52 @@ +export interface Top250Movies { + errorMessage: string; + items: Movie[]; +} +interface Movie { + // max.haveSexWith(typeScript) + crew: string; + fullTitle: string; + id: string; + imDbRating: string; + imDbRatingCount: string; + image: string; + rank: string; + title: string; + year: string; +} + +export interface ComingSoon { + errorMessage: string; + items: ComingSoonItem[]; +} +interface ComingSoonItem { + contentRating: string; + directorList: ComingSoonDirectorList[]; + directors: string; + fullTitle: string; + genreList: ComingSoonOtherList[]; + genres: string; + id: string; + imDbRating: string; + imDbRatingCount: string; + image: string; + metacriticRating: string; + plot: string; + releaseState: string; + runtimeMins: string; + runtimeStr: string; + starList: ComingSoonOtherList[]; + stars: string; + title: string; + year: string; +} + +interface ComingSoonDirectorList { + id: string; + name: string; +} + +interface ComingSoonOtherList { + key: string; + value: string; +}