Capital/app/feed/route.ts

37 lines
996 B
TypeScript
Raw Normal View History

2024-02-24 14:08:36 +00:00
import { Feed } from "feed";
2024-02-24 10:54:47 +00:00
import { SITE_DESCRIPTION, SITE_NAME, SITE_URL } from "@/app/consts";
2024-02-24 14:08:36 +00:00
import { client } from "@/sanity/lib/client";
2024-02-24 10:54:47 +00:00
export async function GET() {
2024-02-24 14:08:36 +00:00
const feed = new Feed({
2024-02-24 10:54:47 +00:00
title: SITE_NAME,
description: SITE_DESCRIPTION,
2024-02-24 14:08:36 +00:00
id: SITE_URL,
link: SITE_URL,
favicon: `${SITE_URL}/favicon.png`,
feedLinks: { atom: `${SITE_URL}/feed` },
2024-02-24 13:56:35 +00:00
language: "zh-CN",
2024-02-24 14:08:36 +00:00
copyright: `Copyright © ${new Date().getFullYear()} SmartSheep Studio`,
2024-02-24 10:54:47 +00:00
});
2024-02-24 14:08:36 +00:00
const posts = await client.fetch<any[]>(`*[_type == "post"] {
title, description, slug, publishedAt,
}`);
posts.forEach((item) => {
feed.addItem({
id: `${SITE_URL}/p/${item.slug.current}`,
link: `${SITE_URL}/p/${item.slug.current}`,
2024-02-24 10:54:47 +00:00
title: item.title,
description: item.description ?? "No description yet.",
2024-02-24 14:08:36 +00:00
date: new Date(item.publishedAt)
2024-02-24 10:54:47 +00:00
});
});
2024-02-24 14:08:36 +00:00
return new Response(feed.rss2(), {
2024-02-24 10:54:47 +00:00
headers: {
2024-02-24 14:08:36 +00:00
"content-type": "application/xml"
}
2024-02-24 10:54:47 +00:00
});
2024-02-24 13:56:35 +00:00
}