datacops-cms
Version:
A modern, extensible CMS built with Next.js and Prisma.
55 lines (49 loc) • 1.62 kB
text/typescript
import fs from "fs";
import { NextResponse } from "next/server";
import path from "path";
// Read all content types
function getContentTypes()
{
const dir = path.resolve(process.cwd(), "content-types");
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir)
.filter(f => f.endsWith(".json"))
.map(f => f.replace(/\.json$/, ""));
}
// Read permissions
function getPermissions()
{
const permPath = path.resolve(process.cwd(), "content/api-permissions.json");
if (!fs.existsSync(permPath)) return {};
return JSON.parse(fs.readFileSync(permPath, "utf-8"));
}
// Generate endpoints list
export async function GET()
{
const types = getContentTypes();
const permissions = getPermissions()
const endpoints = [
...types.flatMap(type => ([
{
path: `/api/content/${type}`,
type,
methods: ["GET", "POST"],
permissions: {
GET: permissions?.[type]?.GET ?? false,
POST: permissions?.[type]?.POST ?? false,
}
},
{
path: `/api/content/${type}/:id`,
type,
methods: ["GET", "PUT", "DELETE"],
permissions: {
GET: permissions?.[type]?.GET ?? false,
PUT: permissions?.[type]?.PUT ?? false,
DELETE: permissions?.[type]?.DELETE ?? false,
}
}
])),
];
return NextResponse.json(endpoints);
}