lastwebnovel-app/server/api/chapters/[novelId].ts
2026-04-11 22:55:16 +02:00

25 lines
720 B
TypeScript

import type { Chapter } from '~/types'
export default defineEventHandler(async (event): Promise<Chapter[]> => {
const novelId = getRouterParam(event, 'novelId')
const chapters = await queryCollection('content').all() as Chapter[]
const novelChapters = chapters
.filter(ch => ch._path?.includes(`/novels/${novelId}/ch-`))
.sort((a, b) => {
const aNum = parseInt(a._path?.match(/ch-(\d+)/)?.[1] || '0') || 0
const bNum = parseInt(b._path?.match(/ch-(\d+)/)?.[1] || '0') || 0
return aNum - bNum
}) as Chapter[]
if (!novelChapters.length) {
throw createError({
statusCode: 404,
statusMessage: 'No chapters found for this novel'
})
}
return novelChapters
})