import type { HostApiContext } from '../context'; import type { NormalizedHostApiRequest } from '../route-utils'; import { ok } from '../route-utils'; export async function handleProviderRoutes( request: NormalizedHostApiRequest, ctx: HostApiContext, ) { const { pathname, method } = request; const parsedBody = request.body && typeof request.body === 'string' ? JSON.parse(request.body) : request.body; if (pathname === '/api/provider-vendors' && method === 'GET') { return ok(ctx.providerApiService.getVendors()); } if (pathname === '/api/provider-accounts' && method === 'GET') { return ok(ctx.providerApiService.getAccounts()); } if (pathname === '/api/providers' && method === 'GET') { return ok(ctx.providerApiService.getProviders()); } if (pathname === '/api/provider-accounts/default' && method === 'GET') { return ok(ctx.providerApiService.getDefault()); } if (pathname === '/api/provider-accounts' && method === 'POST') { return ok(ctx.providerApiService.createAccount(parsedBody || {})); } if (pathname === '/api/provider-accounts/default' && method === 'PUT') { const result = ctx.providerApiService.setDefault(parsedBody || {}); return ok(result, result.success ? 200 : 400); } if (pathname.startsWith('/api/provider-accounts/') && method === 'PUT') { const id = decodeURIComponent(pathname.replace('/api/provider-accounts/', '')); const result = ctx.providerApiService.updateAccount(id, parsedBody || {}); return ok(result, result.success ? 200 : 404); } if (pathname.startsWith('/api/provider-accounts/') && method === 'DELETE') { const id = decodeURIComponent(pathname.replace('/api/provider-accounts/', '')); return ok(ctx.providerApiService.deleteAccount(id)); } if (pathname === '/api/providers/default' && method === 'PUT') { const result = ctx.providerApiService.setDefault({ accountId: (parsedBody as any)?.providerId }); return ok(result, result.success ? 200 : 400); } if (pathname.startsWith('/api/providers/') && pathname.endsWith('/api-key') && method === 'GET') { const id = decodeURIComponent(pathname.replace('/api/providers/', '').replace('/api-key', '')); return ok(ctx.providerApiService.getApiKey(id)); } if (pathname.startsWith('/api/providers/') && method === 'PUT') { const id = decodeURIComponent(pathname.replace('/api/providers/', '')); const result = ctx.providerApiService.updateAccount(id, parsedBody || {}); return ok(result, result.success ? 200 : 404); } if (pathname.startsWith('/api/providers/') && method === 'DELETE') { const rawPath = request.path.replace('/api/providers/', ''); const [rawId, query] = rawPath.split('?'); const id = decodeURIComponent(rawId); if (query && query.includes('apiKeyOnly=1')) { return ok(ctx.providerApiService.deleteApiKey(id)); } return ok(ctx.providerApiService.deleteAccount(id)); } if (pathname === '/api/providers/validate' && method === 'POST') { return ok(await ctx.providerApiService.validateApiKey(parsedBody || {})); } if (pathname === '/api/usage/recent-token-history' && method === 'GET') { const limitRaw = request.url.searchParams.get('limit'); const limit = limitRaw ? Number(limitRaw) : undefined; return ok(await ctx.providerApiService.getUsageHistory(limit)); } return null; }