plebdevs/src/pages/api/stackernews.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-09-02 13:50:07 -05:00
import axios from 'axios';
export default async function handler(req, res) {
try {
const response = await axios.post('https://stacker.news/api/graphql', {
query: `
2024-09-02 16:24:18 -05:00
query RecentDevItemsWithComments {
2024-09-02 13:50:07 -05:00
items(
2024-09-02 16:24:18 -05:00
sub: "devs",
sort: "recent"
2024-09-02 13:50:07 -05:00
) {
items {
id
title
url
createdAt
user {
name
}
sats
2024-09-02 16:24:18 -05:00
comments {
id
text
createdAt
user {
name
}
sats
}
2024-09-02 13:50:07 -05:00
}
}
}
`
}, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
});
res.status(200).json(response.data);
} catch (error) {
console.error('Error fetching from Stacker News:', error.response ? error.response.data : error.message);
res.status(500).json({ error: 'An error occurred while fetching data' });
}
}