Skip to content

Commit

Permalink
feat: custom middleware example
Browse files Browse the repository at this point in the history
  • Loading branch information
stephancill committed Mar 26, 2024
1 parent dee5dcb commit e5674d6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
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>
);
}

0 comments on commit e5674d6

Please sign in to comment.