diff options
| author | seth <[email protected]> | 2024-07-29 04:53:25 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-07-29 04:53:25 -0400 |
| commit | 984f4cfa24ee7e421bb1fbdf5907ae60375cf9ef (patch) | |
| tree | f23c500548bdc82dfe4eb65233327312435ec6b6 /src/index.ts | |
| parent | 9358cb6437bbe257717fe6cfd113aa9c34054c6c (diff) | |
use github contents api for image urls + summer cleaning (#253)
* nix: alejandra -> nixfmt-rfc-style
* nix: pre-commit-hooks -> treefmt-nix
* nix: use corepack
* ci: cleanup workflows
* ci: use better dependabot scopes
* gitignore: extend with github templates
* remove teawie-archive submodule
* pnpm: 8.8.0 -> 9.6.0
* nix: add nrr to shell
* nix: add node lsps to shell
* use github contents api for image urls
* ci: cleanup workflows
* nix: add ci shell
* `octokit` -> `fetch` & cache responses
* nix: use nixpkgs wrangler
* nix: update flake.lock
Flake lock file updates:
• Updated input 'nixpkgs':
'github:NixOS/nixpkgs/d9c0b9d611277e42e6db055636ba0409c59db6d2' (2024-07-05)
→ 'github:NixOS/nixpkgs/038fb464fcfa79b4f08131b07f2d8c9a6bcc4160' (2024-07-28)
* tsconfig: use strictest
* adopt openapi
* package.json: rename to teawie-api
* nix: add treefmt to ci shell
* ci: add release gate
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 103 |
1 files changed, 75 insertions, 28 deletions
diff --git a/src/index.ts b/src/index.ts index 3126b15..9f2703b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,45 +1,92 @@ -import { Hono } from "hono"; import { logger } from "hono/logger"; import { prettyJSON } from "hono/pretty-json"; -import { zValidator } from "@hono/zod-validator"; -import { list } from "./schemas"; +import { OpenAPIHono, createRoute } from "@hono/zod-openapi"; +import { VERSION } from "./consts"; import { Bindings, Variables } from "./env"; +import { + ListTeawiesParams, + ListTeawiesResponse, + RandomTeawiesResponse, +} from "./schemas"; +import { imageUrls } from "./teawie"; -const app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); +const app = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>(); app.use("*", logger()); app.use("*", prettyJSON()); -app.get("/", (c) => { - return c.redirect( - c.env.REDIRECT_ROOT ?? "https://github.com/getchoo/teawieAPI", - ); -}); +app.get("/", (c) => + c.redirect(c.env.REDIRECT_ROOT ?? "https://github.com/getchoo/teawieAPI"), +); -app.get("/static/*", async (c) => { - return await c.env.ASSETS.fetch(c.req.raw); +app.doc("/doc", { + openapi: "3.0.0", + info: { + version: VERSION, + title: "teawieAPI", + }, }); -app.get("/list_teawies", zValidator("query", list), async (c) => { - const { limit } = c.req.query(); - - return c.json( - WIES.slice(0, parseInt(limit ?? "5")).map((wie) => { - return { - url: new URL(`/${WIE_DIR}/${wie}`, c.req.url).toString(), - }; - }), - ); -}); +app.openapi( + createRoute({ + method: "get", + path: "/list_teawies", + request: { + params: ListTeawiesParams, + }, + responses: { + 200: { + content: { + "application/json": { + schema: ListTeawiesResponse, + }, + }, + description: "List known Teawie URLS", + }, + }, + }), + async (c) => { + const { limit } = c.req.query(); + const urls = await imageUrls(c.env.TEAWIE_API); -app.get("/random_teawie", (c) => { - const wie = WIES[Math.floor(Math.random() * WIES.length)]; + return c.json( + { + urls: urls.splice(0, parseInt(limit ?? "5")), + }, + 200, + ); + }, +); - return c.json({ - url: new URL(`/${WIE_DIR}/${wie}`, c.req.url).toString(), - }); -}); +app.openapi( + createRoute({ + method: "get", + path: "/random_teawie", + responses: { + 200: { + content: { + "application/json": { + schema: RandomTeawiesResponse, + }, + }, + description: "A random URL to a picture of Teawie", + }, + }, + }), + async (c) => + imageUrls(c.env.TEAWIE_API).then((urls) => + c.json({ + url: urls[Math.floor(Math.random() * urls.length)], + }), + ), +); app.get("/get_random_teawie", (c) => c.redirect("/random_teawie")); +app.onError((error, c) => { + console.error(error); + + return c.json({ error: error.message }, 500); +}); + export default app; |
