diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/env.ts | 7 | ||||
| -rw-r--r-- | src/globals.d.ts | 1 | ||||
| -rw-r--r-- | src/index.ts | 44 | ||||
| -rw-r--r-- | src/schemas.ts | 12 |
4 files changed, 64 insertions, 0 deletions
diff --git a/src/env.ts b/src/env.ts new file mode 100644 index 0000000..a39f506 --- /dev/null +++ b/src/env.ts @@ -0,0 +1,7 @@ +export type Bindings = { + ASSETS: Fetcher; + URL?: string; + REDIRECT_ROOT?: string; +}; + +export type Variables = Record<string, never>; diff --git a/src/globals.d.ts b/src/globals.d.ts new file mode 100644 index 0000000..9b213f8 --- /dev/null +++ b/src/globals.d.ts @@ -0,0 +1 @@ +declare const WIES: string[]; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..55bd92a --- /dev/null +++ b/src/index.ts @@ -0,0 +1,44 @@ +import { Hono } from "hono"; +import { logger } from "hono/logger"; +import { prettyJSON } from "hono/pretty-json"; +import { secureHeaders } from "hono/secure-headers"; +import { zValidator } from "@hono/zod-validator"; +import { list } from "./schemas"; +import { Bindings, Variables } from "./env"; + +const app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); + +app.use("*", secureHeaders()); +app.use("*", logger()); +app.use("*", prettyJSON()); + +app.get("/", (c) => { + return c.redirect(c.env.REDIRECT_ROOT ?? "https://github.com/getchoo/teawieAPI"); +}); + +app.get("/static/teawie/*", async (c) => { + return await c.env.ASSETS.fetch(c.req.raw); +}); + +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: `${c.env.URL ?? "https://api.mydadleft.me"}/static/teawie/${wie}`, + }; + }), + ); +}); + +app.get("/random_teawie", async (c) => { + const wies = WIES; + const wie = wies[Math.floor(Math.random() * wies.length)]; + + return c.json({ + url: `${c.env.URL ?? "https://api.mydadleft.me"}/static/${wie}`, + }); +}); + +export default app; diff --git a/src/schemas.ts b/src/schemas.ts new file mode 100644 index 0000000..1b858ff --- /dev/null +++ b/src/schemas.ts @@ -0,0 +1,12 @@ +import { z } from "zod"; + +export const list = z.object({ + limit: z + .string() + .optional() + .default("5") + .refine((data) => { + const parsed = parseInt(data); + return !isNaN(parsed); + }), +}); |
