From 8d4933dd600e80bae3f6a6569e12041eba0325bf Mon Sep 17 00:00:00 2001 From: tresabhi <43380238+tresabhi@users.noreply.github.com> Date: Tue, 18 Mar 2025 23:53:52 -0500 Subject: [PATCH] feat: some more apis --- packages/core/src/blitzkit/index.ts | 1 + packages/core/src/blitzkit/tanksFull.ts | 7 ++ packages/core/src/protos/i18n.ts | 2 +- packages/core/src/protos/tank.ts | 2 +- packages/website/src/constants/tools.ts | 2 +- .../src/pages/[...locale]/api/index.astro | 98 ++++++++++--------- .../api/tankopedia/tank-icon/[id]/_index.tsx | 43 -------- .../api/tankopedia/tank-icon/[id]/index.astro | 30 ------ .../website/src/pages/api/tanks/[id].json.ts | 18 ++++ .../website/src/pages/api/tanks/full.json.ts | 13 +++ .../website/src/pages/api/tanks/list.json.ts | 12 +++ 11 files changed, 108 insertions(+), 120 deletions(-) create mode 100644 packages/core/src/blitzkit/tanksFull.ts delete mode 100644 packages/website/src/pages/api/tankopedia/tank-icon/[id]/_index.tsx delete mode 100644 packages/website/src/pages/api/tankopedia/tank-icon/[id]/index.astro create mode 100644 packages/website/src/pages/api/tanks/[id].json.ts create mode 100644 packages/website/src/pages/api/tanks/full.json.ts create mode 100644 packages/website/src/pages/api/tanks/list.json.ts diff --git a/packages/core/src/blitzkit/index.ts b/packages/core/src/blitzkit/index.ts index 6c69eefc2..052f1c41c 100644 --- a/packages/core/src/blitzkit/index.ts +++ b/packages/core/src/blitzkit/index.ts @@ -48,4 +48,5 @@ export * from './metaSortTank'; export * from './romanize'; export * from './tank'; export * from './tanks'; +export * from './tanksFull'; export * from './youtubers'; diff --git a/packages/core/src/blitzkit/tanksFull.ts b/packages/core/src/blitzkit/tanksFull.ts new file mode 100644 index 000000000..93435a313 --- /dev/null +++ b/packages/core/src/blitzkit/tanksFull.ts @@ -0,0 +1,7 @@ +import { fetchPB } from '../protobuf'; +import { TanksFull } from '../protos'; +import { asset } from './asset'; + +export function fetchTanksFull() { + return fetchPB(asset('definitions/tanks-full.pb'), TanksFull); +} diff --git a/packages/core/src/protos/i18n.ts b/packages/core/src/protos/i18n.ts index 56812cce5..27894724e 100644 --- a/packages/core/src/protos/i18n.ts +++ b/packages/core/src/protos/i18n.ts @@ -18,7 +18,7 @@ export interface I18n_LocalesEntry { value: string; } -function createBaseI18n(): I18n { +export function createBaseI18n(): I18n { return { locales: {} }; } diff --git a/packages/core/src/protos/tank.ts b/packages/core/src/protos/tank.ts index b5043f5e8..ff3b06502 100644 --- a/packages/core/src/protos/tank.ts +++ b/packages/core/src/protos/tank.ts @@ -6,7 +6,7 @@ /* eslint-disable */ import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire"; -import { I18n } from "./i18n"; +import { I18n, createBaseI18n } from "./i18n"; export const protobufPackage = "blitzkit"; diff --git a/packages/website/src/constants/tools.ts b/packages/website/src/constants/tools.ts index 6968b49d4..299bbd015 100644 --- a/packages/website/src/constants/tools.ts +++ b/packages/website/src/constants/tools.ts @@ -138,7 +138,7 @@ export const apiTool: Tool = { button: { color: 'indigo', }, - image: '0bWE5hC', + image: 'dIfvWnV', }; export const TOOLS: Tool[] = [ diff --git a/packages/website/src/pages/[...locale]/api/index.astro b/packages/website/src/pages/[...locale]/api/index.astro index b0c51b59c..74909f454 100644 --- a/packages/website/src/pages/[...locale]/api/index.astro +++ b/packages/website/src/pages/[...locale]/api/index.astro @@ -1,5 +1,5 @@ --- -import { Code, Flex, Heading, Link } from '@radix-ui/themes'; +import { Code, Flex, Heading, Separator } from '@radix-ui/themes'; import { PageWrapper } from '../../../components/PageWrapper'; import { apiTool } from '../../../constants/tools'; import ToolLayout from '../../../layouts/ToolLayout.astro'; @@ -12,7 +12,7 @@ interface Tag { description?: string; } -const getDocsRegex = /\/\*\*\n(( \*(.+)?\n?)+)\n \*\/\nexport const GET/gm; +const getDocsRegex = /\/\*\*\n(( \*(.+)?\n?)+)\n \*\/\nexport const GET/; const multiLineCommentPrefixRegex = /^ \* ?/gm; const tagRegex = /(@[^\s]+)(?:\s(.+))?/; const firstWordRegex = /(\S+)(?:\s(.+))?/; @@ -22,7 +22,7 @@ const glob = import.meta.glob('/src/pages/api/**/*.ts', { query: '?raw', import: 'default', }); -const apis: { +let apis: { path: string; content: string; comment: string; @@ -31,7 +31,7 @@ const apis: { for (const rawPath in glob) { const content = (glob[rawPath] as string).replaceAll('\r', ''); - const regexExecution = getDocsRegex.exec(content); + const regexExecution = content.match(getDocsRegex); const commentRaw = regexExecution?.[1]; if (!commentRaw) continue; @@ -45,7 +45,7 @@ for (const rawPath in glob) { const tags: Tag[] = []; commentWithTags.split('\n').forEach((line) => { - const match = tagRegex.exec(line); + const match = line.match(tagRegex); if (match) { const tag = match[1]; @@ -54,7 +54,7 @@ for (const rawPath in glob) { let description: string | undefined; if (tag === '@param') { - const firstWordMatch = firstWordRegex.exec(descriptionRaw)!; + const firstWordMatch = descriptionRaw.match(firstWordRegex)!; discriminator = firstWordMatch[1]; description = firstWordMatch[2]; } else { @@ -75,53 +75,63 @@ for (const rawPath in glob) { apis.push({ path, content, comment, tags }); } + +apis.sort((a, b) => a.path.localeCompare(b.path)); --- - { - apis.map((api) => ( - - - - {api.path} - - + + { + apis.map((api, index) => ( + <> + + + + {api.path} + + - - {api.comment} - + + {api.comment} + - {api.tags.length > 0 && ( - - {api.tags.map(({ tag, description, discriminator }) => ( - - - {tag} - + {api.tags.length > 0 && ( + + {api.tags.map(({ tag, description, discriminator }) => ( + + + {tag} + - {discriminator && ( - - {discriminator} - - )} + {discriminator && ( + + {discriminator} + + )} - {description && ( - - {description} - - )} + {description && ( + + {description} + + )} + + ))} - ))} + )} - )} - - )) - } + + {index < apis.length - 1 && ( + + )} + + )) + } + diff --git a/packages/website/src/pages/api/tankopedia/tank-icon/[id]/_index.tsx b/packages/website/src/pages/api/tankopedia/tank-icon/[id]/_index.tsx deleted file mode 100644 index 96a50aecc..000000000 --- a/packages/website/src/pages/api/tankopedia/tank-icon/[id]/_index.tsx +++ /dev/null @@ -1,43 +0,0 @@ -import { TankSandbox } from '../../../../../components/Tankopedia/HeroSection/components/TankSandbox'; -import { awaitableModelDefinitions } from '../../../../../core/awaitables/modelDefinitions'; -import { awaitableProvisionDefinitions } from '../../../../../core/awaitables/provisionDefinitions'; -import { awaitableTankDefinitions } from '../../../../../core/awaitables/tankDefinitions'; -import { LocaleProvider } from '../../../../../hooks/useLocale'; -import { App } from '../../../../../stores/app'; -import { Duel } from '../../../../../stores/duel'; -import { TankopediaEphemeral } from '../../../../../stores/tankopediaEphemeral'; -import { TankopediaPersistent } from '../../../../../stores/tankopediaPersistent'; - -const [provisionDefinitions, tankDefinitions, modelDefinitions] = - await Promise.all([ - awaitableProvisionDefinitions, - awaitableTankDefinitions, - awaitableModelDefinitions, - ]); - -interface PageProps { - id: number; -} - -export function Page({ id }: PageProps) { - const tank = tankDefinitions.tanks[id]; - const model = modelDefinitions.models[id]; - - return ( - - - - - - - - - - - - ); -} - -function Content() { - return ; -} diff --git a/packages/website/src/pages/api/tankopedia/tank-icon/[id]/index.astro b/packages/website/src/pages/api/tankopedia/tank-icon/[id]/index.astro deleted file mode 100644 index e8e8d414a..000000000 --- a/packages/website/src/pages/api/tankopedia/tank-icon/[id]/index.astro +++ /dev/null @@ -1,30 +0,0 @@ ---- -import { assertSecret } from '@blitzkit/core'; -import type { GetStaticPaths } from 'astro'; -import { getStaticPaths as tankopediaGetStaticPaths } from '../../../../[...locale]/tools/tankopedia/[id]/index.astro'; -import { Page } from './_index'; - -export const getStaticPaths = (async () => { - if (assertSecret(import.meta.env.MODE) === 'development') { - return await tankopediaGetStaticPaths(); - } else { - return []; - } -}) satisfies GetStaticPaths; - -const { id } = Astro.params; ---- - - - - - - - - - - diff --git a/packages/website/src/pages/api/tanks/[id].json.ts b/packages/website/src/pages/api/tanks/[id].json.ts new file mode 100644 index 000000000..044f0d360 --- /dev/null +++ b/packages/website/src/pages/api/tanks/[id].json.ts @@ -0,0 +1,18 @@ +import { fetchTank, fetchTanks } from '@blitzkit/core'; +import type { APIRoute, GetStaticPaths } from 'astro'; + +export const getStaticPaths = (async () => { + const tanks = await fetchTanks(); + return tanks.tanks.map((id) => ({ params: { id } })); +}) satisfies GetStaticPaths; + +/** + * Returns the meta data on a tank which includes its modules, characteristics, ancestry, descendants, equipment, characteristics, etc. However, this does not include information on the tank model. + * + * @param avatar The avatar id. + * @returns A webp image. + */ +export const GET: APIRoute<{}, { id: string }> = async ({ params }) => { + const tank = await fetchTank(params.id); + return Response.json(tank); +}; diff --git a/packages/website/src/pages/api/tanks/full.json.ts b/packages/website/src/pages/api/tanks/full.json.ts new file mode 100644 index 000000000..988602357 --- /dev/null +++ b/packages/website/src/pages/api/tanks/full.json.ts @@ -0,0 +1,13 @@ +import { fetchTanksFull } from '@blitzkit/core'; +import type { APIRoute } from 'astro'; + +/** + * Returns meta data similar to tanks/[id].json but for all tanks as an array. There is no guarantee of order. + * + * @warning This is a large response. Consider using tanks/[id].json for individual tanks. + * @returns JSON. + */ +export const GET: APIRoute = async () => { + const tanksFull = await fetchTanksFull(); + return Response.json(tanksFull.tanks); +}; diff --git a/packages/website/src/pages/api/tanks/list.json.ts b/packages/website/src/pages/api/tanks/list.json.ts new file mode 100644 index 000000000..129d5700e --- /dev/null +++ b/packages/website/src/pages/api/tanks/list.json.ts @@ -0,0 +1,12 @@ +import { fetchTanks } from '@blitzkit/core'; +import type { APIRoute } from 'astro'; + +/** + * Returns a list of all tank ids. There is no guarantee of order. + * + * @returns JSON. + */ +export const GET: APIRoute = async () => { + const tanks = await fetchTanks(); + return Response.json(tanks.tanks); +};