Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
feat(quota): GET /api/quota/keys/[id]/models — preview the qtSd/ models a key sees
diegosouzapw342f12b77ab5
diff --git a/src/app/api/quota/keys/[id]/models/route.ts b/src/app/api/quota/keys/[id]/models/route.tsnew file mode 100644index 0000000000..d4297cfa51--- /dev/null+++ b/src/app/api/quota/keys/[id]/models/route.ts@@ -0,0 +1,70 @@+/**+ * GET /api/quota/keys/[id]/models+ *+ * Returns the `qtSd/` virtual model IDs that a given API key would see in+ * /v1/models, by reusing the SAME catalog.ts approach:+ * resolveQuotaKeyScope(key.allowedQuotas) → scope.poolSlugs+ * filterModelsToQuotaPools(candidates, scope.poolSlugs)+ *+ * Candidates are the combo names from getCombos() mapped to { id: combo.name }+ * (same shape catalog.ts passes to filterModelsToQuotaPools).+ *+ * Auth: requireManagementAuth (management-gated, not quota-key-gated).+ * Error sanitization: buildErrorBody (Hard Rule #12).+ * NOT LOCAL_ONLY — does not spawn processes.+ *+ * Part of: Task 2 — quota-share-v2 plan (2026-06-01).+ */++import { NextResponse } from "next/server";+import { buildErrorBody } from "@omniroute/open-sse/utils/error";+import { requireManagementAuth } from "@/lib/api/requireManagementAuth";+import { getApiKeyById, getCombos } from "@/lib/localDb";+import { resolveQuotaKeyScope } from "@/lib/quota/quotaKey";+import { filterModelsToQuotaPools } from "@/lib/quota/quotaCombos";++export const dynamic = "force-dynamic";++type RouteParams = { params: Promise<{ id: string }> };++export async function GET(request: Request, { params }: RouteParams): Promise<Response> {+ const authError = await requireManagementAuth(request);+ if (authError) return authError;++ try {+ const { id } = await params;++ const key = await getApiKeyById(id);+ if (!key) {+ return NextResponse.json(buildErrorBody(404, "API key not found"), { status: 404 });+ }++ const allowedQuotas: string[] = Array.isArray(+ (key as Record<string, unknown>).allowedQuotas,+ )+ ? ((key as Record<string, unknown>).allowedQuotas as string[])+ : [];++ const scope = await resolveQuotaKeyScope(allowedQuotas);++ // Build the candidate list from all combo names — same shape catalog.ts uses:+ // objects with an `id` field (combo.name) passed to filterModelsToQuotaPools.+ let allCombos: Awaited<ReturnType<typeof getCombos>> = [];+ try {+ allCombos = await getCombos();+ } catch {+ // DB unavailable — treat as empty combo list; models will be [].+ }++ const candidates = allCombos+ .filter((c) => typeof c.name === "string" && c.name.length > 0)+ .map((c) => ({ id: c.name as string }));++ const models = filterModelsToQuotaPools(candidates, scope.poolSlugs).map((m) => m.id);++ return NextResponse.json({ models });+ } catch (err) {+ const message = err instanceof Error ? err.message : "Failed to resolve key models";+ return NextResponse.json(buildErrorBody(500, message), { status: 500 });+ }+}diff --git a/tests/unit/quota-key-models-route.test.ts b/tests/unit/quota-key-models-route.test.tsnew file mode 100644index 0000000000..bb71a649ec--- /dev/null+++ b/tests/unit/quota-key-models-route.test.ts@@ -0,0 +1,200 @@+/**+ * tests/unit/quota-key-models-route.test.ts+ *+ * Task 2 (quota-share-v2) — source-level assertions for:+ * GET /api/quota/keys/[id]/models+ *+ * Uses the same source-scan technique as:+ * tests/unit/quota-groups-route.test.ts+ * tests/unit/quota-pool-log-route.test.ts+ *+ * This allows the test to run with the Node.js native test runner without a+ * Next.js / DOM setup, while providing strong structural coverage of:+ * - Auth guard pattern (requireManagementAuth, 401 gate)+ * - Error sanitization (buildErrorBody, no raw err.stack / err.message leaks)+ * - Quota model resolution imports (resolveQuotaKeyScope, filterModelsToQuotaPools)+ * - Response shape ({ models })+ * - 404 on missing key (buildErrorBody(404, ...))+ * - force-dynamic export+ * - await params (Next 16 async params pattern)+ * - DB imports sourced from @/lib/localDb+ *+ * The behavioral path (allowedQuotas → scope → filterModelsToQuotaPools → models)+ * is covered by:+ * - tests/unit/quota-catalog-filter.test.ts — filterModelsToQuotaPools unit tests+ * - tests/unit/quota-key-resolve.test.ts — resolveQuotaKeyScope unit tests+ * - tests/unit/quota-group-scope.test.ts — group-scoped pool resolution+ * Re-testing the same logic inline here would duplicate DB-setup boilerplate+ * without additional signal. Source-scan matches the project's established+ * convention for route test files (quota-groups-route.test.ts, etc.).+ */++import test from "node:test";+import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import { join, dirname } from "node:path";+import { fileURLToPath } from "node:url";++const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..");++const ROUTE_PATH = join(ROOT, "src/app/api/quota/keys/[id]/models/route.ts");++const src = readFileSync(ROUTE_PATH, "utf8");++// ── Auth guard ────────────────────────────────────────────────────────────────++test("quota/keys/[id]/models: imports requireManagementAuth", () => {+ assert.ok(+ src.includes("requireManagementAuth"),+ "route must import and call requireManagementAuth",+ );+});++test("quota/keys/[id]/models: GET calls requireManagementAuth before data access", () => {+ const getIdx = src.indexOf("export async function GET");+ assert.ok(getIdx >= 0, "GET handler must exist");+ const getBody = src.slice(getIdx);+ const authIdx = getBody.indexOf("requireManagementAuth(request)");+ const dataIdx = getBody.indexOf("getApiKeyById(");+ assert.ok(authIdx >= 0, "requireManagementAuth call must be present in GET handler");+ assert.ok(dataIdx >= 0, "getApiKeyById call must be present in GET handler");+ assert.ok(authIdx < dataIdx, "auth check must come before getApiKeyById call");+});++test("quota/keys/[id]/models: GET returns early when authError is truthy", () => {+ const getIdx = src.indexOf("export async function GET");+ const getBody = src.slice(getIdx);+ assert.ok(+ getBody.includes("if (authError) return authError"),+ "GET must return authError immediately — 401 without auth",+ );+});++// ── Error sanitization ────────────────────────────────────────────────────────++test("quota/keys/[id]/models: imports buildErrorBody from @omniroute/open-sse/utils/error", () => {+ assert.ok(+ src.includes("buildErrorBody"),+ "route must use buildErrorBody — Hard Rule #12",+ );+ assert.ok(+ src.includes("@omniroute/open-sse/utils/error"),+ "route must import buildErrorBody from @omniroute/open-sse/utils/error",+ );+});++test("quota/keys/[id]/models: does NOT leak raw err.stack in response", () => {+ assert.ok(!src.includes("err.stack"), "route must not leak err.stack in response body");+});++test("quota/keys/[id]/models: does NOT leak raw err.message outside buildErrorBody", () => {+ // The only use of err.message must be inside the buildErrorBody(...) call, never+ // passed raw to NextResponse.json() or similar.+ const rawMessageLeak = /NextResponse\.json\s*\(\s*err\.message/.test(src);+ assert.ok(!rawMessageLeak, "route must not pass raw err.message to NextResponse.json");+});++// ── Quota resolution imports ──────────────────────────────────────────────────++test("quota/keys/[id]/models: imports resolveQuotaKeyScope from @/lib/quota/quotaKey", () => {+ assert.ok(+ src.includes("resolveQuotaKeyScope"),+ "route must call resolveQuotaKeyScope to compute pool scope",+ );+ assert.ok(+ src.includes("@/lib/quota/quotaKey"),+ "route must import resolveQuotaKeyScope from @/lib/quota/quotaKey",+ );+});++test("quota/keys/[id]/models: imports filterModelsToQuotaPools from @/lib/quota/quotaCombos", () => {+ assert.ok(+ src.includes("filterModelsToQuotaPools"),+ "route must call filterModelsToQuotaPools to filter combo candidates",+ );+ assert.ok(+ src.includes("@/lib/quota/quotaCombos"),+ "route must import filterModelsToQuotaPools from @/lib/quota/quotaCombos",+ );+});++test("quota/keys/[id]/models: passes scope.poolSlugs to filterModelsToQuotaPools", () => {+ assert.ok(+ src.includes("scope.poolSlugs"),+ "route must pass scope.poolSlugs to filterModelsToQuotaPools (same as catalog.ts approach)",+ );+});++// ── DB imports from localDb ───────────────────────────────────────────────────++test("quota/keys/[id]/models: imports getApiKeyById from @/lib/localDb", () => {+ assert.ok(src.includes("getApiKeyById"), "route must call getApiKeyById");+ assert.ok(+ src.includes("@/lib/localDb"),+ "route must import from @/lib/localDb (not from @/lib/db/apiKeys directly)",+ );+});++test("quota/keys/[id]/models: imports getCombos from @/lib/localDb", () => {+ assert.ok(src.includes("getCombos"), "route must call getCombos to build combo candidates");+});++// ── Response shape ────────────────────────────────────────────────────────────++test("quota/keys/[id]/models: GET returns { models } shape", () => {+ assert.ok(+ src.includes("{ models }") || src.includes("{models}") || src.includes("models:"),+ "GET must return { models } in the response body",+ );+});++test("quota/keys/[id]/models: maps filtered combos to model id strings (.map(m => m.id))", () => {+ assert.ok(+ src.includes(".map(") && src.includes("m.id"),+ "route must map filtered combo entries to their id strings",+ );+});++// ── 404 on missing key ────────────────────────────────────────────────────────++test("quota/keys/[id]/models: returns buildErrorBody(404, ...) when key not found", () => {+ assert.ok(+ src.includes("buildErrorBody(404"),+ "route must use buildErrorBody(404, ...) when the API key is not found",+ );+ assert.ok(src.includes("404"), "route must return HTTP 404 on missing key");+});++// ── force-dynamic ─────────────────────────────────────────────────────────────++test("quota/keys/[id]/models: has dynamic = 'force-dynamic' export", () => {+ assert.ok(+ src.includes('dynamic = "force-dynamic"') || src.includes("dynamic = 'force-dynamic'"),+ "route must export dynamic = 'force-dynamic'",+ );+});++// ── Next 16 async params ──────────────────────────────────────────────────────++test("quota/keys/[id]/models: reads id via await params (Next 16 pattern)", () => {+ assert.ok(+ src.includes("await params"),+ "route must use await params — Next 16 async params pattern",+ );+});++test("quota/keys/[id]/models: params typed as Promise<{ id: string }>", () => {+ assert.ok(+ src.includes("Promise<") && src.includes("id: string"),+ "params type must be Promise<{ id: string }> — matching groups/[id] pattern",+ );+});++// ── exports GET ───────────────────────────────────────────────────────────────++test("quota/keys/[id]/models: exports GET handler", () => {+ assert.ok(+ src.includes("export async function GET"),+ "route must export the GET handler",+ );+});