7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.
TypeScript
fix(analytics): a repeated provider/status param is one filter, not a 500 (#1597)
tashdroid54991ee085d0
diff --git a/server/src/__tests__/routes/analytics.test.ts b/server/src/__tests__/routes/analytics.test.tsindex 42e24db6..1d9eef0f 100644--- a/server/src/__tests__/routes/analytics.test.ts+++ b/server/src/__tests__/routes/analytics.test.ts@@ -846,5 +846,19 @@ describe('Analytics API', () => {expect(res.status).toBe(400);}});++ it('takes the FIRST value of a repeated provider/status param instead of crashing', async () => {+ // Express hands back an ARRAY for a repeated query param. The provider+ // shape check then dies on `provider.startsWith(...)` — arrays have no+ // such method — and the dashboard's recent-calls panel shows a 500 for+ // what is at worst a client bug. Same contract as routes/logs.ts:+ // first value wins, repeated params are not two filters.+ const dup = await request(app, '/api/analytics/requests?range=24h&provider=groq&provider=openrouter');+ expect(dup.status).toBe(200);+ const badSecond = await request(app, '/api/analytics/requests?range=24h&provider=groq&provider=' + encodeURIComponent('has spaces'));+ expect(badSecond.status).toBe(200);+ const dupStatus = await request(app, '/api/analytics/requests?range=24h&status=success&status=error');+ expect(dupStatus.status).toBe(200);+ });});});diff --git a/server/src/routes/analytics.ts b/server/src/routes/analytics.tsindex d6578c52..6a884607 100644--- a/server/src/routes/analytics.ts+++ b/server/src/routes/analytics.ts@@ -25,6 +25,15 @@ const ENDPOINT_ID_SQL = "COALESCE(rtrim(trim(k.base_url), '/'), '')";const toSqliteDateTime = (timestamp: number) =>new Date(timestamp).toISOString().slice(0, 19).replace('T', ' ');+// First value only — Express hands back an array for a repeated query param,+// and a repeated filter is a client bug, not two filters. Same contract as+// routes/logs.ts.+function firstParam(value: unknown): string | undefined {+ if (typeof value === 'string') return value;+ if (Array.isArray(value) && typeof value[0] === 'string') return value[0];+ return undefined;+}+// Return the rolling cutoff timestamp for the selected analytics range.function getSinceTimestamp(range: string): string {const now = Date.now();@@ -616,9 +625,11 @@ analyticsRouter.get('/requests', (req: Request, res: Response) => {const limit = Math.min(Math.max(parseInt(req.query.limit as string, 10) || 100, 1), 500);const offset = Math.max(parseInt(req.query.offset as string, 10) || 0, 0);- // Optional filters. All are validated (whitelist / shape) and applied as- // bound parameters; absent filters keep the default behavior identical.- const status = req.query.status as string | undefined;+ // First value only, like routes/logs.ts: Express hands back an ARRAY for a+ // repeated query param, and the provider shape check below dies on+ // `provider.startsWith(...)` — arrays have no such method — turning a client+ // bug into a 500 on the dashboard's recent-calls panel.+ const status = firstParam(req.query.status);if (status !== undefined && status !== 'success' && status !== 'error' && status !== 'canceled') {res.status(400).json({ error: "invalid status filter (expected 'success', 'error' or 'canceled')" });return;@@ -629,8 +640,8 @@ analyticsRouter.get('/requests', (req: Request, res: Response) => {// endpoint's base_url is what actually selects one). The legacy `platform`// param still works for old clients. Both are bound parameters, never// interpolated, so the only validation needed is shape.- const provider = req.query.provider as string | undefined;- const platform = req.query.platform as string | undefined;+ const provider = firstParam(req.query.provider);+ const platform = firstParam(req.query.platform);let providerFilterSql = '';const providerFilterParams: string[] = [];if (provider !== undefined) {