Skip to content

Commit 2f0421b

Browse files
authored
feat: add delete api keys, add openai provider (#196)
* feat: add delete api key * feat: add openai provider
1 parent a7f706f commit 2f0421b

File tree

6 files changed

+305
-67
lines changed

6 files changed

+305
-67
lines changed

app/api/models/route.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
getAllModels,
3-
getAllOpenRouterModels,
3+
getModelsForUserProviders,
44
getModelsWithAccessFlags,
55
refreshModelsCache,
66
} from "@/lib/models"
@@ -53,10 +53,10 @@ export async function GET() {
5353
})
5454
}
5555

56-
const hasOpenRouterKey = data?.some((k) => k.provider === "openrouter")
56+
const userProviders = data?.map((k) => k.provider) || []
5757

58-
if (hasOpenRouterKey) {
59-
const models = await getAllOpenRouterModels()
58+
if (userProviders.length === 0) {
59+
const models = await getModelsWithAccessFlags()
6060
return new Response(JSON.stringify({ models }), {
6161
status: 200,
6262
headers: {
@@ -65,7 +65,7 @@ export async function GET() {
6565
})
6666
}
6767

68-
const models = await getModelsWithAccessFlags()
68+
const models = await getModelsForUserProviders(userProviders)
6969

7070
return new Response(JSON.stringify({ models }), {
7171
status: 200,
@@ -86,7 +86,6 @@ export async function GET() {
8686

8787
export async function POST() {
8888
try {
89-
// Refresh the models cache
9089
refreshModelsCache()
9190
const models = await getAllModels()
9291

app/api/user-key-status/route.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { createClient } from "@/lib/supabase/server"
22
import { NextResponse } from "next/server"
33

4+
// Supported providers
5+
const SUPPORTED_PROVIDERS = ["openrouter", "openai"]
6+
47
export async function GET() {
58
try {
69
const supabase = await createClient()
@@ -26,9 +29,17 @@ export async function GET() {
2629
return NextResponse.json({ error: error.message }, { status: 500 })
2730
}
2831

29-
const hasOpenRouterKey = data?.some((k) => k.provider === "openrouter")
32+
// Create status object for all supported providers
33+
const userProviders = data?.map((k) => k.provider) || []
34+
const providerStatus = SUPPORTED_PROVIDERS.reduce(
35+
(acc, provider) => {
36+
acc[provider] = userProviders.includes(provider)
37+
return acc
38+
},
39+
{} as Record<string, boolean>
40+
)
3041

31-
return NextResponse.json({ openrouter: hasOpenRouterKey })
42+
return NextResponse.json(providerStatus)
3243
} catch (err) {
3344
console.error("Key status error:", err)
3445
return NextResponse.json(

app/api/user-keys/route.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,47 @@ export async function POST(request: Request) {
4949
)
5050
}
5151
}
52+
53+
export async function DELETE(request: Request) {
54+
try {
55+
const { provider } = await request.json()
56+
57+
if (!provider) {
58+
return NextResponse.json(
59+
{ error: "Provider is required" },
60+
{ status: 400 }
61+
)
62+
}
63+
64+
const supabase = await createClient()
65+
if (!supabase) {
66+
return NextResponse.json(
67+
{ error: "Supabase not available" },
68+
{ status: 500 }
69+
)
70+
}
71+
72+
const { data: authData } = await supabase.auth.getUser()
73+
if (!authData?.user?.id) {
74+
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
75+
}
76+
77+
const { error } = await supabase
78+
.from("user_keys")
79+
.delete()
80+
.eq("user_id", authData.user.id)
81+
.eq("provider", provider)
82+
83+
if (error) {
84+
return NextResponse.json({ error: error.message }, { status: 500 })
85+
}
86+
87+
return NextResponse.json({ success: true })
88+
} catch (error) {
89+
console.error("Error in DELETE /api/user-keys:", error)
90+
return NextResponse.json(
91+
{ error: "Internal server error" },
92+
{ status: 500 }
93+
)
94+
}
95+
}

0 commit comments

Comments
 (0)