feat: api renderer

This commit is contained in:
TrèsAbhi committed 2025-12-19 17:50:59 -06:00
1 parent 428483cf4c
commit 164b7fa454
5 files changed
+79 -13

No files matched your search

@@ -0,0 +1,54 @@
---
import {
createProgram,
displayPartsToString,
forEachChild,
isFunctionDeclaration,
} from "typescript";
const root = `/src/pages/api`;
const globbed = import.meta.glob("/src/pages/api/**/*.ts", {
eager: true,
query: "?raw",
});
const paths: {
path: string;
documentation: string;
}[] = [];
for (const fullPath in globbed) {
const relativePath = `.${fullPath}`;
const path = fullPath.slice(root.length + 1, -3);
const program = createProgram([relativePath], {});
const source = program.getSourceFile(relativePath);
if (!source) throw new Error(`Could not find source for ${fullPath}`);
const checker = program.getTypeChecker();
const getter = forEachChild(source, (node) => {
if (!isFunctionDeclaration(node) || node.name?.text !== "GET") return;
return checker.getSymbolAtLocation(node.name);
});
const documentationComment = getter?.getDocumentationComment(checker);
if (!documentationComment) continue;
const documentation = displayPartsToString(documentationComment);
if (documentation.length === 0) continue;
paths.push({ path, documentation });
}
paths.sort((a, b) => a.path.localeCompare(b.path));
---
<h1>api</h1>
{
paths.map(({ path, documentation }) => (
<>
<h2>{path}</h2>
<p>{documentation}</p>
</>
))
}
@@ -8,6 +8,9 @@ export const getStaticPaths = (async () => {
return tanks.map(({ id }) => ({ params: { id } }));
}) satisfies GetStaticPaths;
/**
* Even Noah can't save these animals.
*/
export async function GET({ params }: APIContext<never, { id: string }>) {
const tank = await api.tank(params.id);
return new Response(TankCatalogComponent.encode(tank).finish());
@@ -1,5 +1,8 @@
import { characteristicsOrder } from "../../../core/tankopedia/characteristicsOrder";
/**
* The naked man fears no pickpocket.
*/
export function GET() {
return Response.json(characteristicsOrder);
}
+9 -1
View File
@@ -1 +1,9 @@
go to <a href="/tanks">/tanks</a>
---
import packageJson from "../../../../package.json";
---
<h1>blitzkit {packageJson.version}</h1>
<p><a href="/tanks">/tanks</a></p>
<p><a href="/api">/api</a></p>
+10 -12
View File
@@ -4,16 +4,14 @@ import { api } from "../../core/api/dynamic";
const tanksList = await api.tanks();
---
<h1>Tanks</h1>
<h1>tanks</h1>
<ul>
{
tanksList.tanks.map(({ id, slug }) => (
<li>
<a href={`/tanks/${slug}`}>
{slug} ({id})
</a>
</li>
))
}
</ul>
{
tanksList.tanks.map(({ id, slug }) => (
<p>
<a href={`/tanks/${slug}`}>
{slug} ({id})
</a>
</p>
))
}