68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import { listPublishedMedia } from '~/server/utils/news'
|
|
import { ok } from '~/server/utils/response'
|
|
import {
|
|
newsItems as seedNewsEn,
|
|
seedVideosEn,
|
|
type MediaPublicItem
|
|
} from '~/data/news-content'
|
|
import { newsItemsZh, seedVideosZh } from '~/data/locale/zh/news.zh'
|
|
import { isSiteLocale, type SiteLocale } from '~/types/locale'
|
|
|
|
function seedPublishedMedia(locale: SiteLocale) {
|
|
const useZh = locale === 'zh'
|
|
const articles = (useZh ? newsItemsZh : seedNewsEn).map(
|
|
(item): MediaPublicItem => ({
|
|
mediaType: 'article',
|
|
title: item.title,
|
|
summary: item.summary,
|
|
thumbnail: item.thumbnail,
|
|
thumbnailAlt: item.thumbnailAlt,
|
|
url: item.url
|
|
})
|
|
)
|
|
const videos = (useZh ? seedVideosZh : seedVideosEn).map(
|
|
(item): MediaPublicItem => ({
|
|
mediaType: 'video',
|
|
title: item.title,
|
|
summary: item.summary,
|
|
thumbnail: item.thumbnail,
|
|
thumbnailAlt: item.thumbnailAlt,
|
|
youtubeUrl: item.youtubeUrl,
|
|
tiktokUrl: item.tiktokUrl
|
|
})
|
|
)
|
|
const items = [...articles, ...videos]
|
|
return {
|
|
items,
|
|
articles: articles.map((item) => ({
|
|
title: item.title,
|
|
summary: item.summary,
|
|
thumbnail: item.thumbnail,
|
|
thumbnailAlt: item.thumbnailAlt,
|
|
url: item.url || ''
|
|
})),
|
|
videos: videos.map((item) => ({
|
|
title: item.title,
|
|
summary: item.summary,
|
|
thumbnail: item.thumbnail,
|
|
thumbnailAlt: item.thumbnailAlt,
|
|
youtubeUrl: item.youtubeUrl,
|
|
tiktokUrl: item.tiktokUrl
|
|
}))
|
|
}
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const query = getQuery(event)
|
|
const localeRaw = String(query.locale || 'en').trim()
|
|
const locale: SiteLocale = isSiteLocale(localeRaw) ? localeRaw : 'en'
|
|
|
|
try {
|
|
const media = await listPublishedMedia(locale)
|
|
return ok(media)
|
|
} catch (error) {
|
|
console.warn('[api/news] database unavailable, using seed media:', error)
|
|
return ok(seedPublishedMedia(locale))
|
|
}
|
|
})
|