From 9ec18b728a05b97daf442e5a64882f7f846a9cc6 Mon Sep 17 00:00:00 2001 From: dusk Date: Mon, 30 Sep 2024 06:50:54 +0300 Subject: [PATCH] feat: add jsonfeed api --- src/routes/entries/_jsonfeed/+server.ts | 34 +++++++++++++++++++++++++ src/routes/entries/_rss/+server.ts | 4 ++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/routes/entries/_jsonfeed/+server.ts diff --git a/src/routes/entries/_jsonfeed/+server.ts b/src/routes/entries/_jsonfeed/+server.ts new file mode 100644 index 0000000..7b41a11 --- /dev/null +++ b/src/routes/entries/_jsonfeed/+server.ts @@ -0,0 +1,34 @@ +import { PUBLIC_BASE_URL } from '$env/static/public'; +import { _allPosts, type PostData } from '../+layout.ts'; + +const entriesUrl = `${PUBLIC_BASE_URL}/entries`; + +export const GET = async ({ }) => { + return new Response( + render(_allPosts), + { + headers: { + 'content-type': 'application/json', + 'cache-control': 'no-store', + } + }) +}; + +const render = (posts: PostData[]) => { + return JSON.stringify({ + version: 'https://jsonfeed.org/version/1.1', + title: 'gaze.systems posts feed', + home_page_url: PUBLIC_BASE_URL, + feed_url: `${entriesUrl}/_jsonfeed`, + items: posts.map((post) => { + return { + id: post.path, + url: `${entriesUrl}/${post.path}`, + title: post.metadata.title, + summary: post.metadata.excerpt, + content_text: 'read the post on the website', + date_published: new Date(post.metadata.date).toISOString(), + } + }), + }) +} \ No newline at end of file diff --git a/src/routes/entries/_rss/+server.ts b/src/routes/entries/_rss/+server.ts index 0714bcc..b3ab76c 100644 --- a/src/routes/entries/_rss/+server.ts +++ b/src/routes/entries/_rss/+server.ts @@ -4,7 +4,9 @@ import { _allPosts, type PostData } from '../+layout.ts'; const entriesUrl = `${PUBLIC_BASE_URL}/entries`; export const GET = async ({ }) => { - return new Response(render(_allPosts), { + return new Response( + render(_allPosts), + { headers: { 'content-type': 'application/xml', 'cache-control': 'no-store',