mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-07 07:42:13 +03:00
- Remove `new Request('http://localhost/api/keys')` default arg from GET handler in src/app/api/keys/route.ts (line 26)
- Fix api-key-reveal-route.test.mjs to pass explicit Request instead of calling GET() with no args
- Add --output-dir coverage to all c8 scripts in package.json
- Add coverage.reportsDirectory: 'coverage' to vitest.config.ts and vitest.mcp.config.ts
- Fix CHANGELOG.md structure (# Changelog + [Unreleased] to top)
- Remove 30+ stale coverage-* directories from project root
- Coverage: Statements 78.76% | Branches 72.75% | Functions 80.93% | Lines 78.76% (all thresholds passed)
106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { getApiKeys, createApiKey, isCloudEnabled, updateApiKeyPermissions } from "@/lib/localDb";
|
|
import { getConsistentMachineId } from "@/shared/utils/machineId";
|
|
import { syncToCloud } from "@/lib/cloudSync";
|
|
import { createKeySchema } from "@/shared/validation/schemas";
|
|
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
|
|
import { isApiKeyRevealEnabled, maskStoredApiKey } from "@/lib/apiKeyExposure";
|
|
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
|
|
|
|
function parsePagination(request: Request) {
|
|
const url = new URL(request.url);
|
|
const limitValue = url.searchParams.get("limit");
|
|
const offsetValue = url.searchParams.get("offset");
|
|
|
|
const parsedLimit = limitValue ? Number.parseInt(limitValue, 10) : undefined;
|
|
const parsedOffset = offsetValue ? Number.parseInt(offsetValue, 10) : 0;
|
|
|
|
const limit =
|
|
Number.isInteger(parsedLimit) && parsedLimit && parsedLimit > 0 ? parsedLimit : null;
|
|
const offset = Number.isInteger(parsedOffset) && parsedOffset > 0 ? parsedOffset : 0;
|
|
|
|
return { limit, offset };
|
|
}
|
|
|
|
// GET /api/keys - List API keys
|
|
export async function GET(request: Request) {
|
|
const authError = await requireManagementAuth(request);
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const keys = await getApiKeys();
|
|
const maskedKeys = keys.map((k) => ({
|
|
...k,
|
|
key: maskStoredApiKey(k.key),
|
|
}));
|
|
const { limit, offset } = parsePagination(request);
|
|
const pagedKeys =
|
|
limit === null ? maskedKeys.slice(offset) : maskedKeys.slice(offset, offset + limit);
|
|
|
|
return NextResponse.json({
|
|
keys: pagedKeys,
|
|
total: maskedKeys.length,
|
|
allowKeyReveal: isApiKeyRevealEnabled(),
|
|
});
|
|
} catch (error) {
|
|
console.log("Error fetching keys:", error);
|
|
return NextResponse.json({ error: "Failed to fetch keys" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// POST /api/keys - Create new API key
|
|
export async function POST(request) {
|
|
const authError = await requireManagementAuth(request);
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const body = await request.json();
|
|
|
|
// Zod validation
|
|
const validation = validateBody(createKeySchema, body);
|
|
if (isValidationFailure(validation)) {
|
|
return NextResponse.json({ error: validation.error }, { status: 400 });
|
|
}
|
|
const { name, noLog } = validation.data;
|
|
|
|
// Always get machineId from server
|
|
const machineId = await getConsistentMachineId();
|
|
const apiKey = await createApiKey(name, machineId);
|
|
if (noLog === true) {
|
|
await updateApiKeyPermissions(apiKey.id, { noLog: true });
|
|
}
|
|
|
|
// Auto sync to Cloud if enabled
|
|
await syncKeysToCloudIfEnabled();
|
|
|
|
return NextResponse.json(
|
|
{
|
|
key: apiKey.key,
|
|
name: apiKey.name,
|
|
id: apiKey.id,
|
|
machineId: apiKey.machineId,
|
|
noLog: noLog === true,
|
|
},
|
|
{ status: 201 }
|
|
);
|
|
} catch (error) {
|
|
console.log("Error creating key:", error);
|
|
return NextResponse.json({ error: "Failed to create key" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sync API keys to Cloud if enabled
|
|
*/
|
|
async function syncKeysToCloudIfEnabled() {
|
|
try {
|
|
const cloudEnabled = await isCloudEnabled();
|
|
if (!cloudEnabled) return;
|
|
|
|
const machineId = await getConsistentMachineId();
|
|
await syncToCloud(machineId);
|
|
} catch (error) {
|
|
console.log("Error syncing keys to cloud:", error);
|
|
}
|
|
}
|