Add awaits

This commit is contained in:
Andrew Pennington 2024-08-15 23:40:42 +01:00
parent f11180db7a
commit 631a5e3e31
No known key found for this signature in database
GPG key ID: E9DA097213FD17EA
8 changed files with 14 additions and 13 deletions

View file

@ -22,7 +22,7 @@ const Page = () => {
const fetchChats = async () => { const fetchChats = async () => {
setLoading(true); setLoading(true);
const res = await fetch(`${getServerEnv("BACKEND_API_URL")}/chats`, { const res = await fetch(`${await getServerEnv("BACKEND_API_URL")}/chats`, {
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View file

@ -40,7 +40,7 @@ const useSocket = (
); );
const providers = await fetch( const providers = await fetch(
`${getServerEnv("BACKEND_API_URL")}/models`, `${await getServerEnv("BACKEND_API_URL")}/models`,
{ {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -221,7 +221,7 @@ const loadMessages = async (
setNotFound: (notFound: boolean) => void, setNotFound: (notFound: boolean) => void,
) => { ) => {
const res = await fetch( const res = await fetch(
`${getServerEnv("BACKEND_API_URL")}/chats/${chatId}`, `${await getServerEnv("BACKEND_API_URL")}/chats/${chatId}`,
{ {
method: 'GET', method: 'GET',
headers: { headers: {

View file

@ -21,7 +21,7 @@ const DeleteChat = ({
setLoading(true); setLoading(true);
try { try {
const res = await fetch( const res = await fetch(
`${getServerEnv("BACKEND_API_URL")}/chats/${chatId}`, `${await getServerEnv("BACKEND_API_URL")}/chats/${chatId}`,
{ {
method: 'DELETE', method: 'DELETE',
headers: { headers: {

View file

@ -35,7 +35,7 @@ const SearchImages = ({
const chatModel = localStorage.getItem('chatModel'); const chatModel = localStorage.getItem('chatModel');
const res = await fetch( const res = await fetch(
`${getServerEnv("BACKEND_API_URL")}/images`, `${await getServerEnv("BACKEND_API_URL")}/images`,
{ {
method: 'POST', method: 'POST',
headers: { headers: {

View file

@ -48,7 +48,7 @@ const Searchvideos = ({
const chatModel = localStorage.getItem('chatModel'); const chatModel = localStorage.getItem('chatModel');
const res = await fetch( const res = await fetch(
`${getServerEnv("BACKEND_API_URL")}/videos`, `${await getServerEnv("BACKEND_API_URL")}/videos`,
{ {
method: 'POST', method: 'POST',
headers: { headers: {

View file

@ -89,7 +89,7 @@ const SettingsDialog = ({
if (isOpen) { if (isOpen) {
const fetchConfig = async () => { const fetchConfig = async () => {
setIsLoading(true); setIsLoading(true);
const res = await fetch(`${getServerEnv("BACKEND_API_URL")}/config`, { const res = await fetch(`${await getServerEnv("BACKEND_API_URL")}/config`, {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
@ -149,7 +149,7 @@ const SettingsDialog = ({
setIsUpdating(true); setIsUpdating(true);
try { try {
await fetch(`${getServerEnv("BACKEND_API_URL")}/config`, { await fetch(`${await getServerEnv("BACKEND_API_URL")}/config`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View file

@ -5,7 +5,7 @@ export const getSuggestions = async (chatHisory: Message[]) => {
const chatModel = localStorage.getItem('chatModel'); const chatModel = localStorage.getItem('chatModel');
const chatModelProvider = localStorage.getItem('chatModelProvider'); const chatModelProvider = localStorage.getItem('chatModelProvider');
const res = await fetch(`${getServerEnv("BACKEND_API_URL")}/suggestions`, { const res = await fetch(`${await getServerEnv("BACKEND_API_URL")}/suggestions`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View file

@ -1,7 +1,8 @@
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
import process from 'process';
// In-memory cache for configuration data // In-memory cache for configuration data
let cachedConfig: { [key: string]: any } | null = null; let cachedConfig: { [key: string]: string } ;
let cacheTimestamp: number | null = null; let cacheTimestamp: number | null = null;
const CACHE_DURATION_MS = 5 * 60 * 1000; // Cache duration: 5 minutes const CACHE_DURATION_MS = 5 * 60 * 1000; // Cache duration: 5 minutes
@ -22,13 +23,13 @@ async function fetchConfig() {
} }
} }
export async function getServerEnv(envVar: string): Promise<string | null> { export async function await getServerEnv(envVar: string): Promise<string> {
// Check if the cache is still valid // Check if the cache is still valid
if (cachedConfig && cacheTimestamp && Date.now() - cacheTimestamp < CACHE_DURATION_MS) { if (cachedConfig && cacheTimestamp && Date.now() - cacheTimestamp < CACHE_DURATION_MS) {
return cachedConfig[envVar] || null; return cachedConfig[envVar] || process.env[envVar];
} }
// Fetch and cache the config if not in cache or cache is expired // Fetch and cache the config if not in cache or cache is expired
await fetchConfig(); await fetchConfig();
return cachedConfig ? cachedConfig[envVar] || null : null; return cachedConfig[envVar];
} }