43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import type { Article, Category } from '#shared/types/blog'
|
|||
|
|
|
||
|
|
interface RawArticle {
|
||
|
|
id: string
|
||
|
|
title: string
|
||
|
|
slug: string
|
||
|
|
publishedAt: string
|
||
|
|
content: string | null
|
||
|
|
cover: string
|
||
|
|
coverAlt: string | null
|
||
|
|
authorName: string | null
|
||
|
|
expand?: { category?: Category }
|
||
|
|
}
|
||
|
|
|
||
|
|
export default defineEventHandler(async (event): Promise<Article> => {
|
||
|
|
const slug = getRouterParam(event, 'slug')
|
||
|
|
|
||
|
|
if (!slug) {
|
||
|
|
throw createError({ statusCode: 400, statusMessage: 'Missing article slug' })
|
||
|
|
}
|
||
|
|
|
||
|
|
const response = await pbFetch<PbList<RawArticle>>(
|
||
|
|
`/api/collections/articles/records?fields=${ARTICLE_DETAIL_FIELDS}&expand=category&filter=${encodeURIComponent(`slug = ${quote(slug)}`)}&${pagination(1, 1)}`
|
||
|
|
)
|
||
|
|
|
||
|
|
const article = response.items[0]
|
||
|
|
|
||
|
|
if (!article) {
|
||
|
|
throw createError({ statusCode: 404, statusMessage: 'Article not found' })
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
title: article.title,
|
||
|
|
slug: article.slug,
|
||
|
|
publishedAt: article.publishedAt,
|
||
|
|
cover: toMediaImage('articles', article.id, article.cover, article.coverAlt),
|
||
|
|
category: article.expand?.category ?? null,
|
||
|
|
html: renderMarkdown(article.content),
|
||
|
|
summary: summarise(article.content),
|
||
|
|
author: article.authorName || null,
|
||
|
|
}
|
||
|
|
})
|