From 03fdbb5d96e15b7f274b3348639732bf2d404152 Mon Sep 17 00:00:00 2001 From: Md Afiq Iskandar Date: Thu, 24 Jul 2025 17:29:30 +0800 Subject: [PATCH] Refactor Documentation API Endpoints for Environment-Specific Paths - Updated the documentation API endpoints to dynamically set the documentation directory based on the NODE_ENV variable, improving compatibility between development and production environments. - Enhanced error handling for file retrieval to ensure proper responses when files are not found or invalid. - Standardized import statements for consistency across the documentation files. --- server/api/documentation/[file].get.js | 26 ++++++++++++++++---------- server/api/documentation/index.get.js | 7 ++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/server/api/documentation/[file].get.js b/server/api/documentation/[file].get.js index 3286dc7..e89773b 100644 --- a/server/api/documentation/[file].get.js +++ b/server/api/documentation/[file].get.js @@ -1,13 +1,19 @@ -import fs from 'fs' -import path from 'path' +import fs from "fs"; +import path from "path"; export default defineEventHandler(async (event) => { - const { file } = event.context.params - const docsDir = path.resolve(process.cwd(), 'content/documentation') - const filePath = path.join(docsDir, file) - if (!file.endsWith('.md') || !fs.existsSync(filePath)) { - return { error: 'File not found' } + const { file } = event.context.params; + let docsDir = ""; + if (process.env.NODE_ENV === "development") { + docsDir = path.resolve(process.cwd(), "content/documentation"); + } else { + docsDir = path.resolve(process.cwd(), "../content/documentation"); } - const content = fs.readFileSync(filePath, 'utf-8') - return { content } -}) \ No newline at end of file + + const filePath = path.join(docsDir, file); + if (!file.endsWith(".md") || !fs.existsSync(filePath)) { + return { error: "File not found" }; + } + const content = fs.readFileSync(filePath, "utf-8"); + return { content }; +}); diff --git a/server/api/documentation/index.get.js b/server/api/documentation/index.get.js index 0eb1591..af4ff30 100644 --- a/server/api/documentation/index.get.js +++ b/server/api/documentation/index.get.js @@ -2,7 +2,12 @@ import fs from 'fs' import path from 'path' export default defineEventHandler(async (event) => { - const docsDir = path.resolve(process.cwd(), 'content/documentation') + let docsDir = ""; + if (process.env.NODE_ENV === "development") { + docsDir = path.resolve(process.cwd(), "content/documentation"); + } else { + docsDir = path.resolve(process.cwd(), "../content/documentation"); + } const files = fs.readdirSync(docsDir) .filter(file => file.endsWith('.md')) return files