From 456f5da00d68b91f483ec7622d41623987605df9 Mon Sep 17 00:00:00 2001 From: dalechyn Date: Wed, 17 Jul 2024 22:15:07 +0300 Subject: [PATCH] nit: tested composer actions --- playground/src/composerAction.tsx | 4 +-- playground/src/index.tsx | 2 ++ src/frog-base.tsx | 41 +++---------------------------- src/utils/getRouteParameters.ts | 4 +-- 4 files changed, 9 insertions(+), 42 deletions(-) diff --git a/playground/src/composerAction.tsx b/playground/src/composerAction.tsx index 1c5a7484..e9ace242 100644 --- a/playground/src/composerAction.tsx +++ b/playground/src/composerAction.tsx @@ -11,9 +11,9 @@ export const app = new Frog({ c.actionData.fid }`, ) - if (Math.random() > 0.5) return c.error({ message: 'Action failed :(' }) + // if (Math.random() > 0.5) return c.error({ message: 'Action failed :(' }) return c.res({ title: 'Some Composer Action', - url: 'https://somewhere.com/some-form', + url: 'https://example.com', }) }) diff --git a/playground/src/index.tsx b/playground/src/index.tsx index 11e4d4bb..85c146d2 100644 --- a/playground/src/index.tsx +++ b/playground/src/index.tsx @@ -5,6 +5,7 @@ import { neynar } from 'frog/hubs' import { Box, Heading, vars } from './ui.js' import { app as castActionApp } from './castAction.js' +import { app as composerActionApp } from './composerAction.js' import { app as fontsApp } from './fonts.js' import { app as initial } from './initial.js' import { app as middlewareApp } from './middleware.js' @@ -193,6 +194,7 @@ export const app = new Frog({ return c.error({ message: 'Bad inputs!' }) }) .route('/castAction', castActionApp) + .route('/composerAction', composerActionApp) .route('/initial', initial) .route('/ui', uiSystemApp) .route('/fonts', fontsApp) diff --git a/src/frog-base.tsx b/src/frog-base.tsx index 0443e1f9..bb5b874e 100644 --- a/src/frog-base.tsx +++ b/src/frog-base.tsx @@ -12,7 +12,6 @@ import lz from 'lz-string' import { default as p } from 'path-browserify' import type { CastActionOptions } from './types/castAction.js' -import type { ComposerActionOptions } from './types/composerAction.js' import type { Env } from './types/env.js' import type { FrameImageAspectRatio, @@ -435,7 +434,7 @@ export class FrogBase< composerAction: HandlerInterface = ( ...parameters: any[] ) => { - const [path, middlewares, handler, options] = getRouteParameters< + const [path, middlewares, handler, options = {}] = getRouteParameters< env, ComposerActionHandler, 'composerAction' @@ -443,40 +442,6 @@ export class FrogBase< const { verify = this.verify } = options - // Composer Action Route (implements GET). - if ('handler' in options) { - this.hono.get(parseHonoPath(path), ...middlewares, async (c) => { - const url = getRequestUrl(c.req) - - const { aboutUrl, name, description, icon } = await options.handler(c) - return c.json({ - aboutUrl, - action: { - type: 'post', - }, - name, - description, - icon, - postUrl: url, - }) - }) - } else { - const { aboutUrl, name, description, icon } = options - - this.hono.get(parseHonoPath(path), ...middlewares, async (c) => { - const url = getRequestUrl(c.req) - return c.json({ - aboutUrl, - action: { - type: 'post', - }, - name, - description, - icon, - postUrl: url, - }) - }) - } // Composer Action Route (implements POST). this.hono.post(parseHonoPath(path), ...middlewares, async (c) => { const { context } = getComposerActionContext({ @@ -496,8 +461,8 @@ export class FrogBase< return c.json({ message: response.error.message }) } - const { url: formUrl } = response.data - return c.json({ url: formUrl, type: 'form' }) + const { url: formUrl, title } = response.data + return c.json({ url: formUrl, title, type: 'form' }) }) return this diff --git a/src/utils/getRouteParameters.ts b/src/utils/getRouteParameters.ts index b7a2b9fa..4e8d1d64 100644 --- a/src/utils/getRouteParameters.ts +++ b/src/utils/getRouteParameters.ts @@ -12,11 +12,11 @@ export function getRouteParameters< string, MiddlewareHandler[], handler, - method extends 'castAction' | 'composerAction' + method extends 'castAction' ? RouteOptions : RouteOptions | undefined, ] { - const options: method extends 'castAction' | 'composerAction' + const options: method extends 'castAction' ? RouteOptions : RouteOptions | undefined = typeof parameters[parameters.length - 1] === 'object'