From 54676a987cb82857c10473fa9e00ccc0c120d7a8 Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Thu, 8 Feb 2024 15:19:30 +1100 Subject: [PATCH] refactor: simplify routes --- src/index.tsx | 100 ++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 56 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 2db2b0fd..730a6fb6 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -32,66 +32,38 @@ type FrameReturnType = { intents: JSX.Element } -const previewRenderer = jsxRenderer( - ({ children }) => { - return ( - - - 𝑭𝒂𝒓𝒄 Preview - - - {children} - - ) - }, - { docType: true }, -) - -const frameRenderer = jsxRenderer( - ({ context, intents }) => { - const serializedContext = encodeURIComponent(JSON.stringify(context)) - return ( - - - - - - - {parseIntents(intents)} - - - ) - }, - { docType: true }, -) - export class Framework extends Hono { frame( path: string, handler: (c: FrameContext) => FrameReturnType | Promise, ) { - // Frame Routes - this.use(frameRenderer) - .get(async (c) => { - const context = await getFrameContext(c) - const { intents } = await handler(context) - return c.render(null, { context, intents }) - }) - .post(async (c) => { - const context = await getFrameContext(c) - const { intents } = await handler(context) - return c.render(null, { context, intents }) - }) + // Frame Route (implements GET & POST). + this.use(async (c) => { + const context = await getFrameContext(c) + const { intents } = await handler(context) + const serializedContext = encodeURIComponent(JSON.stringify(context)) + return c.render( + + + + + + + {parseIntents(intents)} + + , + ) + }) // OG Image Route this.get('image', async (c) => { @@ -104,7 +76,23 @@ export class Framework extends Hono { }) // Frame Preview Routes - this.use('preview', previewRenderer) + this.use( + 'preview', + jsxRenderer( + ({ children }) => { + return ( + + + 𝑭𝒂𝒓𝒄 Preview + + + {children} + + ) + }, + { docType: true }, + ), + ) .get(async (c) => { const baseUrl = c.req.url.replace('/preview', '') const response = await fetch(baseUrl)