Created
July 9, 2018 10:26
-
-
Save drenther/dabfab9186b10c6ad20fdb9a2d120e62 to your computer and use it in GitHub Desktop.
utilites/apiCalls.js for next-pwa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ./utilities/apiCalls.js */ | |
import 'isomorphic-unfetch'; | |
import { API_KEY } from './config'; | |
const BASE_URI = 'https://api.themoviedb.org/3/movie'; | |
const IMAGE_BASE_URI = 'https://image.tmdb.org/t/p'; | |
const fetchWithErrorHandling = async url => { | |
try { | |
return await (await fetch(url)).json(); | |
} catch (err) { | |
return { error: true }; | |
} | |
}; | |
export const getMovieDetails = async id => | |
fetchWithErrorHandling( | |
`${BASE_URI}/${id}?api_key=${API_KEY}&language=en-US&append_to_response=credits` | |
); | |
export const getUpcomingMovies = async () => | |
fetchWithErrorHandling( | |
`${BASE_URI}/upcoming?api_key=${API_KEY}&language=en-US&page=1` | |
); | |
export const getImageSrc = (path, size) => | |
`${IMAGE_BASE_URI}/${size ? `w${size}` : 'original'}${path}`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment