mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-21 18:28:00 -05:00
make redis optional
This commit is contained in:
@@ -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<string, FrameNotificationDetails>();
|
||||
|
||||
// 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<FrameNotificationDetails | null> {
|
||||
return await redis.get<FrameNotificationDetails>(
|
||||
getUserNotificationDetailsKey(fid)
|
||||
);
|
||||
const key = getUserNotificationDetailsKey(fid);
|
||||
if (redis) {
|
||||
return await redis.get<FrameNotificationDetails>(key);
|
||||
}
|
||||
return localStore.get(key) || null;
|
||||
}
|
||||
|
||||
export async function setUserNotificationDetails(
|
||||
fid: number,
|
||||
notificationDetails: FrameNotificationDetails
|
||||
): 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(
|
||||
fid: number
|
||||
): Promise<void> {
|
||||
await redis.del(getUserNotificationDetailsKey(fid));
|
||||
const key = getUserNotificationDetailsKey(fid);
|
||||
if (redis) {
|
||||
await redis.del(key);
|
||||
} else {
|
||||
localStore.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user