-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dee5dcb
commit e5674d6
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"framesjs-starter": patch | ||
--- | ||
|
||
feat: custom middleware example |
27 changes: 27 additions & 0 deletions
27
examples/framesjs-starter/app/examples/new-api-custom-middleware/frames/frames.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}); |
13 changes: 13 additions & 0 deletions
13
examples/framesjs-starter/app/examples/new-api-custom-middleware/frames/route.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
33 changes: 33 additions & 0 deletions
33
examples/framesjs-starter/app/examples/new-api-custom-middleware/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |