-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: composer actions * chore: changesets * refactor: drop `RouteOptions` for composer action, add state * nit: add todo * feat: add `postComposerActionMessage` helper * nit: tested composer actions * fix: types * nit: update `ComposerActionMessage` with `channelKey`
- Loading branch information
Showing
12 changed files
with
296 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"frog": patch | ||
--- | ||
|
||
Added support for Composer Actions. [See More](https://warpcast.notion.site/Draft-Composer-Actions-7f2b8739ee8447cc8a6b518c234b1eeb). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Frog } from 'frog' | ||
|
||
import { vars } from './ui.js' | ||
|
||
export const app = new Frog({ | ||
ui: { vars }, | ||
title: 'Composer Action', | ||
}).composerAction('/', async (c) => { | ||
console.log( | ||
`Composer Action call ${JSON.stringify(c.actionData, null, 2)} from ${ | ||
c.actionData.fid | ||
}`, | ||
) | ||
// if (Math.random() > 0.5) return c.error({ message: 'Action failed :(' }) | ||
return c.res({ | ||
title: 'Some Composer Action', | ||
url: 'https://example.com', | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
// TODO: Rename this package to `js` as most of it doesn't strictly depend | ||
// on Next.JS specific features. Only `handle` does. | ||
|
||
export { getFrameMetadata } from './getFrameMetadata.js' | ||
export { handle } from '../vercel/index.js' | ||
export { isFrameRequest } from './isFrameRequest.js' | ||
export { | ||
postComposerActionMessage, | ||
postComposerCreateCastActionMessage, | ||
} from './postComposerActionMessage.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export type ComposerActionMessage = { | ||
type: 'createCast' | ||
data: { | ||
cast: { | ||
channelKey?: string | undefined | ||
embeds: string[] | ||
parent?: string | undefined | ||
text: string | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Posts Composer Action Message to `window.parent`. | ||
*/ | ||
export function postComposerActionMessage(message: ComposerActionMessage) { | ||
if (typeof window === 'undefined') | ||
throw new Error( | ||
'`postComposerActionMessage` must be called in the Client Component.', | ||
) | ||
|
||
window.parent.postMessage(message, '*') | ||
} | ||
|
||
/** | ||
* Posts Composer Create Cast Action Message to `window.parent`. | ||
* | ||
* This is a convinience method and it calls `postComposerActionMessage` under the hood. | ||
*/ | ||
export function postComposerCreateCastActionMessage( | ||
message: ComposerActionMessage['data']['cast'], | ||
) { | ||
return postComposerActionMessage({ | ||
type: 'createCast', | ||
data: { | ||
cast: message, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { TypedResponse } from './response.js' | ||
|
||
export type ComposerActionResponse = { | ||
/** | ||
* Title of the action. | ||
*/ | ||
title: string | ||
/** | ||
* URL of the form. | ||
* | ||
* @example https://example.com/form | ||
*/ | ||
url: string | ||
} | ||
|
||
export type ComposerActionResponseFn = ( | ||
response: ComposerActionResponse, | ||
) => TypedResponse<ComposerActionResponse> | ||
|
||
export type ComposerActionData = { | ||
buttonIndex: 1 | ||
castId: { fid: number; hash: string } | ||
fid: number | ||
messageHash: string | ||
network: number | ||
timestamp: number | ||
url: string | ||
state: { | ||
requestId: string | ||
cast: { | ||
parent?: string | undefined | ||
text: string | ||
embeds: string[] | ||
castDistribution: string | ||
} | ||
} | ||
} | ||
|
||
export type TrustedData = { | ||
messageBytes: string | ||
} | ||
|
||
export type UntrustedData = ComposerActionData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Input } from 'hono' | ||
import type { ComposerActionContext, Context } from '../types/context.js' | ||
import type { Env } from '../types/env.js' | ||
|
||
type GetComposerActionContextParameters< | ||
env extends Env = Env, | ||
path extends string = string, | ||
input extends Input = {}, | ||
> = { | ||
context: Context<env, path, input> | ||
} | ||
|
||
type GetComposerActionContextReturnType< | ||
env extends Env = Env, | ||
path extends string = string, | ||
input extends Input = {}, | ||
> = { | ||
context: ComposerActionContext<env, path, input> | ||
} | ||
|
||
export function getComposerActionContext< | ||
env extends Env, | ||
path extends string, | ||
input extends Input = {}, | ||
>( | ||
parameters: GetComposerActionContextParameters<env, path, input>, | ||
): GetComposerActionContextReturnType<env, path, input> { | ||
const { context } = parameters | ||
const { env, frameData, req, verified } = context || {} | ||
|
||
if (!frameData) | ||
throw new Error('Frame data must be present for action handlers.') | ||
if (!frameData.state) | ||
throw new Error('State must be present for composer action handler.') | ||
|
||
return { | ||
context: { | ||
actionData: { | ||
buttonIndex: 1, | ||
castId: frameData.castId, | ||
fid: frameData.fid, | ||
network: frameData.network, | ||
messageHash: frameData.messageHash, | ||
timestamp: frameData.timestamp, | ||
state: JSON.parse(decodeURIComponent(frameData.state)), | ||
url: frameData.url, | ||
}, | ||
env, | ||
error: (data) => ({ | ||
error: data, | ||
format: 'composerAction', | ||
status: 'error', | ||
}), | ||
req, | ||
res: (data) => ({ | ||
data, | ||
format: 'composerAction', | ||
status: 'success', | ||
}), | ||
var: context.var, | ||
verified, | ||
}, | ||
} | ||
} |