25 lines
794 B
TypeScript
25 lines
794 B
TypeScript
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) }
|
||
|
|
})
|