|
1 | 1 | import { isAddress } from "@happy.tech/common" |
2 | | -import type { Hono } from "hono" |
| 2 | +import type { Context } from "hono" |
3 | 3 | import { describeRoute } from "hono-openapi" |
4 | 4 | import { resolver } from "hono-openapi/zod" |
5 | 5 | import { validator } from "hono-openapi/zod" |
| 6 | +import type { BlankEnv } from "hono/types" |
6 | 7 | import { err } from "neverthrow" |
| 8 | +import type { Address } from "viem" |
7 | 9 | import { z } from "zod" |
8 | 10 | import { env } from "./env" |
9 | | -import type { CloudflareService } from "./services/cloudflare" |
10 | | -import type { FaucetService } from "./services/faucet" |
| 11 | +import { cloudflareService } from "./services/cloudflare" |
| 12 | +import { faucetService } from "./services/faucet" |
11 | 13 | import { makeResponse } from "./utils" |
12 | 14 |
|
| 15 | +type Request = { |
| 16 | + json: { |
| 17 | + address: Address |
| 18 | + cfToken: string |
| 19 | + } |
| 20 | +} |
| 21 | + |
13 | 22 | export const inputSchema = z.object({ |
14 | 23 | address: z.string().refine(isAddress), |
15 | 24 | cfToken: z.string(), |
16 | 25 | }) |
17 | 26 |
|
18 | 27 | export const outputSchema = z.object({ |
19 | 28 | success: z.boolean(), |
20 | | - message: z.string(), |
| 29 | + message: z.string().optional(), |
21 | 30 | }) |
22 | 31 |
|
23 | 32 | export type FaucetInput = z.infer<typeof inputSchema> |
24 | 33 | export type FaucetOutput = z.infer<typeof outputSchema> |
25 | 34 |
|
26 | | -export const setupFaucetRoutes = (app: Hono, faucetService: FaucetService, cloudflareService: CloudflareService) => { |
27 | | - const validation = validator("json", inputSchema) |
28 | | - const description = describeRoute({ |
29 | | - description: "Faucet endpoint with Cloudflare Turnstile validation", |
30 | | - responses: { |
31 | | - 200: { |
32 | | - description: "Tokens sent", |
33 | | - content: { "application/json": { schema: resolver(outputSchema) } }, |
34 | | - }, |
35 | | - 403: { |
36 | | - description: "Turnstile verification failed", |
37 | | - content: { "application/json": { schema: resolver(outputSchema) } }, |
38 | | - }, |
39 | | - 429: { |
40 | | - description: "Rate limit exceeded", |
41 | | - content: { "application/json": { schema: resolver(outputSchema) } }, |
42 | | - }, |
43 | | - }, |
44 | | - validateResponse: env.NODE_ENV !== "production", |
45 | | - }) |
46 | | - |
47 | | - app.post("/faucet", validation, description, async (c) => { |
48 | | - try { |
49 | | - const { address, cfToken } = c.req.valid("json") |
| 35 | +export const validation = validator("json", inputSchema) |
50 | 36 |
|
51 | | - const captchaResult = await cloudflareService.verifyTurnstile(cfToken) |
| 37 | +export const description = describeRoute({ |
| 38 | + description: "Faucet endpoint with Cloudflare Turnstile validation", |
| 39 | + responses: { |
| 40 | + 200: { |
| 41 | + description: "Tokens sent", |
| 42 | + content: { "application/json": { schema: resolver(outputSchema) } }, |
| 43 | + }, |
| 44 | + 403: { |
| 45 | + description: "Turnstile verification failed", |
| 46 | + content: { "application/json": { schema: resolver(outputSchema) } }, |
| 47 | + }, |
| 48 | + 429: { |
| 49 | + description: "Rate limit exceeded", |
| 50 | + content: { "application/json": { schema: resolver(outputSchema) } }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + validateResponse: env.NODE_ENV !== "production", |
| 54 | +}) |
52 | 55 |
|
53 | | - if (captchaResult.isErr()) { |
54 | | - const response = makeResponse(captchaResult) |
55 | | - return c.json(response[0], response[1]) |
56 | | - } |
| 56 | +export const faucetHandler = async (c: Context<BlankEnv, "/faucet", { in: Request; out: Request }>) => { |
| 57 | + try { |
| 58 | + const { address, cfToken } = c.req.valid("json") |
57 | 59 |
|
58 | | - const result = await faucetService.sendTokens(address) |
| 60 | + const captchaResult = await cloudflareService.verifyTurnstile(cfToken) |
59 | 61 |
|
60 | | - const response = makeResponse(result) |
61 | | - return c.json(response[0], response[1]) |
62 | | - } catch (error) { |
63 | | - const response = makeResponse(err(error)) |
| 62 | + if (captchaResult.isErr()) { |
| 63 | + const response = makeResponse(captchaResult) |
64 | 64 | return c.json(response[0], response[1]) |
65 | 65 | } |
66 | | - }) |
| 66 | + |
| 67 | + const result = await faucetService.sendTokens(address) |
| 68 | + |
| 69 | + const response = makeResponse(result) |
| 70 | + return c.json(response[0], response[1]) |
| 71 | + } catch (error) { |
| 72 | + const response = makeResponse(err(error)) |
| 73 | + return c.json(response[0], response[1]) |
| 74 | + } |
67 | 75 | } |
0 commit comments