diff --git a/src/next/index.ts b/src/next/index.ts index db1fcd09..a63f9607 100644 --- a/src/next/index.ts +++ b/src/next/index.ts @@ -4,3 +4,7 @@ export { getFrameMetadata } from './getFrameMetadata.js' export { handle } from '../vercel/index.js' export { isFrameRequest } from './isFrameRequest.js' +export { + postComposerActionMessage, + postComposerCreateCastActionMessage, +} from './postComposerActionMessage.js' diff --git a/src/next/postComposerActionMessage.ts b/src/next/postComposerActionMessage.ts new file mode 100644 index 00000000..af3f5dbe --- /dev/null +++ b/src/next/postComposerActionMessage.ts @@ -0,0 +1,38 @@ +export type ComposerActionMessage = { + type: 'createCast' + data: { + cast: { + parent?: string | undefined + text: string + embeds: 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, + }, + }) +}