mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-07 07:42:13 +03:00
Extracted validateBody, isValidationFailure, and loginSchema from the 935-line schemas.ts barrel file into a dedicated helpers.ts module. Updated 70 API route files to import directly from helpers.ts. Root cause: webpack on certain environments fails to resolve exports from the bottom of large barrel files (schemas.ts), causing '(0, O.Jb) is not a function' errors in production builds. Fix: Split validation helpers into a small dedicated module (helpers.ts) so webpack can correctly resolve all exports regardless of file size. - TypeScript compiles with 0 errors - All API routes updated to import from helpers.ts - schemas.ts re-exports from helpers.ts for backward compatibility
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { validateApiKey, getModelAliases } from "@/models";
|
|
import { cloudResolveAliasSchema } from "@/shared/validation/schemas";
|
|
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
|
|
|
|
// Resolve model alias to provider/model
|
|
export async function POST(request: Request) {
|
|
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 authHeader = request.headers.get("Authorization");
|
|
if (!authHeader?.startsWith("Bearer ")) {
|
|
return NextResponse.json({ error: "Missing API key" }, { status: 401 });
|
|
}
|
|
|
|
const apiKey = authHeader.slice(7);
|
|
const validation = validateBody(cloudResolveAliasSchema, rawBody);
|
|
if (isValidationFailure(validation)) {
|
|
return NextResponse.json({ error: validation.error }, { status: 400 });
|
|
}
|
|
const { alias } = validation.data;
|
|
|
|
// Validate API key
|
|
const isValid = await validateApiKey(apiKey);
|
|
if (!isValid) {
|
|
return NextResponse.json({ error: "Invalid API key" }, { status: 401 });
|
|
}
|
|
|
|
// Get model aliases
|
|
const modelAliases = await getModelAliases();
|
|
const resolvedValue = modelAliases[alias];
|
|
const resolved = typeof resolvedValue === "string" ? resolvedValue : null;
|
|
|
|
if (resolved) {
|
|
// Parse provider/model
|
|
const firstSlash = resolved.indexOf("/");
|
|
if (firstSlash > 0) {
|
|
return NextResponse.json({
|
|
alias,
|
|
provider: resolved.slice(0, firstSlash),
|
|
model: resolved.slice(firstSlash + 1),
|
|
});
|
|
}
|
|
}
|
|
|
|
// Not found
|
|
return NextResponse.json({ error: "Alias not found" }, { status: 404 });
|
|
} catch (error) {
|
|
console.log("Model resolve error:", error);
|
|
return NextResponse.json({ error: "Internal error" }, { status: 500 });
|
|
}
|
|
}
|