Spaces:
Runtime error
Runtime error
File size: 3,055 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 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import { NextResponse } from "next/server";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import {
getPricing,
getPricingWithSources,
updatePricing,
resetPricing,
resetAllPricing,
} from "@/lib/localDb";
import { updatePricingSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
/**
* GET /api/pricing
* Get current pricing configuration (merged user + defaults)
*/
export async function GET(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
const includeSources = new URL(request.url).searchParams.get("includeSources") === "1";
if (includeSources) {
return NextResponse.json(await getPricingWithSources());
}
const pricing = await getPricing();
return NextResponse.json(pricing);
} catch (error) {
console.error("Error fetching pricing:", error);
return NextResponse.json({ error: "Failed to fetch pricing" }, { status: 500 });
}
}
/**
* PATCH /api/pricing
* Update pricing configuration
* Body: { provider: { model: { input: number, output: number, cached: number, ... } } }
*/
export async function PATCH(request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
let rawBody;
try {
rawBody = await request.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}
try {
const validation = validateBody(updatePricingSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const body = validation.data;
const updatedPricing = await updatePricing(body);
return NextResponse.json(updatedPricing);
} catch (error) {
console.error("Error updating pricing:", error);
return NextResponse.json({ error: "Failed to update pricing" }, { status: 500 });
}
}
/**
* DELETE /api/pricing
* Reset pricing to defaults
* Query params: ?provider=xxx&model=yyy (optional)
*/
export async function DELETE(request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
const { searchParams } = new URL(request.url);
const provider = searchParams.get("provider");
const model = searchParams.get("model");
if (provider && model) {
// Reset specific model
await resetPricing(provider, model);
} else if (provider) {
// Reset entire provider
await resetPricing(provider);
} else {
// Reset all pricing
await resetAllPricing();
}
const pricing = await getPricing();
return NextResponse.json(pricing);
} catch (error) {
console.error("Error resetting pricing:", error);
return NextResponse.json({ error: "Failed to reset pricing" }, { status: 500 });
}
}
|