Skip to content

Commit

Permalink
feat: add an optional link property to action response (#293)
Browse files Browse the repository at this point in the history
* feat: add an optional `link` property to action response

[See more](https://docs.farcaster.xyz/reference/actions/spec#message-response-type).

* chore: changesets

* nit: tweak

* docs: update

* chore: up

---------

Co-authored-by: Tom Meagher <tom@meagher.co>
  • Loading branch information
dalechyn and tmm committed May 3, 2024
1 parent c31814d commit 536c491
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/sour-tables-happen.md
@@ -0,0 +1,6 @@
---
"frog": patch
---

Added `link` property to `CastActionMessageResponse` to display the toasted message as an external link to the specified URL.
[See more](https://docs.farcaster.xyz/reference/actions/spec#message-response-type).
5 changes: 4 additions & 1 deletion playground/src/castAction.tsx
Expand Up @@ -57,7 +57,10 @@ export const app = new Frog({
}`,
)
if (Math.random() > 0.5) return c.error({ message: 'Action failed :(' })
return c.message({ message: 'Action Succeeded' })
return c.message({
message: 'Action Succeeded',
link: 'https://frog.fm',
})
},
{
name: 'Log This!',
Expand Down
34 changes: 31 additions & 3 deletions site/pages/reference/frog-cast-action-response.mdx
Expand Up @@ -35,7 +35,7 @@ app.castAction('/action/foo', (c) => {
'Cache-Control': 'max-age=0', // [!code focus]
}, // [!code focus]
message: 'Success!',
statusCode: 200
statusCode: 200,
})
})
```
Expand All @@ -56,7 +56,35 @@ export const app = new Frog()
app.castAction('/', (c) => {
return c.res({
message: 'Action Succeeded!', // [!code focus]
statusCode: 200
statusCode: 200,
})
})
```

## link
#
:::warning
The `link` property is valid to be added only in response with HTTP status 200.
Thus you cannot add one in `.error()` response.
:::

- **Type:** `string | undefined`

An optional URL. Must be http:// or https:// protocol.
If present, clients must display the message as an external link to this URL.

```tsx twoslash
/** @jsxImportSource frog/jsx */
// ---cut---
import { Button, Frog } from 'frog'

export const app = new Frog()

app.castAction('/', (c) => {
return c.res({
message: 'Action Succeeded!',
link: 'https://frog.fm', // [!code focus]
statusCode: 200,
})
})
```
Expand All @@ -78,7 +106,7 @@ export const app = new Frog()
app.castAction('/', (c) => {
return c.res({
message: 'Action Succeeded!',
statusCode: 200 // [!code focus]
statusCode: 200, // [!code focus]
})
})
```
Expand Down
8 changes: 3 additions & 5 deletions src/frog-base.tsx
Expand Up @@ -329,15 +329,14 @@ export class FrogBase<
const origin = this.origin ?? url.origin
const baseUrl = origin + parsePath(this.basePath)

if (c.req.method === 'GET') {
if (c.req.method === 'GET')
return c.json({
...installParameters,
postUrl: baseUrl + parsePath(path),
action: {
type: 'post',
},
})
}

const { context } = getCastActionContext<env, string>({
context: await requestBodyToContext(c, {
Expand Down Expand Up @@ -366,9 +365,8 @@ export class FrogBase<
})
}

const { message } = response.data

return c.json({ message, type: 'message' })
const { message, link } = response.data
return c.json({ message, link, type: 'message' })
})

return this
Expand Down
4 changes: 4 additions & 0 deletions src/types/castAction.ts
Expand Up @@ -20,6 +20,10 @@ export type CastActionMessageResponse = {
* @example 'Action succeded!'
*/
message: string
/**
* If present, clients must display the message as an external link to this URL.
*/
link?: string | undefined
}

export type CastActionMessageResponseFn = (
Expand Down

0 comments on commit 536c491

Please sign in to comment.