File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed
examples/framesjs-starter/app/examples/new-api-custom-middleware Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " framesjs-starter " : patch
3+ ---
4+
5+ feat: custom middleware example
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments