From 6f105d37c7669b297f95e61e87f58b6cf389282b Mon Sep 17 00:00:00 2001 From: lucas-neynar Date: Fri, 14 Mar 2025 18:00:39 -0700 Subject: [PATCH] make redis optional --- dev.js | 4 ++-- package.json | 2 +- src/lib/kv.ts | 35 ++++++++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/dev.js b/dev.js index 89ad5a0..5b748a7 100644 --- a/dev.js +++ b/dev.js @@ -21,10 +21,10 @@ async function startDev() { 💻 To test on desktop: 1. Open the localtunnel URL in your browser: ${tunnel.url} 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 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 ❗️ diff --git a/package.json b/package.json index a92ea24..f519ad5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "create-neynar-farcaster-frame", - "version": "1.0.0", + "version": "1.0.1", "type": "module", "files": [ "bin/index.js" diff --git a/src/lib/kv.ts b/src/lib/kv.ts index 05e1918..b49c45b 100644 --- a/src/lib/kv.ts +++ b/src/lib/kv.ts @@ -1,10 +1,15 @@ import { FrameNotificationDetails } from "@farcaster/frame-sdk"; import { Redis } from "@upstash/redis"; -const redis = new Redis({ - url: process.env.KV_REST_API_URL, - token: process.env.KV_REST_API_TOKEN, -}); +// In-memory fallback storage +const localStore = new Map(); + +// 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 { return `${process.env.NEXT_PUBLIC_FRAME_NAME}:user:${fid}`; @@ -13,20 +18,32 @@ function getUserNotificationDetailsKey(fid: number): string { export async function getUserNotificationDetails( fid: number ): Promise { - return await redis.get( - getUserNotificationDetailsKey(fid) - ); + const key = getUserNotificationDetailsKey(fid); + if (redis) { + return await redis.get(key); + } + return localStore.get(key) || null; } export async function setUserNotificationDetails( fid: number, notificationDetails: FrameNotificationDetails ): Promise { - 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( fid: number ): Promise { - await redis.del(getUserNotificationDetailsKey(fid)); + const key = getUserNotificationDetailsKey(fid); + if (redis) { + await redis.del(key); + } else { + localStore.delete(key); + } }