96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
import axios from 'axios'
|
|
import * as auth from '../auth.json';
|
|
import { Movie } from './movie-wall';
|
|
import { Config } from '../App';
|
|
|
|
const tmdb = axios.create({
|
|
baseURL: `https://api.themoviedb.org/3/movie`,
|
|
headers: {
|
|
Authorization: 'Bearer ' + auth.token,
|
|
'Content-Type': 'application/json',
|
|
}
|
|
})
|
|
export default {
|
|
/**
|
|
* Calls tmdb API/popular and then fires the callback with res.data.results as argument
|
|
* @param {Function} callback
|
|
* @returns {boolean}
|
|
*/
|
|
getPopular: async function({ language, region, page }: Config, callback: Function) {
|
|
let res = await tmdb.get('/popular',
|
|
{
|
|
params: {
|
|
language,
|
|
region,
|
|
page
|
|
}
|
|
})
|
|
console.log(res)
|
|
if (res.status === 200) {
|
|
res = res.data.results
|
|
callback(res)
|
|
return true
|
|
} else {
|
|
throw Error("API call failed! Response: " + JSON.stringify(res))
|
|
}
|
|
},
|
|
/**
|
|
* Calls TMDB/similar, then fires the callback with res.data.results.unshift(movie) as argument
|
|
*/
|
|
getSimilar: async function({ language, page }: Config, movie: Movie, callback: Function) {
|
|
let res = await tmdb.get(movie.id + '/similar', {
|
|
params: {
|
|
language,
|
|
page
|
|
}
|
|
})
|
|
console.log(res)
|
|
if (res.status === 200) {
|
|
const array: Array<Movie> = res.data.results
|
|
.filter((e: Movie) => {
|
|
return e.poster_path
|
|
})
|
|
array.splice(19)
|
|
// array.unshift(movie)
|
|
callback(array)
|
|
return true
|
|
} else {
|
|
throw Error("API call failed! Response: " + JSON.stringify(res))
|
|
}
|
|
|
|
},
|
|
/**
|
|
*Calls TMDB/images, and sets the backdrop state accordingly
|
|
*
|
|
*/
|
|
getBackdrop: async function(movie: Movie, setBackdrop: Function) {
|
|
let res = await tmdb.get(movie.id + '/images')
|
|
console.log(res)
|
|
if (res.status === 200) {
|
|
const file_path = res.data.backdrops[0].file_path
|
|
console.log("file_path:" + file_path)
|
|
const imgUrl = this.makeBgImgUrl(file_path)
|
|
console.log("imgUrl: " + imgUrl)
|
|
setBackdrop(imgUrl)
|
|
} else {
|
|
throw Error("API call failed! Response: " + JSON.stringify(res))
|
|
}
|
|
|
|
|
|
},
|
|
makeBgImgUrl:
|
|
/**
|
|
* Returns a complete tmdb image url formatted for css
|
|
*/
|
|
function(path: string) {
|
|
return "url(https://image.tmdb.org/t/p/w1280" + path + ")"
|
|
},
|
|
makeImgUrl:
|
|
/**
|
|
* Returns a complete tmdb img url
|
|
*/
|
|
function(path: string) {
|
|
return "https://image.tmdb.org/t/p/w500" + path
|
|
}
|
|
}
|