43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import type { ArticleSummary, Category, Paginated } from '#shared/types/blog'
|
|||
|
|
|
||
|
|
interface RawArticleSummary {
|
||
|
|
id: string
|
||
|
|
title: string
|
||
|
|
slug: string
|
||
|
|
publishedAt: string
|
||
|
|
cover: string
|
||
|
|
coverAlt: string | null
|
||
|
|
expand?: { category?: Category }
|
||
|
|
}
|
||
|
|
|
||
|
|
function toSummary(raw: RawArticleSummary): ArticleSummary {
|
||
|
|
return {
|
||
|
|
title: raw.title,
|
||
|
|
slug: raw.slug,
|
||
|
|
publishedAt: raw.publishedAt,
|
||
|
|
cover: toMediaImage('articles', raw.id, raw.cover, raw.coverAlt),
|
||
|
|
category: raw.expand?.category ?? null,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export default defineEventHandler(async (event): Promise<Paginated<ArticleSummary>> => {
|
||
|
|
const query = getQuery(event)
|
||
|
|
const page = pageParam(query.page)
|
||
|
|
const category = typeof query.category === 'string' ? query.category : null
|
||
|
|
|
||
|
|
const filter = category
|
||
|
|
? `&filter=${encodeURIComponent(`category.slug = ${quote(category)}`)}`
|
||
|
|
: ''
|
||
|
|
|
||
|
|
const response = await pbFetch<PbList<RawArticleSummary>>(
|
||
|
|
`/api/collections/articles/records?fields=${ARTICLE_SUMMARY_FIELDS}&expand=category&sort=-publishedAt&${pagination(page)}${filter}`
|
||
|
|
)
|
||
|
|
|
||
|
|
return {
|
||
|
|
items: response.items.map(toSummary),
|
||
|
|
page: response.page,
|
||
|
|
pageCount: response.totalPages,
|
||
|
|
total: response.totalItems,
|
||
|
|
}
|
||
|
|
})
|