make redis optional

This commit is contained in:
lucas-neynar 2025-03-14 18:00:39 -07:00
parent c9deb0512c
commit 6f105d37c7
No known key found for this signature in database
3 changed files with 29 additions and 12 deletions

4
dev.js
View File

@ -21,10 +21,10 @@ async function startDev() {
💻 To test on desktop: 💻 To test on desktop:
1. Open the localtunnel URL in your browser: ${tunnel.url} 1. Open the localtunnel URL in your browser: ${tunnel.url}
2. Enter your IP address in the password field${ip ? `: ${ip}` : ''} 2. Enter your IP address in the password field${ip ? `: ${ip}` : ''}
3. Click "Click to Submit" -- your frame should now load 3. Click "Click to Submit" -- your frame should now load in the browser
4. Navigate to the Warpcast Frame Developer Tools: https://warpcast.com/~/developers/frames 4. Navigate to the Warpcast Frame Developer Tools: https://warpcast.com/~/developers/frames
5. Enter your frame URL: ${tunnel.url} 5. Enter your frame URL: ${tunnel.url}
6. Click "Preview" to launch your frame within Warpcast 6. Click "Preview" to launch your frame within Warpcast (note that it may take ~10 seconds to load)
You will not be able to load your frame in Warpcast until You will not be able to load your frame in Warpcast until

View File

@ -1,6 +1,6 @@
{ {
"name": "create-neynar-farcaster-frame", "name": "create-neynar-farcaster-frame",
"version": "1.0.0", "version": "1.0.1",
"type": "module", "type": "module",
"files": [ "files": [
"bin/index.js" "bin/index.js"

View File

@ -1,10 +1,15 @@
import { FrameNotificationDetails } from "@farcaster/frame-sdk"; import { FrameNotificationDetails } from "@farcaster/frame-sdk";
import { Redis } from "@upstash/redis"; import { Redis } from "@upstash/redis";
const redis = new Redis({ // In-memory fallback storage
url: process.env.KV_REST_API_URL, const localStore = new Map<string, FrameNotificationDetails>();
token: process.env.KV_REST_API_TOKEN,
}); // Use Redis if KV env vars are present, otherwise use in-memory
const useRedis = process.env.KV_REST_API_URL && process.env.KV_REST_API_TOKEN;
const redis = useRedis ? new Redis({
url: process.env.KV_REST_API_URL!,
token: process.env.KV_REST_API_TOKEN!,
}) : null;
function getUserNotificationDetailsKey(fid: number): string { function getUserNotificationDetailsKey(fid: number): string {
return `${process.env.NEXT_PUBLIC_FRAME_NAME}:user:${fid}`; return `${process.env.NEXT_PUBLIC_FRAME_NAME}:user:${fid}`;
@ -13,20 +18,32 @@ function getUserNotificationDetailsKey(fid: number): string {
export async function getUserNotificationDetails( export async function getUserNotificationDetails(
fid: number fid: number
): Promise<FrameNotificationDetails | null> { ): Promise<FrameNotificationDetails | null> {
return await redis.get<FrameNotificationDetails>( const key = getUserNotificationDetailsKey(fid);
getUserNotificationDetailsKey(fid) if (redis) {
); return await redis.get<FrameNotificationDetails>(key);
}
return localStore.get(key) || null;
} }
export async function setUserNotificationDetails( export async function setUserNotificationDetails(
fid: number, fid: number,
notificationDetails: FrameNotificationDetails notificationDetails: FrameNotificationDetails
): Promise<void> { ): Promise<void> {
await redis.set(getUserNotificationDetailsKey(fid), notificationDetails); const key = getUserNotificationDetailsKey(fid);
if (redis) {
await redis.set(key, notificationDetails);
} else {
localStore.set(key, notificationDetails);
}
} }
export async function deleteUserNotificationDetails( export async function deleteUserNotificationDetails(
fid: number fid: number
): Promise<void> { ): Promise<void> {
await redis.del(getUserNotificationDetailsKey(fid)); const key = getUserNotificationDetailsKey(fid);
if (redis) {
await redis.del(key);
} else {
localStore.delete(key);
}
} }