Files
blog/frontend/server/api/articles/[slug].get.ts
T

25 lines
794 B
TypeScript
Raw Normal View History

import type { Article } from '#shared/types/blog'
type RawArticle = Omit<Article, 'html' | 'summary'> & { content: string | null }
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 strapiFetch<StrapiList<RawArticle>>(
`/api/articles?${ARTICLE_DETAIL_QUERY}&filters[slug][$eq]=${encodeURIComponent(slug)}&pagination[pageSize]=1`
)
const article = response.data[0]
if (!article) {
throw createError({ statusCode: 404, statusMessage: 'Article not found' })
}
const { content, ...rest } = article
return { ...rest, html: renderMarkdown(content), summary: summarise(content) }
})