chore: wiki: automatic list of all api endpoints
This commit is contained in:
1 parent
7664d058df
commit
3fc0697db8
11 files changed
+204
-38
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { getSongbooks, addSongbook } from "@/lib/db/managing";
|
import { getSongbooks, addSongbook } from "@/lib/db/managing";
|
||||||
|
|
||||||
|
// @wikiDescription: Liste aller Liederbücher abrufen
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const songbooks = await getSongbooks();
|
const songbooks = await getSongbooks();
|
||||||
@@ -10,6 +10,8 @@ export async function GET() {
|
|||||||
return NextResponse.json({ success: false, message: error}, { status: 500})
|
return NextResponse.json({ success: false, message: error}, { status: 500})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @wikiDescription: Neues Liederbuch erstellen
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const { name, language } = await request.json();
|
const { name, language } = await request.json();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { getSongInfos } from "@/lib/db/managing";
|
import { getSongInfos } from "@/lib/db/managing";
|
||||||
|
|
||||||
|
// @wikiDescription: Informationen eines Liedes abrufen
|
||||||
export async function GET(
|
export async function GET(
|
||||||
request: Request,
|
request: Request,
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
{ params }: { params: Promise<{ id: string }> }
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { attachSongToSongbook } from "@/lib/db/managing";
|
import { attachSongToSongbook } from "@/lib/db/managing";
|
||||||
|
|
||||||
|
// @wikiDescription: Lied zu einem Liederbuch hinzufügen
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { removeSongfromSongbook } from "@/lib/db/managing";
|
import { removeSongfromSongbook } from "@/lib/db/managing";
|
||||||
|
|
||||||
|
// @wikiDescription: Lied von einem Liederbuch entfernen
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
|
|||||||
|
|
||||||
import { getSongs, addSongToSongbook } from "@/lib/db/managing";
|
import { getSongs, addSongToSongbook } from "@/lib/db/managing";
|
||||||
|
|
||||||
|
// @wikiDescription: Liste aller Lieder abrufen
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
const songs = await getSongs();
|
const songs = await getSongs();
|
||||||
if (songs) {
|
if (songs) {
|
||||||
@@ -10,6 +11,8 @@ export async function GET() {
|
|||||||
return NextResponse.json({ success: false}, { status: 500})
|
return NextResponse.json({ success: false}, { status: 500})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @wikiDescription: Lied in einem Liederbuch erstellen
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const { title, songbookId, number } = await request.json();
|
const { title, songbookId, number } = await request.json();
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ let currentSong = {
|
|||||||
title: ""
|
title: ""
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// @wikiDescription: Akutell gestreamtes Lied abrufen
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
return NextResponse.json(currentSong)
|
return NextResponse.json(currentSong)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @wikiDescription: Lied zum Streamen festlegen
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const data = await request.json();
|
const data = await request.json();
|
||||||
|
|||||||
+27
-34
@@ -1,41 +1,34 @@
|
|||||||
import Article from "@/components/wiki/Article";
|
import { getAllApis } from "@/lib/wiki";
|
||||||
import { API, Callout } from "@/components/mdx-components"
|
import Article from '@/components/wiki/Article';
|
||||||
|
import { API } from '@/components/mdx-components';
|
||||||
|
|
||||||
|
export default async function Page() {
|
||||||
|
const allApis = await getAllApis();
|
||||||
|
|
||||||
|
// Gruppiert die APIs nach ihrer Kategorie
|
||||||
|
const groupedApis = Object.groupBy(allApis, (api) => api.category);
|
||||||
|
|
||||||
export default function Page() {
|
|
||||||
return (
|
return (
|
||||||
<Article title="API-Endpunkte">
|
<Article title="API-Endpunkte">
|
||||||
<Callout type="info">
|
{Object.entries(groupedApis).map(([category, apis]) => (
|
||||||
Es ist weder eine Authentifikation noch ein API-Key erforderlich.
|
<div key={category} className="mb-10">
|
||||||
</Callout>
|
{/* Kategorie-Überschrift wird nur EINMAL pro Kategorie gerendert */}
|
||||||
|
<h2 className="text-2xl font-bold text-white mt-10 mb-4 border-b border-zinc-800 pb-2">
|
||||||
|
{category}
|
||||||
|
</h2>
|
||||||
|
|
||||||
<h2>Stream</h2>
|
{/* Routen innerhalb dieser Kategorie durchgehen */}
|
||||||
|
{apis?.map((api, index) => (
|
||||||
<API method="GET" route="stream/currentID">
|
<div key={index} className="my-4">
|
||||||
Jetziges angezeigtes Lied abrufen
|
{api.methods.map((method, mIndex) => (
|
||||||
</API>
|
<API route={api.route} method={method.method} key={mIndex}>
|
||||||
<API method="POST" route="stream/currentID">
|
{method.description}
|
||||||
Lied zum Anzeigen festlegen
|
|
||||||
</API>
|
|
||||||
|
|
||||||
<h2>Lieder</h2>
|
|
||||||
|
|
||||||
<API method="GET" route="songs/[id]">
|
|
||||||
Informationen eines Liedes abrufen
|
|
||||||
</API>
|
|
||||||
<API method="POST" route="songs/attach">
|
|
||||||
Lied zu einem Liederbuch hinzufügen
|
|
||||||
</API>
|
|
||||||
<API method="POST" route="songs/attach">
|
|
||||||
Lied von einem Liederbuch entfernen
|
|
||||||
</API>
|
|
||||||
|
|
||||||
<h2>Liederbücher</h2>
|
|
||||||
<API method="GET" route="songbooks">
|
|
||||||
Liste aller Liederbücher abrufen
|
|
||||||
</API>
|
|
||||||
<API method="POST" route="songbooks">
|
|
||||||
Neues Liederbuch erstellen
|
|
||||||
</API>
|
</API>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
</Article>
|
</Article>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
+154
-1
@@ -1,6 +1,8 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import matter from 'gray-matter';
|
import matter from 'gray-matter';
|
||||||
|
import ts from 'typescript';
|
||||||
|
import { ApiRoute, ApiMethod } from '@/types/api';
|
||||||
|
|
||||||
export type WikiArticle = {
|
export type WikiArticle = {
|
||||||
slug: string;
|
slug: string;
|
||||||
@@ -16,7 +18,6 @@ export async function getWikiPageBySlug(slug: string): Promise<WikiArticle | nul
|
|||||||
const fullPath = path.join(wikiDirectory, `${slug}.mdx`);
|
const fullPath = path.join(wikiDirectory, `${slug}.mdx`);
|
||||||
const fileContents = fs.readFileSync(fullPath, "utf-8");
|
const fileContents = fs.readFileSync(fullPath, "utf-8");
|
||||||
|
|
||||||
// Metadaten und Inhalt parsen
|
|
||||||
const { data, content } = matter(fileContents);
|
const { data, content } = matter(fileContents);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -30,3 +31,155 @@ export async function getWikiPageBySlug(slug: string): Promise<WikiArticle | nul
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getAllTsFiles(dirPath: string): string[] {
|
||||||
|
let results: string[] = [];
|
||||||
|
|
||||||
|
if (!fs.existsSync(dirPath)) return results;
|
||||||
|
|
||||||
|
const list = fs.readdirSync(dirPath);
|
||||||
|
|
||||||
|
list.forEach((file) => {
|
||||||
|
const filePath = path.join(dirPath, file);
|
||||||
|
const stat = fs.statSync(filePath);
|
||||||
|
|
||||||
|
if (stat && stat.isDirectory()) {
|
||||||
|
results = results.concat(getAllTsFiles(filePath));
|
||||||
|
} else if (filePath.endsWith('.ts') && !filePath.endsWith('.d.ts')) {
|
||||||
|
results.push(filePath);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liest die Kategorie aus der Datei aus (@wikiCategory) oder ermittelt sie aus dem Ordnernamen.
|
||||||
|
*/
|
||||||
|
export function filterCategory(
|
||||||
|
content: string,
|
||||||
|
filePath: string,
|
||||||
|
prefix: string = '@wikiCategory'
|
||||||
|
): string {
|
||||||
|
// 1. In Kommentaren nach @wikiCategory suchen
|
||||||
|
const lines = content.split('\n');
|
||||||
|
for (const line of lines) {
|
||||||
|
const cleaned = line.replace(/^\/\/\s*/, '').replace(/^\/\*\*?\s*/, '').replace(/\*\/$/, '').trim();
|
||||||
|
if (cleaned.startsWith(prefix)) {
|
||||||
|
let category = cleaned.slice(prefix.length).trim().replace(/^:\s*/, '');
|
||||||
|
if (category) return category;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Fallback: Ersten Ordnernamen unter app/api/ als Kategorie nutzen
|
||||||
|
const normalizedPath = filePath.replace(/\\/g, '/');
|
||||||
|
const apiMatch = normalizedPath.match(/app\/api\/([^/]+)/);
|
||||||
|
if (apiMatch && apiMatch[1]) {
|
||||||
|
const folderName = apiMatch[1];
|
||||||
|
// Ersten Buchstaben großschreiben (z. B. "stream" -> "Stream")
|
||||||
|
return folderName.charAt(0).toUpperCase() + folderName.slice(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'Allgemein';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parst TypeScript-Code via AST und extrahiert HTTP-Methoden sowie deren Kommentare.
|
||||||
|
*/
|
||||||
|
export function filterMethods(
|
||||||
|
content: string,
|
||||||
|
prefix: string = '@wikiDescription'
|
||||||
|
): ApiMethod[] {
|
||||||
|
const sourceFile = ts.createSourceFile(
|
||||||
|
'temp.ts',
|
||||||
|
content,
|
||||||
|
ts.ScriptTarget.Latest,
|
||||||
|
/* setParentNodes */ true
|
||||||
|
);
|
||||||
|
|
||||||
|
const methods: ApiMethod[] = [];
|
||||||
|
const validMethods = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']);
|
||||||
|
|
||||||
|
ts.forEachChild(sourceFile, (node) => {
|
||||||
|
if (ts.isFunctionDeclaration(node) && node.name) {
|
||||||
|
const functionName = node.name.text;
|
||||||
|
|
||||||
|
if (validMethods.has(functionName)) {
|
||||||
|
let description = '';
|
||||||
|
|
||||||
|
const commentRanges = ts.getLeadingCommentRanges(content, node.pos);
|
||||||
|
|
||||||
|
if (commentRanges && commentRanges.length > 0) {
|
||||||
|
const lastCommentRange = commentRanges[commentRanges.length - 1];
|
||||||
|
const rawComment = content.substring(
|
||||||
|
lastCommentRange.pos,
|
||||||
|
lastCommentRange.end
|
||||||
|
);
|
||||||
|
|
||||||
|
const cleanedComment = rawComment
|
||||||
|
.replace(/^\/\/\s*/, '')
|
||||||
|
.replace(/^\/\*\*?\s*/, '')
|
||||||
|
.replace(/\*\/$/, '')
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => line.replace(/^\s*\*\s?/, '').trim())
|
||||||
|
.join(' ')
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
if (cleanedComment.startsWith(prefix)) {
|
||||||
|
let textAfterPrefix = cleanedComment.slice(prefix.length).trim();
|
||||||
|
textAfterPrefix = textAfterPrefix.replace(/^:\s*/, '');
|
||||||
|
description = textAfterPrefix;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
methods.push({
|
||||||
|
method: functionName,
|
||||||
|
description: description || 'Keine Beschreibung vorhanden.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return methods;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liest alle API-Dateien dynamisch aus und wandelt sie in ApiRoute-Objekte um.
|
||||||
|
*/
|
||||||
|
export async function getAllApis(): Promise<ApiRoute[]> {
|
||||||
|
try {
|
||||||
|
const apiBasePath = path.join(process.cwd(), "app/api");
|
||||||
|
const allTsFiles = getAllTsFiles(apiBasePath);
|
||||||
|
const routes: ApiRoute[] = [];
|
||||||
|
|
||||||
|
allTsFiles.forEach((filePath) => {
|
||||||
|
const fileContents = fs.readFileSync(filePath, "utf-8");
|
||||||
|
|
||||||
|
// 1. Methoden extrahieren
|
||||||
|
const methods = filterMethods(fileContents, "@wikiDescription");
|
||||||
|
|
||||||
|
// Nur Routen hinzufügen, die auch API-Methoden exportieren
|
||||||
|
if (methods.length > 0) {
|
||||||
|
// 2. Kategorie ermitteln
|
||||||
|
const category = filterCategory(fileContents, filePath, "@wikiCategory");
|
||||||
|
|
||||||
|
// 3. Route-Pfad automatisch aus dem Dateipfad generieren (app/api/stream/currentID/route.ts -> stream/currentID)
|
||||||
|
const relativePath = path.relative(apiBasePath, filePath).replace(/\\/g, '/');
|
||||||
|
const routePath = relativePath
|
||||||
|
.replace(/\/route\.ts$/, '')
|
||||||
|
.replace(/\.ts$/, '');
|
||||||
|
|
||||||
|
routes.push({
|
||||||
|
route: routePath,
|
||||||
|
category,
|
||||||
|
methods,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return routes;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Fehler beim Auslesen der API-Dateien:", error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -38,7 +38,7 @@
|
|||||||
"eslint": "^9",
|
"eslint": "^9",
|
||||||
"eslint-config-next": "16.2.9",
|
"eslint-config-next": "16.2.9",
|
||||||
"tailwindcss": "^4",
|
"tailwindcss": "^4",
|
||||||
"typescript": "^5"
|
"typescript": "^5.9.3"
|
||||||
},
|
},
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"onlyBuiltDependencies": [
|
"onlyBuiltDependencies": [
|
||||||
|
|||||||
Generated
+1
-1
@@ -91,7 +91,7 @@ importers:
|
|||||||
specifier: ^4
|
specifier: ^4
|
||||||
version: 4.3.1
|
version: 4.3.1
|
||||||
typescript:
|
typescript:
|
||||||
specifier: ^5
|
specifier: ^5.9.3
|
||||||
version: 5.9.3
|
version: 5.9.3
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
export interface ApiMethod {
|
||||||
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | string;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApiRoute {
|
||||||
|
category: string;
|
||||||
|
route: string;
|
||||||
|
methods: ApiMethod[];
|
||||||
|
}
|
||||||
Reference in new issue
Block a user