Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle button post_url attribute #490

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/angry-eels-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"frames.js": patch
"@frames.js/debugger": patch
"@frames.js/render": patch
---

fix: handle button post_url attribute
2 changes: 2 additions & 0 deletions docs/pages/reference/js/types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ export interface FrameButtonPost {
/**
* POST the packet to fc:frame:button:$idx:action:target if present
* POST the packet to fc:frame:post_url if target was not present.
* POST the packet to fc:frame:button:$idx:post_url if target was not present.
*/
target?: string;
post_url?: string;
/** A 256-byte string which is label of the button */
label: string;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/debugger/app/frames/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export async function GET(request: NextRequest): Promise<Response> {
} catch (err) {
// eslint-disable-next-line no-console -- provide feedback to the developer
console.error(err);

if (err instanceof Error) {
return Response.json({ message: err.message }, { status: 500 });
}

return Response.json({ message: err }, { status: 500 });
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/frames.js/src/frame-parsers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,24 @@ function parsePostOrPostRedirectButton(
return undefined;
}

if (
button.post_url &&
!isValid(
reporter,
`${metaPropertyPrefix}:${button.index}:post_url`,
validateUrl,
button.post_url,
false
)
) {
return undefined;
}

return {
action: button.action as "post" | "post_redirect",
label: button.label,
target: button.target,
post_url: button.post_url,
};
}

Expand Down
36 changes: 36 additions & 0 deletions packages/frames.js/src/getFrame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,40 @@ describe("getFrame", () => {
},
});
});

it("should parse button post_url (farcaster)", () => {
const html = `
<meta name="fc:frame" content="vNext"/>
<meta name="fc:frame:image" content="http://example.com/image.png"/>
<meta name="og:image" content="http://example.com/image.png"/>
<meta name="fc:frame:button:1" content="Submit"/>
<meta name="fc:frame:button:1:post_url" content="https://example.com/submit"/>
<title>test</title>
`;
const frame = getFrame({
htmlString: html,
url: "https://example.com",
});

expect(frame).toEqual({
status: "success",
frame: {
version: "vNext",
image: "http://example.com/image.png",
ogImage: "http://example.com/image.png",
buttons: [
{
label: "Submit",
action: "post",
target: undefined,
post_url: "https://example.com/submit",
},
],
title: "test",
postUrl: "https://example.com/",
},
reports: {},
framesVersion: undefined,
});
});
});
8 changes: 5 additions & 3 deletions packages/frames.js/src/getFrameFlattened.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("getFrameFlattened", () => {
{
label: "Button 1",
action: "post",
target: "target1",
post_url: "post1",
},
{
label: "Button 2",
Expand All @@ -41,6 +41,7 @@ describe("getFrameFlattened", () => {
version: "vNext",
},
],
title: "test title",
};

const flattened = getFrameFlattened(frame);
Expand All @@ -54,7 +55,7 @@ describe("getFrameFlattened", () => {
"fc:frame:image:aspect_ratio": "1:1",
"fc:frame:button:1": "Button 1",
"fc:frame:button:1:action": "post",
"fc:frame:button:1:target": "target1",
"fc:frame:button:1:post_url": "post1",
"fc:frame:button:2": "Button 2",
"fc:frame:button:2:action": "post",
"fc:frame:button:2:target": "target2",
Expand All @@ -63,7 +64,7 @@ describe("getFrameFlattened", () => {
"of:accepts:farcaster": "vNext",
"of:button:1": "Button 1",
"of:button:1:action": "post",
"of:button:1:target": "target1",
"of:button:1:post_url": "post1",
"of:button:2": "Button 2",
"of:button:2:action": "post",
"of:button:2:target": "target2",
Expand All @@ -74,6 +75,7 @@ describe("getFrameFlattened", () => {
"of:state": '{"foo":"bar"}',
"of:version": "vNext",
"frames.js:version": "0.0.0-mock",
"og:title": "test title",
});
});

Expand Down
8 changes: 6 additions & 2 deletions packages/frames.js/src/getFrameFlattened.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export function getFrameFlattened(
[`of:button:${index + 1}`]: button.label,
[`of:button:${index + 1}:action`]: button.action,
[`of:button:${index + 1}:target`]: button.target,
...(button.action === "tx"
...(button.action === "tx" ||
button.action === "post" ||
button.action === "post_redirect"
? { [`of:button:${index + 1}:post_url`]: button.post_url }
: {}),
}),
Expand All @@ -79,7 +81,9 @@ export function getFrameFlattened(
[`fc:frame:button:${index + 1}`]: button.label,
[`fc:frame:button:${index + 1}:action`]: button.action,
[`fc:frame:button:${index + 1}:target`]: button.target,
...(button.action === "tx"
...(button.action === "tx" ||
button.action === "post" ||
button.action === "post_redirect"
? { [`fc:frame:button:${index + 1}:post_url`]: button.post_url }
: {}),
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/frames.js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ export interface FrameButtonPost {
/**
* POST the packet to fc:frame:button:$idx:action:target if present
* POST the packet to fc:frame:post_url if target was not present.
* POST the packet to fc:frame:button:$idx:post_url if target was not present.
*/
target?: string;
post_url?: string;
/** A 256-byte string which is label of the button */
label: string;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/render/src/use-frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ export function useFrame<
case "post_redirect": {
try {
const target =
frameButton.target || currentFrame.postUrl || homeframeUrl;
frameButton.target ||
frameButton.post_url ||
currentFrame.postUrl ||
homeframeUrl;

if (!target) {
throw new Error("missing target");
Expand Down
Loading