mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-05 06:42:12 +03:00
chore(ts): wave 4b — type 7 more API routes (providers, test, usage, nodes)
Files typed: - providers/route.ts (POST Request, providerSpecificData typed) - providers/[id]/test/route.ts (13 functions typed, runtime casts) - usage/call-logs/route.ts (GET Request, filter Record) - usage/proxy-logs/route.ts (GET Request, filters Record, error casts) - oauth/cursor/auto-import/route.ts (tokens Record, error casts) - usage/[connectionId]/route.ts (GET Request+params, updateData Record) - provider-nodes/[id]/route.ts (PUT/DELETE Request+params, updates Record) TS errors: 347 → 313 (-34)
This commit is contained in:
@@ -41,7 +41,7 @@ export async function GET() {
|
||||
.prepare("SELECT key, value FROM itemTable WHERE key IN (?, ?)")
|
||||
.all("cursorAuth/accessToken", "storage.serviceMachineId");
|
||||
|
||||
const tokens = {};
|
||||
const tokens: Record<string, any> = {};
|
||||
for (const row of rows) {
|
||||
if (row.key === "cursorAuth/accessToken") {
|
||||
tokens.accessToken = row.value;
|
||||
@@ -69,11 +69,11 @@ export async function GET() {
|
||||
db?.close();
|
||||
return NextResponse.json({
|
||||
found: false,
|
||||
error: `Failed to read database: ${error.message}`,
|
||||
error: `Failed to read database: ${(error as any).message}`,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Cursor auto-import error:", error);
|
||||
return NextResponse.json({ found: false, error: error.message }, { status: 500 });
|
||||
return NextResponse.json({ found: false, error: (error as any).message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from "@/models";
|
||||
|
||||
// PUT /api/provider-nodes/[id] - Update provider node
|
||||
export async function PUT(request, { params }) {
|
||||
export async function PUT(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
@@ -50,7 +50,7 @@ export async function PUT(request, { params }) {
|
||||
}
|
||||
}
|
||||
|
||||
const updates = {
|
||||
const updates: Record<string, any> = {
|
||||
name: name.trim(),
|
||||
prefix: prefix.trim(),
|
||||
baseUrl: sanitizedBaseUrl,
|
||||
@@ -85,7 +85,7 @@ export async function PUT(request, { params }) {
|
||||
}
|
||||
|
||||
// DELETE /api/provider-nodes/[id] - Delete provider node and its connections
|
||||
export async function DELETE(request, { params }) {
|
||||
export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const node = await getProviderNodeById(id);
|
||||
|
||||
@@ -91,13 +91,13 @@ const CLI_RUNTIME_PROVIDER_MAP = {
|
||||
kilocode: "kilo",
|
||||
};
|
||||
|
||||
function toSafeMessage(value, fallback = "Unknown error") {
|
||||
function toSafeMessage(value: any, fallback = "Unknown error"): string {
|
||||
if (typeof value !== "string") return fallback;
|
||||
const trimmed = value.trim();
|
||||
return trimmed || fallback;
|
||||
}
|
||||
|
||||
function makeDiagnosis(type, source, message, code = null) {
|
||||
function makeDiagnosis(type: string, source: string, message: string | null, code: string | null = null) {
|
||||
return {
|
||||
type,
|
||||
source,
|
||||
@@ -106,7 +106,7 @@ function makeDiagnosis(type, source, message, code = null) {
|
||||
};
|
||||
}
|
||||
|
||||
function classifyFailure({ error, statusCode = null, refreshFailed = false, unsupported = false }) {
|
||||
function classifyFailure({ error, statusCode = null, refreshFailed = false, unsupported = false }: { error: string; statusCode?: number | null; refreshFailed?: boolean; unsupported?: boolean }) {
|
||||
const message = toSafeMessage(error, "Connection test failed");
|
||||
const normalized = message.toLowerCase();
|
||||
const numericStatus = Number.isFinite(statusCode) ? Number(statusCode) : null;
|
||||
@@ -183,7 +183,7 @@ function classifyFailure({ error, statusCode = null, refreshFailed = false, unsu
|
||||
);
|
||||
}
|
||||
|
||||
async function getProviderRuntimeStatus(provider) {
|
||||
async function getProviderRuntimeStatus(provider: string) {
|
||||
const toolId = CLI_RUNTIME_PROVIDER_MAP[provider];
|
||||
if (!toolId) return null;
|
||||
|
||||
@@ -208,7 +208,7 @@ async function getProviderRuntimeStatus(provider) {
|
||||
error: runtimeMessage,
|
||||
};
|
||||
} catch (error) {
|
||||
const runtimeMessage = `Failed to check local CLI runtime: ${error?.message || "runtime_check_failed"}`;
|
||||
const runtimeMessage = `Failed to check local CLI runtime: ${(error as any)?.message || "runtime_check_failed"}`;
|
||||
return {
|
||||
installed: false,
|
||||
runnable: false,
|
||||
@@ -227,7 +227,7 @@ async function getProviderRuntimeStatus(provider) {
|
||||
*
|
||||
* @returns {object} { accessToken, expiresIn, refreshToken } or null if failed
|
||||
*/
|
||||
async function refreshOAuthToken(connection) {
|
||||
async function refreshOAuthToken(connection: any) {
|
||||
const { provider, refreshToken } = connection;
|
||||
if (!refreshToken) return null;
|
||||
|
||||
@@ -241,7 +241,7 @@ async function refreshOAuthToken(connection) {
|
||||
const result = await getAccessToken(provider, credentials, console);
|
||||
return result; // { accessToken, expiresIn, refreshToken } or null
|
||||
} catch (err) {
|
||||
console.log(`Error refreshing ${provider} token:`, err.message);
|
||||
console.log(`Error refreshing ${provider} token:`, (err as any).message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -249,7 +249,7 @@ async function refreshOAuthToken(connection) {
|
||||
/**
|
||||
* Check if token is expired or about to expire (within 5 minutes)
|
||||
*/
|
||||
function isTokenExpired(connection) {
|
||||
function isTokenExpired(connection: any) {
|
||||
const expiresAtValue = connection.expiresAt || connection.tokenExpiresAt;
|
||||
if (!expiresAtValue) return false;
|
||||
const expiresAt = new Date(expiresAtValue).getTime();
|
||||
@@ -277,7 +277,7 @@ async function syncToCloudIfEnabled() {
|
||||
* Auto-refreshes token if expired
|
||||
* @returns {{ valid: boolean, error: string|null, refreshed: boolean, newTokens: object|null }}
|
||||
*/
|
||||
async function testOAuthConnection(connection) {
|
||||
async function testOAuthConnection(connection: any) {
|
||||
const config = OAUTH_TEST_CONFIG[connection.provider];
|
||||
|
||||
if (!config) {
|
||||
@@ -467,7 +467,7 @@ async function testOAuthConnection(connection) {
|
||||
/**
|
||||
* Test API key connection
|
||||
*/
|
||||
async function testApiKeyConnection(connection) {
|
||||
async function testApiKeyConnection(connection: any) {
|
||||
if (!connection.apiKey) {
|
||||
const error = "Missing API key";
|
||||
return {
|
||||
@@ -509,7 +509,7 @@ async function testApiKeyConnection(connection) {
|
||||
* @param {string} connectionId
|
||||
* @returns {Promise<object>} Test result (same shape as the JSON response)
|
||||
*/
|
||||
export async function testSingleConnection(connectionId) {
|
||||
export async function testSingleConnection(connectionId: string) {
|
||||
const connection = await getProviderConnectionById(connectionId);
|
||||
|
||||
if (!connection) {
|
||||
@@ -520,12 +520,12 @@ export async function testSingleConnection(connectionId) {
|
||||
const startTime = Date.now();
|
||||
const runtime = await getProviderRuntimeStatus(connection.provider);
|
||||
|
||||
if (runtime?.diagnosis) {
|
||||
if ((runtime as any)?.diagnosis) {
|
||||
result = {
|
||||
valid: false,
|
||||
error: runtime.error,
|
||||
error: (runtime as any).error,
|
||||
refreshed: false,
|
||||
diagnosis: runtime.diagnosis,
|
||||
diagnosis: (runtime as any).diagnosis,
|
||||
};
|
||||
} else if (connection.authType === "apikey") {
|
||||
result = await testApiKeyConnection(connection);
|
||||
@@ -543,7 +543,7 @@ export async function testSingleConnection(connectionId) {
|
||||
? makeDiagnosis("ok", "local", null, null)
|
||||
: classifyFailure({ error: result.error, statusCode: result.statusCode }));
|
||||
|
||||
const updateData = {
|
||||
const updateData: Record<string, any> = {
|
||||
testStatus: result.valid ? "active" : "error",
|
||||
lastError: result.valid ? null : result.error,
|
||||
lastErrorAt: result.valid ? null : now,
|
||||
@@ -624,7 +624,7 @@ export async function testSingleConnection(connectionId) {
|
||||
}
|
||||
|
||||
// POST /api/providers/[id]/test - Test connection
|
||||
export async function POST(request, { params }) {
|
||||
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const data = await testSingleConnection(id);
|
||||
|
||||
@@ -36,7 +36,7 @@ export async function GET() {
|
||||
}
|
||||
|
||||
// POST /api/providers - Create new connection (API Key only, OAuth via separate flow)
|
||||
export async function POST(request) {
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
@@ -58,7 +58,7 @@ export async function POST(request) {
|
||||
return NextResponse.json({ error: "Invalid provider" }, { status: 400 });
|
||||
}
|
||||
|
||||
let providerSpecificData = null;
|
||||
let providerSpecificData: Record<string, any> | null = null;
|
||||
const allowMultipleCompatibleConnections =
|
||||
process.env.ALLOW_MULTI_CONNECTIONS_PER_COMPAT_NODE === "true";
|
||||
|
||||
@@ -117,7 +117,7 @@ export async function POST(request) {
|
||||
});
|
||||
|
||||
// Hide sensitive fields
|
||||
const result = { ...newConnection };
|
||||
const result: Record<string, any> = { ...newConnection };
|
||||
delete result.apiKey;
|
||||
|
||||
// Auto sync to Cloud if enabled
|
||||
|
||||
@@ -21,7 +21,7 @@ async function syncToCloudIfEnabled() {
|
||||
* Refresh credentials using executor and update database
|
||||
* @returns {{ connection, refreshed: boolean }}
|
||||
*/
|
||||
async function refreshAndUpdateCredentials(connection) {
|
||||
async function refreshAndUpdateCredentials(connection: any) {
|
||||
const executor = getExecutor(connection.provider);
|
||||
|
||||
// Build credentials object from connection
|
||||
@@ -55,7 +55,7 @@ async function refreshAndUpdateCredentials(connection) {
|
||||
|
||||
// Build update object
|
||||
const now = new Date().toISOString();
|
||||
const updateData = {
|
||||
const updateData: Record<string, any> = {
|
||||
updatedAt: now,
|
||||
};
|
||||
|
||||
@@ -103,7 +103,7 @@ async function refreshAndUpdateCredentials(connection) {
|
||||
/**
|
||||
* GET /api/usage/[connectionId] - Get usage data for a specific connection
|
||||
*/
|
||||
export async function GET(request, { params }) {
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ connectionId: string }> }) {
|
||||
try {
|
||||
const { connectionId } = await params;
|
||||
|
||||
@@ -133,7 +133,7 @@ export async function GET(request, { params }) {
|
||||
console.error("[Usage API] Credential refresh failed:", refreshError);
|
||||
return Response.json(
|
||||
{
|
||||
error: `Credential refresh failed: ${refreshError.message}`,
|
||||
error: `Credential refresh failed: ${(refreshError as any).message}`,
|
||||
},
|
||||
{ status: 401 }
|
||||
);
|
||||
@@ -144,7 +144,7 @@ export async function GET(request, { params }) {
|
||||
return Response.json(usage);
|
||||
} catch (error) {
|
||||
console.error("[Usage API] Error fetching usage:", error);
|
||||
console.error("[Usage API] Error stack:", error.stack);
|
||||
return Response.json({ error: error.message }, { status: 500 });
|
||||
console.error("[Usage API] Error stack:", (error as any).stack);
|
||||
return Response.json({ error: (error as any).message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getCallLogs } from "@/lib/usageDb";
|
||||
|
||||
export async function GET(request) {
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
const filter = {};
|
||||
const filter: Record<string, any> = {};
|
||||
if (searchParams.get("status")) filter.status = searchParams.get("status");
|
||||
if (searchParams.get("model")) filter.model = searchParams.get("model");
|
||||
if (searchParams.get("provider")) filter.provider = searchParams.get("provider");
|
||||
|
||||
@@ -4,11 +4,11 @@ import { getProxyLogs, clearProxyLogs, getProxyLogStats } from "@/lib/proxyLogge
|
||||
* GET /api/usage/proxy-logs — get proxy usage logs
|
||||
* Query params: ?status=ok|error|timeout&type=http|socks5&provider=xxx&level=global|provider|combo|key&search=xxx&limit=300
|
||||
*/
|
||||
export async function GET(request) {
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
const filters = {};
|
||||
const filters: Record<string, any> = {};
|
||||
if (searchParams.get("status")) filters.status = searchParams.get("status");
|
||||
if (searchParams.get("type")) filters.type = searchParams.get("type");
|
||||
if (searchParams.get("provider")) filters.provider = searchParams.get("provider");
|
||||
@@ -20,7 +20,7 @@ export async function GET(request) {
|
||||
return Response.json(logs);
|
||||
} catch (error) {
|
||||
return Response.json(
|
||||
{ error: { message: error.message, type: "server_error" } },
|
||||
{ error: { message: (error as any).message, type: "server_error" } },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
@@ -35,7 +35,7 @@ export async function DELETE() {
|
||||
return Response.json({ cleared: true });
|
||||
} catch (error) {
|
||||
return Response.json(
|
||||
{ error: { message: error.message, type: "server_error" } },
|
||||
{ error: { message: (error as any).message, type: "server_error" } },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user