Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions app/api/page-content/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { NextRequest } from 'next/server';
import { readFile } from 'fs/promises';
import { join } from 'path';
import matter from 'gray-matter';
import docsJson from '@/content/docs.json';

interface Doc {
file: string;
meta: Record<string, unknown>;
content: string;
}

const docs = docsJson.docs as Doc[];
Comment on lines +2 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add runtime validation for the imported JSON structure.

The type assertion as Doc[] bypasses TypeScript's type checking, which could lead to runtime errors if docs.json doesn't match the expected structure.

Consider adding runtime validation:

-const docs = docsJson.docs as Doc[];
+const docs = Array.isArray(docsJson.docs) 
+  ? (docsJson.docs as Doc[])
+  : [];
+
+// Validate the structure exists
+if (docs.length === 0) {
+  console.warn('No docs found in docs.json - Copy Page feature may not work');
+}

Additionally, consider making the meta property more specific if you know the expected fields:

interface Doc {
  file: string;
-  meta: Record<string, unknown>;
+  meta: {
+    title?: string;
+    description?: string;
+    [key: string]: unknown;
+  };
  content: string;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import docsJson from '@/content/docs.json';
interface Doc {
file: string;
meta: Record<string, unknown>;
content: string;
}
const docs = docsJson.docs as Doc[];
import docsJson from '@/content/docs.json';
interface Doc {
file: string;
meta: {
title?: string;
description?: string;
[key: string]: unknown;
};
content: string;
}
const docs = Array.isArray(docsJson.docs)
? (docsJson.docs as Doc[])
: [];
// Validate the structure exists
if (docs.length === 0) {
console.warn('No docs found in docs.json - Copy Page feature may not work');
}
🤖 Prompt for AI Agents
In app/api/page-content/route.ts around lines 2 to 10, the imported JSON is cast
to Doc[] using a type assertion, which skips runtime validation and risks
runtime errors if the JSON structure is incorrect. To fix this, implement
runtime validation of the imported JSON data against the expected Doc interface,
for example by using a validation library or manual checks to confirm each
object has the required fields with correct types before assigning to docs.
Also, refine the meta property type from Record<string, unknown> to a more
specific interface if the expected fields are known, improving type safety.


export async function GET(request: NextRequest) {
try {
Expand All @@ -16,28 +22,20 @@ export async function GET(request: NextRequest) {
return new Response('Invalid path parameter', { status: 400 });
}

const basePath = join(process.cwd(), 'content');
const indexPath = join(basePath, path, 'index.mdx');
const directPath = join(basePath, `${path}.mdx`);
const doc = docs.find(d =>
d.file === `${path}.mdx` ||
d.file === `${path}/index.mdx` ||
d.file === path
);

let fileContent: string;

try {
fileContent = await readFile(indexPath, 'utf-8');
} catch {
try {
fileContent = await readFile(directPath, 'utf-8');
} catch {
return new Response('Page not found', { status: 404 });
}
if (!doc) {
return new Response('Page not found', { status: 404 });
}

const { content, data } = matter(fileContent);

return Response.json({
content,
title: data.title || '',
description: data.description || '',
content: doc.content,
title: doc.meta.title || '',
description: doc.meta.description || '',
path
});
} catch (error) {
Expand Down