diff --git a/packages/website-ue/src/pages/[...locale]/api/index.astro b/packages/website-ue/src/pages/[...locale]/api/index.astro index f263df023..c55f02f0f 100644 --- a/packages/website-ue/src/pages/[...locale]/api/index.astro +++ b/packages/website-ue/src/pages/[...locale]/api/index.astro @@ -1,5 +1,11 @@ --- import "@radix-ui/colors/amber-dark.css"; +import { + createProgram, + displayPartsToString, + forEachChild, + isFunctionDeclaration, +} from "typescript"; import BaseLayout from "../../../layouts/BaseLayout.astro"; export { getStaticPaths } from "../index.astro"; @@ -15,38 +21,26 @@ const paths: { 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 }); -// } - for (const fullPath in globbed) { - if (!fullPath.endsWith(".ts")) continue; - - const mdPath = fullPath.replace(".ts", ".md"); - const documentation = globbed[mdPath] as string; - - if (!documentation) continue; - + 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 }); } diff --git a/packages/website-ue/src/pages/api/backgrounds/[id].webp.ts b/packages/website-ue/src/pages/api/backgrounds/[id].webp.ts index 02de949f6..d601c7be3 100644 --- a/packages/website-ue/src/pages/api/backgrounds/[id].webp.ts +++ b/packages/website-ue/src/pages/api/backgrounds/[id].webp.ts @@ -4,9 +4,6 @@ import { imageProxy } from "../../../core/api/imageProxy"; export { getStaticPaths } from "./[id].json"; -/** - * One day I will write documentation for this - */ export async function GET({ params }: APIContext) { const background = await api.background(params.id); return await imageProxy(background.profile_background!.background); diff --git a/packages/website-ue/src/pages/api/enums/[enum].json.ts b/packages/website-ue/src/pages/api/enums/[enum].json.ts index 1fcf326eb..29e582754 100644 --- a/packages/website-ue/src/pages/api/enums/[enum].json.ts +++ b/packages/website-ue/src/pages/api/enums/[enum].json.ts @@ -2,8 +2,9 @@ import { TankClass, TankType } from "@blitzkit/core"; import { Nation } from "@protos/blitz_static_tank_component"; import type { APIContext, GetStaticPaths } from "astro"; import { GunType } from "../../../core/tankopedia/characteristics"; +import { GET as _GET } from "./all.json"; -const enums = { +export const ENUMS = { nation: Nation, "tank-class": TankClass, "tank-type": TankType, @@ -11,29 +12,12 @@ const enums = { }; export const getStaticPaths = (() => { - return Object.keys(enums).map((name) => ({ params: { enum: name } })); + return Object.keys(ENUMS).map((name) => ({ params: { enum: name } })); }) satisfies GetStaticPaths; -/** - * Enumerations used within BlitzKit, for nominal development reference only. - * - * DO NOT programmatically use this API as it is designed solely for human - * reference. - * - * DO NOT blindly copy entries as naming conventions vary. Most enums come - * directly from the game which uses a SCREAMING_SNAKE_CASE convention while - * BlitzKit uses PascalCase. - */ -export function GET({ +export async function GET({ params, -}: APIContext) { - const map: Record = {}; - - for (const key in enums[params.enum]) { - const value = enums[params.enum][key]; - if (typeof value !== "string") continue; - map[key] = value; - } - - return Response.json(map); +}: APIContext) { + const enums = (await _GET().json()) as Record>; + return Response.json(enums[params.enum]); } diff --git a/packages/website-ue/src/pages/api/enums/all.json.ts b/packages/website-ue/src/pages/api/enums/all.json.ts new file mode 100644 index 000000000..c08f140e6 --- /dev/null +++ b/packages/website-ue/src/pages/api/enums/all.json.ts @@ -0,0 +1,20 @@ +import { ENUMS } from "./[enum].json"; + +export function GET() { + const enums: Record> = {}; + + for (const name in ENUMS) { + const _enum = ENUMS[name as keyof typeof ENUMS]; + const map: Record = {}; + + for (const key in _enum) { + const value = _enum[key]; + if (typeof value !== "string") continue; + map[key] = value; + } + + enums[name] = map; + } + + return Response.json(enums); +} diff --git a/packages/website-ue/src/pages/api/enums/list.json.ts b/packages/website-ue/src/pages/api/enums/list.json.ts new file mode 100644 index 000000000..5af2a3fb1 --- /dev/null +++ b/packages/website-ue/src/pages/api/enums/list.json.ts @@ -0,0 +1,6 @@ +import { ENUMS } from "./[enum].json"; + +export function GET() { + const list = Object.keys(ENUMS); + return Response.json(list); +}