Skip to content

Instantly share code, notes, and snippets.

@drenther
Created July 9, 2018 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drenther/dabfab9186b10c6ad20fdb9a2d120e62 to your computer and use it in GitHub Desktop.
Save drenther/dabfab9186b10c6ad20fdb9a2d120e62 to your computer and use it in GitHub Desktop.
utilites/apiCalls.js for next-pwa
/* ./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