movie-explorer/src/objects/tmdb.tsx

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-05-02 20:47:28 +00:00
import axios from 'axios'
import * as auth from '../auth.json';
2024-05-06 09:09:21 +00:00
import { Movie } from './movie-wall';
2024-05-02 20:47:28 +00:00
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 {
2024-05-05 21:59:33 +00:00
/**
* 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',
2024-05-02 20:47:28 +00:00
{
params: {
language,
2024-05-05 21:59:33 +00:00
region,
page
2024-05-02 20:47:28 +00:00
}
})
2024-05-05 21:59:33 +00:00
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))
}
},
/**
2024-05-06 09:09:21 +00:00
* Calls TMDB/similar, then fires the callback with res.data.results.unshift(movie) as argument
2024-05-05 21:59:33 +00:00
*/
2024-05-06 09:09:21 +00:00
getSimilar: async function({ language, page }: Config, movie: Movie, callback: Function) {
let res = await tmdb.get(movie.id + '/similar', {
2024-05-05 21:59:33 +00:00
params: {
language,
page
}
})
console.log(res)
if (res.status === 200) {
2024-05-06 09:09:21 +00:00
const array: Array<Movie> = res.data.results
array.unshift(movie)
callback(array)
2024-05-05 21:59:33 +00:00
return true
} else {
throw Error("API call failed! Response: " + JSON.stringify(res))
}
2024-05-02 20:47:28 +00:00
}
2024-05-05 21:59:33 +00:00
2024-05-02 20:47:28 +00:00
}