Skip to content

Commit 0bc117a

Browse files
authored
Merge pull request #248 from framesjs/feat/custom-middleware-example
feat: custom middleware example
2 parents 6142ac5 + e5674d6 commit 0bc117a

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

.changeset/yellow-zoos-promise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"framesjs-starter": patch
3+
---
4+
5+
feat: custom middleware example
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createFrames, types } from "frames.js/next";
2+
3+
const priceMiddleware: types.FramesMiddleware<
4+
any,
5+
{ ethPrice?: number }
6+
> = async (ctx, next) => {
7+
try {
8+
const res = await fetch(
9+
"https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
10+
);
11+
const {
12+
ethereum: { usd: ethPrice },
13+
} = await res.json();
14+
return next({ ethPrice });
15+
} catch (error) {
16+
console.error("Error fetching ETH price:", error);
17+
}
18+
return next();
19+
};
20+
21+
export const frames = createFrames({
22+
basePath: "/",
23+
initialState: {
24+
pageIndex: 0,
25+
},
26+
middleware: [priceMiddleware],
27+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-disable react/jsx-key */
2+
import { Button } from "frames.js/next";
3+
import { frames } from "./frames";
4+
5+
const handler = frames(async (ctx) => {
6+
return {
7+
image: <div tw="flex">ETH price: ${ctx.ethPrice}</div>,
8+
buttons: [<Button action="post">Refresh</Button>],
9+
};
10+
});
11+
12+
export const GET = handler;
13+
export const POST = handler;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Link from "next/link";
2+
import { currentURL, vercelURL } from "../../utils";
3+
import { createDebugUrl } from "../../debug";
4+
import type { Metadata } from "next";
5+
import { fetchMetadata } from "frames.js/next";
6+
7+
export async function generateMetadata(): Promise<Metadata> {
8+
return {
9+
title: "New api example",
10+
description: "This is a new api example",
11+
other: {
12+
...(await fetchMetadata(
13+
new URL(
14+
"/examples/new-api-custom-middleware/frames",
15+
vercelURL() || "http://localhost:3000"
16+
)
17+
)),
18+
},
19+
};
20+
}
21+
22+
export default async function Home() {
23+
const url = currentURL("/examples/new-api-cache-control");
24+
25+
return (
26+
<div>
27+
Custom middleware example{" "}
28+
<Link href={createDebugUrl(url)} className="underline">
29+
Debug
30+
</Link>
31+
</div>
32+
);
33+
}

0 commit comments

Comments
 (0)