Add get latest post api fetch with title,excerpt,url return

This commit is contained in:
Amir Husayn Panahifar 2025-04-08 02:51:41 +03:30
parent 436475bc2e
commit 240aed2061

35
src/apis/main.js Normal file
View file

@ -0,0 +1,35 @@
import axios from "axios";
import { log } from "../utils/logger.js";
const getLatestPost = async () => {
try {
const latestpostUrl = "https://forum.parchlinux.com/latest.json";
const response = await axios.get(latestpostUrl);
const topics = response?.data?.topic_list?.topics;
if (!Array.isArray(topics) || topics.length === 0) {
log.warn("No topics found in latest post data");
return null;
}
const latestPost = topics.find(t => !t.pinned);
if (!latestPost) {
log.warn("No non-pinned topic found");
return null;
}
log.info("Fetched latest post successfully");
return {
title: latestPost.title,
excerpt: latestPost.excerpt,
url: `https://forum.parchlinux.com/t/${latestPost.slug}/${latestPost.id}`,
};
} catch (error) {
log.error("Error fetching latest post:", error?.message || error);
return null;
}
};
export { getLatestPost };