Files
DataStack/app/api/clients/route.ts
T

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-10-23 19:56:38 -07:00
import { SDKManager } from "@/core/sdk/sdkManager";
import { NextRequest, NextResponse } from "next/server";
2026-10-23 11:43:49 -05:00
export async function GET() {
try {
const allClients = await SDKManager.listClientsAsync();
const clients = allClients.map(({ secret, ...client }) => {
void secret;
return client;
});
2026-10-23 11:43:49 -05:00
return NextResponse.json({ clients, count: clients.length });
} catch (err: unknown) {
2026-10-23 19:56:38 -07:00
const message =
err instanceof Error ? err.message : "Failed to list clients";
2026-10-23 11:43:49 -05:00
return NextResponse.json({ error: message }, { status: 500 });
}
}
export async function POST(req: NextRequest) {
try {
const body = await req.json();
const { clientId, name, rateLimit } = body;
if (!clientId || !name) {
return NextResponse.json(
2026-10-23 19:56:38 -07:00
{ error: "clientId and name are required" },
{ status: 400 },
2026-10-23 11:43:49 -05:00
);
}
const safeClientId = String(clientId)
.trim()
.toLowerCase()
.replace(/[^a-z0-9_-]/g, "_");
const existing = await SDKManager.getClientAsync(safeClientId);
2026-10-23 11:43:49 -05:00
if (existing) {
return NextResponse.json(
{ error: `Client '${safeClientId}' already exists` },
2026-10-23 19:56:38 -07:00
{ status: 409 },
2026-10-23 11:43:49 -05:00
);
}
const newClient = await SDKManager.registerClientAsync({
clientId: safeClientId,
name: String(name),
2026-10-23 11:43:49 -05:00
rateLimit: rateLimit ? Number(rateLimit) : 100,
});
return NextResponse.json({ client: newClient }, { status: 201 });
} catch (err: unknown) {
2026-10-23 19:56:38 -07:00
const message =
err instanceof Error ? err.message : "Client creation failed";
2026-10-23 11:43:49 -05:00
return NextResponse.json({ error: message }, { status: 500 });
}
}