Servidor/controllers/anilistController.js

51 lines
1.3 KiB
JavaScript

const axios = require('axios');
const searchAniListMedia = async (req, res) => {
const { query, type } = req.query;
if (!query || !type || (type !== 'ANIME' && type !== 'MANGA')) {
return res.status(400).json({ error: 'Parámetros "query" y "type" (ANIME o MANGA) requeridos' });
}
const gqlQuery = {
query: `
query ($search: String, $type: MediaType) {
Page(perPage: 10) {
media(search: $search, type: $type) {
id
title {
romaji
}
coverImage {
large
}
}
}
}
`,
variables: {
search: query,
type
}
};
try {
const response = await axios.post('https://graphql.anilist.co', gqlQuery, {
headers: { 'Content-Type': 'application/json' }
});
const results = response.data.data.Page.media.map(item => ({
id: item.id,
title: item.title.romaji,
imageUrl: item.coverImage.large
}));
res.json(results);
} catch (error) {
console.error('AniList API error:', error.message);
res.status(500).json({ error: 'Error al consultar AniList' });
}
};
module.exports = { searchAniListMedia };