Skip to content

Commit

Permalink
nit: tested composer actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Jul 17, 2024
1 parent 050804b commit 456f5da
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 42 deletions.
4 changes: 2 additions & 2 deletions playground/src/composerAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
})
2 changes: 2 additions & 0 deletions playground/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 3 additions & 38 deletions src/frog-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -435,48 +434,14 @@ export class FrogBase<
composerAction: HandlerInterface<env, 'composerAction', schema, basePath> = (
...parameters: any[]
) => {
const [path, middlewares, handler, options] = getRouteParameters<
const [path, middlewares, handler, options = {}] = getRouteParameters<
env,
ComposerActionHandler<env>,
'composerAction'
>(...parameters)

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<env, string>({
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/utils/getRouteParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function getRouteParameters<
string,
MiddlewareHandler<env>[],
handler,
method extends 'castAction' | 'composerAction'
method extends 'castAction'
? RouteOptions<method>
: RouteOptions<method> | undefined,
] {
const options: method extends 'castAction' | 'composerAction'
const options: method extends 'castAction'
? RouteOptions<method>
: RouteOptions<method> | undefined =
typeof parameters[parameters.length - 1] === 'object'
Expand Down

0 comments on commit 456f5da

Please sign in to comment.