From 993c7be12d94017b51ff142f5cb3512de712cd8c Mon Sep 17 00:00:00 2001 From: DirkSchlossmacher <62424946+DirkSchlossmacher@users.noreply.github.com> Date: Tue, 14 Nov 2023 11:36:02 +0100 Subject: [PATCH] redis no error --- app/utils/cloud/redisRestClient.ts | 35 ++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/app/utils/cloud/redisRestClient.ts b/app/utils/cloud/redisRestClient.ts index 56cffef2d..0069a3f6b 100644 --- a/app/utils/cloud/redisRestClient.ts +++ b/app/utils/cloud/redisRestClient.ts @@ -1,20 +1,33 @@ // redisRestClient.ts import { Redis } from "@upstash/redis"; -// Check if the environment variables are set -const redisUrl = process.env.UPSTASH_REDIS_URL; -const redisToken = process.env.UPSTASH_REDIS_REST_TOKEN; +let redis: Redis | undefined; -if (!redisUrl || !redisToken) { - throw new Error('The Upstash Redis URL and token must be provided.'); -} +const getRedisClient = () => { + if (!redis) { + const redisUrl = process.env.UPSTASH_REDIS_URL; + const redisToken = process.env.UPSTASH_REDIS_REST_TOKEN; -// Create the Redis instance with the known-to-be-defined values -const redis = new Redis({ - url: redisUrl, - token: redisToken, -}); + if (!redisUrl || !redisToken) { + // Handle missing environment variables more gracefully + console.error('The Upstash Redis URL and token must be provided.'); + return null; // Or throw an error if that's the desired behavior + } + redis = new Redis({ + url: redisUrl, + token: redisToken, + }); + } + return redis; +}; + + +// Use getRedisClient() to access the Redis client in your functions +export const incrementSignInCount = async (email: string | undefined, dateKey: string) => { + const redis = getRedisClient(); + // ... rest of your function +}; export const incrementSignInCount = async (email: string | undefined, dateKey: string) => { if (!email) {