File size: 1,295 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextResponse } from "next/server";
import { z } from "zod";
import { getAllKeyGroups, createKeyGroup, getKeyGroup } from "@/lib/localDb";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";

const createKeyGroupSchema = z.object({
  name: z.string().trim().min(1, "name is required"),
  description: z.string().optional().default(""),
});

/**
 * GET /api/keys/groups — List all key groups
 */
export async function GET() {
  try {
    const groups = getAllKeyGroups();
    return NextResponse.json({ groups });
  } catch (error) {
    return NextResponse.json({ error: "Failed to list groups" }, { status: 500 });
  }
}

/**
 * POST /api/keys/groups — Create a key group
 * Body: { name, description? }
 */
export async function POST(request: Request) {
  try {
    const rawBody = await request.json();
    const validation = validateBody(createKeyGroupSchema, rawBody);
    if (isValidationFailure(validation)) {
      return NextResponse.json({ error: validation.error }, { status: 400 });
    }
    const group = createKeyGroup(validation.data.name, validation.data.description);
    return NextResponse.json({ group }, { status: 201 });
  } catch (error) {
    return NextResponse.json({ error: "Failed to create group" }, { status: 500 });
  }
}