forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
75 lines (60 loc) · 2.19 KB
/
Copy pathroute.ts
File metadata and controls
75 lines (60 loc) · 2.19 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// src/app/api/settings/quicklinks/route.ts
//
// Functions: GET, PUT
import { eq } from "drizzle-orm"
import { NextResponse } from "next/server"
import { authenticate, parseJsonBody, validateMaxLength } from "@/lib/api-helpers"
import { db } from "@/lib/db"
import { appSettings } from "@/lib/db/schema"
import { QUICKLINK_SLUG_MAX, QUICKLINK_SLUGS_MAX } from "@/lib/limits"
import { log } from "@/lib/logger"
export async function GET() {
const auth = await authenticate()
if (auth instanceof NextResponse) return auth
const [settings] = await db
.select({ draftQuicklinks: appSettings.draftQuicklinks })
.from(appSettings)
.limit(1)
if (!settings) {
return NextResponse.json({ error: "Not configured" }, { status: 400 })
}
let slugs: string[] = []
if (settings.draftQuicklinks) {
try {
const parsed = JSON.parse(settings.draftQuicklinks)
if (Array.isArray(parsed) && parsed.every((s) => typeof s === "string")) {
slugs = parsed
}
} catch (err) {
log.warn({ error: String(err) }, "Corrupt draftQuicklinks JSON in DB, returning empty")
slugs = []
}
}
return NextResponse.json({ slugs })
}
export async function PUT(request: Request) {
const auth = await authenticate()
if (auth instanceof NextResponse) return auth
const body = await parseJsonBody(request)
if (body instanceof NextResponse) return body
const { slugs } = body
if (!Array.isArray(slugs) || !slugs.every((s) => typeof s === "string")) {
return NextResponse.json({ error: "slugs must be an array of strings" }, { status: 400 })
}
if (slugs.length > QUICKLINK_SLUGS_MAX) {
return NextResponse.json({ error: "Too many quicklinks (max 100)" }, { status: 400 })
}
for (const s of slugs) {
const slugErr = validateMaxLength(s, QUICKLINK_SLUG_MAX, "Slug")
if (slugErr) return slugErr
}
const [settings] = await db.select({ id: appSettings.id }).from(appSettings).limit(1)
if (!settings) {
return NextResponse.json({ error: "Not configured" }, { status: 400 })
}
await db
.update(appSettings)
.set({ draftQuicklinks: JSON.stringify(slugs) })
.where(eq(appSettings.id, settings.id))
return NextResponse.json({ slugs })
}