refactor: 将数据获取从app端移至server端
- 调整数据获取位置以提升安全性 - 对于后端状态为Archived的数据,通过server控制不进行获取
This commit is contained in:
28
server/api/cms/solution/[id].ts
Normal file
28
server/api/cms/solution/[id].ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, 'id');
|
||||
if (!id)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Solution ID is required',
|
||||
});
|
||||
|
||||
const query = readFileSync(path.resolve('graphql/solution.graphql'), 'utf-8');
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ solutions_by_id: Solution }>(query, {
|
||||
id: id,
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const solution = data?.solutions_by_id;
|
||||
|
||||
if (!solution || solution.status === 'archived') {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Solution not found' });
|
||||
}
|
||||
|
||||
return solution;
|
||||
});
|
||||
Reference in New Issue
Block a user