-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathroute.js
More file actions
57 lines (50 loc) · 1.56 KB
/
Copy pathroute.js
File metadata and controls
57 lines (50 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NextResponse } from "next/server"
import { withAuth } from "@lib/api-utils"
const appServerUrl =
process.env.NEXT_PUBLIC_ENVIRONMENT === "selfhost"
? process.env.INTERNAL_APP_SERVER_URL
: process.env.NEXT_PUBLIC_APP_SERVER_URL
export const PUT = withAuth(async function PUT(
request,
{ params, authHeader }
) {
const { memoryId } = params
const backendUrl = new URL(`${appServerUrl}/memories/${memoryId}`)
try {
const body = await request.json()
const response = await fetch(backendUrl.toString(), {
method: "PUT",
headers: { "Content-Type": "application/json", ...authHeader },
body: JSON.stringify(body)
})
const data = await response.json()
if (!response.ok) {
throw new Error(data.detail || "Failed to update memory")
}
return NextResponse.json(data)
} catch (error) {
console.error(`API Error in /memories/${memoryId} (PUT):`, error)
return NextResponse.json({ error: error.message }, { status: 500 })
}
})
export const DELETE = withAuth(async function DELETE(
request,
{ params, authHeader }
) {
const { memoryId } = params
const backendUrl = new URL(`${appServerUrl}/memories/${memoryId}`)
try {
const response = await fetch(backendUrl.toString(), {
method: "DELETE",
headers: { ...authHeader }
})
const data = await response.json()
if (!response.ok) {
throw new Error(data.detail || "Failed to delete memory")
}
return NextResponse.json(data)
} catch (error) {
console.error(`API Error in /memories/${memoryId} (DELETE):`, error)
return NextResponse.json({ error: error.message }, { status: 500 })
}
})