Skip to content

Commit

Permalink
clean up even more code
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianka committed Nov 11, 2024
1 parent 089dc01 commit 17149f0
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 70 deletions.
2 changes: 1 addition & 1 deletion nextjs/src/app/api/cards/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";
import { createServiceRoleClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import initClients from "@/lib/initClient";
import { initClients } from "@/utils/initClients";

// retrieve thread history
async function getThreadHistory(
Expand Down
19 changes: 2 additions & 17 deletions nextjs/src/app/api/flashcards/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
"use server";
import { createServiceRoleClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import initClients from "@/lib/initClient";

// retrieve assistant from database
async function getAssistant(supabase: ReturnType<typeof createServiceRoleClient>) {
const { data, error } = await supabase
.from("assistants")
.select("*")
.eq("name", "StudyHelper")
.single();

if (error) {
console.error("Error fetching assistant:", error);
throw new Error("Assistant not found");
}

return data;
}
import { initClients } from "@/utils/initClients";
import { getAssistant } from "@/utils/initClients";

// create thread and store it in database
async function getOrCreateThread(
Expand Down
19 changes: 2 additions & 17 deletions nextjs/src/app/api/history/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
"use server";
import { createServiceRoleClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import initClients from "@/lib/initClient";

// retrieve assistant from database
async function getAssistant(supabase: ReturnType<typeof createServiceRoleClient>) {
const { data, error } = await supabase
.from("assistants")
.select("*")
.eq("name", "StudyHelper")
.single();

if (error) {
console.error("Error fetching assistant:", error);
throw new Error("Assistant not found");
}

return data;
}
import { initClients } from "@/utils/initClients";
import { getAssistant } from "@/utils/initClients";

// create thread and store it in database
async function getOrCreateThread(
Expand Down
19 changes: 2 additions & 17 deletions nextjs/src/app/api/openai/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
"use server";
import { createServiceRoleClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import initClients from "@/lib/initClient";

// retrieve assistant from database
async function getAssistant(supabase: ReturnType<typeof createServiceRoleClient>) {
const { data, error } = await supabase
.from("assistants")
.select("*")
.eq("name", "StudyHelper")
.single();

if (error) {
console.error("Error fetching assistant:", error);
throw new Error("Assistant not found");
}

return data;
}
import { initClients } from "@/utils/initClients";
import { getAssistant } from "@/utils/initClients";

// create thread and store it in database
async function getOrCreateThread(
Expand Down
2 changes: 1 addition & 1 deletion nextjs/src/app/api/profile/cards/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { createServiceRoleClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import { CardsDifficultyType } from "@/types";
import initClients from "@/lib/initClient";
import { initClients } from "@/utils/initClients";

async function updateCards(
supabase: ReturnType<typeof createServiceRoleClient>,
Expand Down
2 changes: 1 addition & 1 deletion nextjs/src/app/api/profile/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";
import { createServiceRoleClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import initClients from "@/lib/initClient";
import { initClients } from "@/utils/initClients";

async function getProfile(supabase: ReturnType<typeof createServiceRoleClient>, userId: string) {
const { data, error } = await supabase.from("profiles").select("*").eq("id", userId);
Expand Down
2 changes: 1 addition & 1 deletion nextjs/src/app/api/results/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";
import { createServiceRoleClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import initClients from "@/lib/initClient";
import { initClients } from "@/utils/initClients";

interface Results {
result: string;
Expand Down
15 changes: 0 additions & 15 deletions nextjs/src/lib/initClient.ts

This file was deleted.

31 changes: 31 additions & 0 deletions nextjs/src/utils/initClients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createServiceRoleClient } from "@/utils/supabase/server";
import { OpenAI } from "openai";

export async function initClients() {
try {
const supabase = createServiceRoleClient();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
return { supabase, openai };
} catch (error) {
console.error("Error initializing clients:", error);
throw new Error("Failed to initialize clients");
}
}

// retrieve assistant from database
export async function getAssistant(supabase: ReturnType<typeof createServiceRoleClient>) {
const { data, error } = await supabase
.from("assistants")
.select("*")
.eq("name", "StudyHelper")
.single();

if (error) {
console.error("Error fetching assistant:", error);
throw new Error("Assistant not found");
}

return data;
}

0 comments on commit 17149f0

Please sign in to comment.