Add get latest post api fetch with title,excerpt,url return
This commit is contained in:
parent
436475bc2e
commit
240aed2061
1 changed files with 35 additions and 0 deletions
35
src/apis/main.js
Normal file
35
src/apis/main.js
Normal 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 };
|
Loading…
Add table
Add a link
Reference in a new issue