From 50f0779a8ef7cbcd6de20f86f68bab2a4bc37987 Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Wed, 12 Nov 2025 19:49:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E8=AF=BB=E5=8F=96GraphQL=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将GraphQL移至server/assets,在构建后会被写入server中 - 在server端通过storage读取数据 --- server/api/cms/companyProfile.ts | 12 ++++++---- server/api/cms/contactInfo.ts | 12 ++++++---- server/api/cms/documentList.ts | 12 ++++++---- server/api/cms/homepage.ts | 11 ++++++--- server/api/cms/product/[id].ts | 11 ++++++--- server/api/cms/productList.ts | 12 ++++++---- server/api/cms/questionList.ts | 12 ++++++---- server/api/cms/solution/[id].ts | 11 ++++++--- server/api/cms/solutionList.ts | 12 ++++++---- .../assets/graphql}/companyProfile.graphql | 0 .../assets/graphql}/contactInfo.graphql | 0 .../assets/graphql}/documentList.graphql | 0 .../assets/graphql}/homepage.graphql | 0 .../assets/graphql}/product.graphql | 0 .../assets/graphql}/productList.graphql | 0 .../assets/graphql}/questionList.graphql | 0 .../assets/graphql}/solution.graphql | 0 .../assets/graphql}/solutionList.graphql | 0 server/utils/serverAssets.ts | 24 +++++++++++++++++++ 19 files changed, 90 insertions(+), 39 deletions(-) rename {graphql => server/assets/graphql}/companyProfile.graphql (100%) rename {graphql => server/assets/graphql}/contactInfo.graphql (100%) rename {graphql => server/assets/graphql}/documentList.graphql (100%) rename {graphql => server/assets/graphql}/homepage.graphql (100%) rename {graphql => server/assets/graphql}/product.graphql (100%) rename {graphql => server/assets/graphql}/productList.graphql (100%) rename {graphql => server/assets/graphql}/questionList.graphql (100%) rename {graphql => server/assets/graphql}/solution.graphql (100%) rename {graphql => server/assets/graphql}/solutionList.graphql (100%) create mode 100644 server/utils/serverAssets.ts diff --git a/server/api/cms/companyProfile.ts b/server/api/cms/companyProfile.ts index 03fd886..d624a24 100644 --- a/server/api/cms/companyProfile.ts +++ b/server/api/cms/companyProfile.ts @@ -1,12 +1,14 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { - const query = readFileSync( - path.resolve('graphql/companyProfile.graphql'), - 'utf-8' + const query = await loadAssetAsString( + 'assets:server', + 'graphql/companyProfile.graphql' ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ company_profile: CompanyProfile }>( diff --git a/server/api/cms/contactInfo.ts b/server/api/cms/contactInfo.ts index 37a3f06..7f71f65 100644 --- a/server/api/cms/contactInfo.ts +++ b/server/api/cms/contactInfo.ts @@ -1,12 +1,14 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { - const query = readFileSync( - path.resolve('graphql/contactInfo.graphql'), - 'utf-8' + const query = await loadAssetAsString( + 'assets:server', + 'graphql/contactInfo.graphql' ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ contact_info: ContactInfo }>(query, { diff --git a/server/api/cms/documentList.ts b/server/api/cms/documentList.ts index 5c426ba..9c6bf02 100644 --- a/server/api/cms/documentList.ts +++ b/server/api/cms/documentList.ts @@ -1,12 +1,14 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { - const query = readFileSync( - path.resolve('graphql/documentList.graphql'), - 'utf-8' + const query = await loadAssetAsString( + 'assets:server', + 'graphql/documentList.graphql' ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ product_documents: ProductDocument[] }>( diff --git a/server/api/cms/homepage.ts b/server/api/cms/homepage.ts index a65a36b..6cce426 100644 --- a/server/api/cms/homepage.ts +++ b/server/api/cms/homepage.ts @@ -1,9 +1,14 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { - const query = readFileSync(path.resolve('graphql/homepage.graphql'), 'utf-8'); + const query = await loadAssetAsString( + 'assets:server', + 'graphql/homepage.graphql' + ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ homepage: Homepage }>(query, { diff --git a/server/api/cms/product/[id].ts b/server/api/cms/product/[id].ts index b53b14c..441d2b7 100644 --- a/server/api/cms/product/[id].ts +++ b/server/api/cms/product/[id].ts @@ -1,6 +1,5 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { const id = getRouterParam(event, 'id'); @@ -10,7 +9,13 @@ export default defineEventHandler(async (event) => { statusMessage: 'Product ID is required', }); - const query = readFileSync(path.resolve('graphql/product.graphql'), 'utf-8'); + const query = await loadAssetAsString( + 'assets/server', + 'graphql/product.graphql' + ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ products_by_id: Product }>(query, { diff --git a/server/api/cms/productList.ts b/server/api/cms/productList.ts index c3cabb5..0e4054d 100644 --- a/server/api/cms/productList.ts +++ b/server/api/cms/productList.ts @@ -1,12 +1,14 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { - const query = readFileSync( - path.resolve('graphql/productList.graphql'), - 'utf-8' + const query = await loadAssetAsString( + 'assets:server', + 'graphql/productList.graphql' ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ products: Product[] }>(query, { diff --git a/server/api/cms/questionList.ts b/server/api/cms/questionList.ts index 9ffa07c..e47b941 100644 --- a/server/api/cms/questionList.ts +++ b/server/api/cms/questionList.ts @@ -1,12 +1,14 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { - const query = readFileSync( - path.resolve('graphql/questionList.graphql'), - 'utf-8' + const query = await loadAssetAsString( + 'assets:server', + 'graphql/questionList.graphql' ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ questions: Question[] }>(query, { diff --git a/server/api/cms/solution/[id].ts b/server/api/cms/solution/[id].ts index 103a44d..9b8e742 100644 --- a/server/api/cms/solution/[id].ts +++ b/server/api/cms/solution/[id].ts @@ -1,6 +1,5 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { const id = getRouterParam(event, 'id'); @@ -10,7 +9,13 @@ export default defineEventHandler(async (event) => { statusMessage: 'Solution ID is required', }); - const query = readFileSync(path.resolve('graphql/solution.graphql'), 'utf-8'); + const query = await loadAssetAsString( + 'assets:server', + 'graphql/solution.graphql' + ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ solutions_by_id: Solution }>(query, { diff --git a/server/api/cms/solutionList.ts b/server/api/cms/solutionList.ts index a712640..ff36e0e 100644 --- a/server/api/cms/solutionList.ts +++ b/server/api/cms/solutionList.ts @@ -1,12 +1,14 @@ import { directus } from '~~/server/utils/directus'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; +import { loadAssetAsString } from '~~/server/utils/serverAssets'; export default defineEventHandler(async (event) => { - const query = readFileSync( - path.resolve('graphql/solutionList.graphql'), - 'utf-8' + const query = await loadAssetAsString( + 'assets:server', + 'graphql/solutionList.graphql' ); + if (!query) + throw createError({ statusCode: 500, message: 'GraphQL query not found' }); + const locale = getHeader(event, 'x-locale') || 'zh-CN'; const data = await directus.query<{ solutions: Solution[] }>(query, { diff --git a/graphql/companyProfile.graphql b/server/assets/graphql/companyProfile.graphql similarity index 100% rename from graphql/companyProfile.graphql rename to server/assets/graphql/companyProfile.graphql diff --git a/graphql/contactInfo.graphql b/server/assets/graphql/contactInfo.graphql similarity index 100% rename from graphql/contactInfo.graphql rename to server/assets/graphql/contactInfo.graphql diff --git a/graphql/documentList.graphql b/server/assets/graphql/documentList.graphql similarity index 100% rename from graphql/documentList.graphql rename to server/assets/graphql/documentList.graphql diff --git a/graphql/homepage.graphql b/server/assets/graphql/homepage.graphql similarity index 100% rename from graphql/homepage.graphql rename to server/assets/graphql/homepage.graphql diff --git a/graphql/product.graphql b/server/assets/graphql/product.graphql similarity index 100% rename from graphql/product.graphql rename to server/assets/graphql/product.graphql diff --git a/graphql/productList.graphql b/server/assets/graphql/productList.graphql similarity index 100% rename from graphql/productList.graphql rename to server/assets/graphql/productList.graphql diff --git a/graphql/questionList.graphql b/server/assets/graphql/questionList.graphql similarity index 100% rename from graphql/questionList.graphql rename to server/assets/graphql/questionList.graphql diff --git a/graphql/solution.graphql b/server/assets/graphql/solution.graphql similarity index 100% rename from graphql/solution.graphql rename to server/assets/graphql/solution.graphql diff --git a/graphql/solutionList.graphql b/server/assets/graphql/solutionList.graphql similarity index 100% rename from graphql/solutionList.graphql rename to server/assets/graphql/solutionList.graphql diff --git a/server/utils/serverAssets.ts b/server/utils/serverAssets.ts new file mode 100644 index 0000000..ba71dd2 --- /dev/null +++ b/server/utils/serverAssets.ts @@ -0,0 +1,24 @@ +/** + * 将存储中的资源作为字符串加载 + * + * @param storage - 存储名称 + * @param path - 资源路径 + * + * --- + * @example + * const content = await loadAssetAsString('myStorage', 'path/to/asset.txt'); + * // typeof content === 'string' + */ +export async function loadAssetAsString( + storage: string, + path: string +): Promise { + const data = await useStorage(storage).getItem(path); + if (data instanceof Uint8Array) { + return Buffer.from(data).toString('utf-8'); + } + if (typeof data === 'string') { + return data; + } + throw new Error(`Invalid asset type for ${path}`); +}