From db939298d72fb4e543171f82d64d1ed66bb4a268 Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Wed, 7 Feb 2024 16:41:46 +1100 Subject: [PATCH] wip: more faffing --- example/.gitignore | 2 ++ example/src/index.tsx | 37 +++++++++++++++++++++++++++++++++++-- src/index.tsx | 41 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/example/.gitignore b/example/.gitignore index 3c3629e6..0192dfa7 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -1 +1,3 @@ node_modules + +.vercel diff --git a/example/src/index.tsx b/example/src/index.tsx index 76b6b096..feb34b36 100644 --- a/example/src/index.tsx +++ b/example/src/index.tsx @@ -6,9 +6,42 @@ import { Button, Framework } from "@wevm/framework"; const app = new Framework(); -app.frame("/", () => { +app.frame("/", ({ untrustedData }) => { + const { buttonIndex } = untrustedData || {}; return { - image:
hello
, + image: ( +
+
+ {typeof buttonIndex === "number" + ? `Button Index: ${buttonIndex}` + : "Welcome!"} +
+
+ ), intents: ( <> diff --git a/src/index.tsx b/src/index.tsx index 23fdb62f..06f3fddf 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,6 +2,20 @@ import { type Context, Hono } from "hono"; import { ImageResponse } from "hono-og"; import { type JSXNode } from "hono/jsx"; +type FrameContext = Context & { + trustedData?: { messageBytes: string }; + untrustedData?: { + fid: number; + url: string; + messageHash: string; + timestamp: number; + network: number; + buttonIndex?: 1 | 2 | 3 | 4; + castId: { fid: number; hash: string }; + inputText?: string; + }; +}; + type FrameReturnType = { image: JSX.Element; intents: JSX.Element; @@ -10,7 +24,7 @@ type FrameReturnType = { export class Framework extends Hono { frame( path: string, - handler: (c: Context) => FrameReturnType | Promise, + handler: (c: FrameContext) => FrameReturnType | Promise, ) { this.get(path, async (c) => { const { intents } = await handler(c); @@ -20,6 +34,7 @@ export class Framework extends Hono { + {parseIntents(intents)} , @@ -27,10 +42,26 @@ export class Framework extends Hono { }); // TODO: don't slice - this.get(`${path.slice(1)}/_og`, async (c) => { + this.get(`${path.slice(1)}_og`, async (c) => { const { image } = await handler(c); return new ImageResponse(image); }); + + this.post(path, async (c) => { + const context = await parsePostContext(c); + const { intents } = await handler(context); + return c.render( + + + + + + + {parseIntents(intents)} + + , + ); + }); } } @@ -50,6 +81,12 @@ export function Button({ children }: ButtonProps) { type Counter = { button: number }; +async function parsePostContext(ctx: Context): Promise { + const { trustedData, untrustedData } = + (await ctx.req.json().catch(() => {})) || {}; + return Object.assign(ctx, { trustedData, untrustedData }); +} + function parseIntents(intents_: JSX.Element) { const intents = intents_ as unknown as JSXNode; const counter: Counter = {