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

feat: custom middleware example #248

Merged
merged 1 commit into from
Mar 27, 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
5 changes: 5 additions & 0 deletions .changeset/yellow-zoos-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"framesjs-starter": patch
---

feat: custom middleware example
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createFrames, types } from "frames.js/next";

const priceMiddleware: types.FramesMiddleware<
any,
{ ethPrice?: number }
> = async (ctx, next) => {
try {
const res = await fetch(
"https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
);
const {
ethereum: { usd: ethPrice },
} = await res.json();
return next({ ethPrice });
} catch (error) {
console.error("Error fetching ETH price:", error);
}
return next();
};

export const frames = createFrames({
basePath: "/",
initialState: {
pageIndex: 0,
},
middleware: [priceMiddleware],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable react/jsx-key */
import { Button } from "frames.js/next";
import { frames } from "./frames";

const handler = frames(async (ctx) => {
return {
image: <div tw="flex">ETH price: ${ctx.ethPrice}</div>,
buttons: [<Button action="post">Refresh</Button>],
};
});

export const GET = handler;
export const POST = handler;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Link from "next/link";
import { currentURL, vercelURL } from "../../utils";
import { createDebugUrl } from "../../debug";
import type { Metadata } from "next";
import { fetchMetadata } from "frames.js/next";

export async function generateMetadata(): Promise<Metadata> {
return {
title: "New api example",
description: "This is a new api example",
other: {
...(await fetchMetadata(
new URL(
"/examples/new-api-custom-middleware/frames",
vercelURL() || "http://localhost:3000"
)
)),
},
};
}

export default async function Home() {
const url = currentURL("/examples/new-api-cache-control");

return (
<div>
Custom middleware example{" "}
<Link href={createDebugUrl(url)} className="underline">
Debug
</Link>
</div>
);
}
Loading