buster/apps/server/src/api/v2/reports/[id]/GET.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-08-05 05:37:48 +08:00
import { getReport } from '@buster/database';
import type { GetReportIndividualResponse } from '@buster/server-shared/reports';
import { markdownToPlatejs } from '@buster/server-utils/report';
import { Hono } from 'hono';
import { HTTPException } from 'hono/http-exception';
2025-08-05 09:27:18 +08:00
import { standardErrorHandler } from '../../../../utils/response';
2025-08-05 05:37:48 +08:00
export async function getReportHandler(
reportId: string,
2025-08-05 05:37:48 +08:00
user: { id: string }
2025-08-03 11:48:15 +08:00
): Promise<GetReportIndividualResponse> {
2025-08-05 05:37:48 +08:00
const report = await getReport({ reportId, userId: user.id });
2025-08-08 05:09:49 +08:00
try {
const platejsResult = await markdownToPlatejs(report.content);
if (platejsResult.error) {
console.error('Error converting markdown to PlateJS:', platejsResult.error);
throw new HTTPException(500, {
message: 'Error converting markdown to PlateJS',
});
}
const content = platejsResult.elements ?? [];
const response: GetReportIndividualResponse = {
...report,
content,
};
2025-08-08 05:09:49 +08:00
return response;
} catch (error) {
console.error('Error converting markdown to PlateJS:', error);
throw new HTTPException(500, {
2025-08-08 05:09:49 +08:00
message: 'Error converting markdown',
});
}
}
2025-08-05 09:27:18 +08:00
const app = new Hono()
.get('/', async (c) => {
const reportId = c.req.param('id');
const user = c.get('busterUser');
2025-08-03 10:15:48 +08:00
2025-08-05 09:27:18 +08:00
if (!reportId) {
throw new HTTPException(404, { message: 'Report ID is required' });
}
2025-08-03 10:15:48 +08:00
2025-08-05 09:27:18 +08:00
const response: GetReportIndividualResponse = await getReportHandler(reportId, user);
return c.json(response);
})
.onError(standardErrorHandler);
export default app;