movie-explorer/src/objects/tmdb.tsx

58 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-05-02 20:47:28 +00:00
import axios from 'axios'
import * as auth from '../auth.json';
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))
}
},
/**
* Calls TMDB/similar
*/
getSimilar: async function({ language, page }: Config, id: number, callback: Function) {
let res = await tmdb.get(id + '/similar', {
params: {
language,
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))
}
2024-05-02 20:47:28 +00:00
}
2024-05-05 21:59:33 +00:00
2024-05-02 20:47:28 +00:00
}