mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
Moves every `pages/api/**` handler into its TanStack server route under `routes/api/**` and deletes `pages/api/**`, part of the final pass of the TanStack Start migration (FE-3106). Handlers move verbatim and still run through `toWebHandler`, so auth (`apiWrapper`), response shapes and headers are unchanged. Part 2 of the stack. **Depends on:** #53199 **Changed:** - `routes/api/**` – each route now holds its handler (formerly the `pages/api` default export) as a local `nextHandler`/named function. Next-only `config`/`maxDuration` exports are dropped: `vite.config.ts` already sets the function `maxDuration`, and `toWebHandler` parses bodies itself - `routes/api/ai/docs.ts` – Web-native handler inlined, `NextRequest` → `Request` - `lib/api/apiHelpers.ts` – `getPgMetaRedirectUrl` moved here from the old `tables` handler, which the other pg-meta routes imported it from - `eslint.config.cjs`, `.github/CODEOWNERS` – `pages/api` paths → `routes/api` - `tests/routes/api/**`, `lib/api/{generate-v4,rate}.test.ts`, `scripts/__tests__/server-source-maps.test.ts` – call the route's registered handler with a Web `Request` (`tests/lib/server-route.ts`); the MCP query-validation cases now run against the native route **Removed:** - `pages/api/**` (102 files), including the Next twins of the already-native `routes/api/mcp/index.ts` and `functions/$slug/body.ts` - unused `ProjectSettings` types and the `node-mocks-http` dev dependency Parity: every route registers the same methods its handler serves. The exceptions are `check-cname`, `get-ip-address`, `get-utc-time`, `get-s3-keys`, `get-deployment-commit`, `cli-release-version` and `connect`: in Next they answered any method, and their routes register `GET` only, as before this PR. Addresses [FE-3106](https://linear.app/supabase/issue/FE-3106/move-page-bodies-into-route-files-and-delete-pages). ## To test - Load the table editor, SQL editor and Auth users on a self-hosted build – `/api/platform/**` and `/api/v1/**` respond as before - Use the Assistant and the docs search – `/api/ai/**` streams as before - `GET /api/get-deployment-commit` – same JSON and `Cache-Control` Ran typecheck, ESLint, lint ratchet, knip, Prettier, the TanStack build and the full Studio vitest suite: all pass except `lib/server/configcat.test.ts`, which also fails on `master` in an environment with ConfigCat env vars set. Self-hosted E2E is CI-only. --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
140 lines
5.3 KiB
TypeScript
140 lines
5.3 KiB
TypeScript
import { createFileRoute } from '@tanstack/react-router'
|
|
import { parse } from 'libpg-query'
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
|
|
|
import { toWebHandler } from '@/compat/next/api'
|
|
|
|
const getOperation = (stmt: Record<string, unknown>) => {
|
|
if ('SelectStmt' in stmt) return 'SELECT'
|
|
if ('InsertStmt' in stmt) return 'INSERT'
|
|
if ('UpdateStmt' in stmt) return 'UPDATE'
|
|
if ('DeleteStmt' in stmt) return 'DELETE'
|
|
}
|
|
|
|
const getTablesInQuery = (ast: unknown) => {
|
|
const tables: string[] = []
|
|
|
|
function traverse(node: unknown): void {
|
|
if (!node || typeof node !== 'object') return
|
|
const obj = node as Record<string, unknown>
|
|
|
|
if ('RangeVar' in obj) {
|
|
const rv = obj.RangeVar as { relname?: string; schemaname?: string }
|
|
if (rv.relname) tables.push(rv.schemaname ? `${rv.schemaname}.${rv.relname}` : rv.relname)
|
|
}
|
|
|
|
if ('relation' in obj && obj.relation && typeof obj.relation === 'object') {
|
|
const rv = obj.relation as { relname?: string; schemaname?: string }
|
|
if (rv.relname) tables.push(rv.schemaname ? `${rv.schemaname}.${rv.relname}` : rv.relname)
|
|
}
|
|
|
|
for (const value of Object.values(obj)) {
|
|
Array.isArray(value) ? value.forEach(traverse) : traverse(value)
|
|
}
|
|
}
|
|
|
|
traverse(ast)
|
|
return [...new Set(tables)].sort((a, b) => a.localeCompare(b))
|
|
}
|
|
|
|
// libpg-query nodes only expose the location of their own leading token (e.g. a BoolExpr's
|
|
// location is its "AND"/"OR" operator, not the start of its left operand), and some nodes
|
|
// (SortBy) use -1 as an "unset" sentinel. Recursing to the smallest non-negative location in
|
|
// a subtree gives the true start of that subtree's text in the original SQL.
|
|
const getMinLocation = (node: unknown): number | undefined => {
|
|
if (!node || typeof node !== 'object') return undefined
|
|
const obj = node as Record<string, unknown>
|
|
let min: number | undefined
|
|
const consider = (loc: unknown) => {
|
|
if (typeof loc === 'number' && loc >= 0 && (min === undefined || loc < min)) min = loc
|
|
}
|
|
consider(obj.location)
|
|
for (const value of Object.values(obj)) {
|
|
if (Array.isArray(value)) value.forEach((item) => consider(getMinLocation(item)))
|
|
else if (value && typeof value === 'object') consider(getMinLocation(value))
|
|
}
|
|
return min
|
|
}
|
|
|
|
// Clauses that can immediately follow WHERE in each statement type, paired with the keyword
|
|
// that introduces them in the original SQL — used to find where the WHERE condition's text
|
|
// ends, since libpg-query has no deparser to do this for us.
|
|
const FOLLOWING_CLAUSES: Record<string, [field: string, keyword: RegExp][]> = {
|
|
SelectStmt: [
|
|
['groupClause', /\bgroup\s+by\b/i],
|
|
['havingClause', /\bhaving\b/i],
|
|
['windowClause', /\bwindow\b/i],
|
|
['sortClause', /\border\s+by\b/i],
|
|
['limitOffset', /\boffset\b/i],
|
|
['limitCount', /\blimit\b/i],
|
|
['lockingClause', /\bfor\b/i],
|
|
],
|
|
UpdateStmt: [['returningList', /\breturning\b/i]],
|
|
DeleteStmt: [['returningList', /\breturning\b/i]],
|
|
}
|
|
|
|
const getWhereClauseText = (sql: string, stmtType: string, stmt: Record<string, unknown>) => {
|
|
const start = getMinLocation(stmt.whereClause)
|
|
if (start === undefined) return null
|
|
|
|
const candidateEnds: number[] = []
|
|
for (const [field, keyword] of FOLLOWING_CLAUSES[stmtType] ?? []) {
|
|
const value = stmt[field]
|
|
const node = Array.isArray(value) ? value[0] : value
|
|
if (!node) continue
|
|
|
|
// Bound the keyword search to before the clause's own expression when we know where that
|
|
// starts; otherwise (e.g. lockingClause, which carries no location at all) search onward.
|
|
const exprStart = getMinLocation(node)
|
|
const window =
|
|
exprStart !== undefined && exprStart > start ? sql.slice(start, exprStart) : sql.slice(start)
|
|
const match = keyword.exec(window)
|
|
if (match) candidateEnds.push(start + match.index)
|
|
else if (exprStart !== undefined && exprStart > start) candidateEnds.push(exprStart)
|
|
}
|
|
|
|
const end =
|
|
candidateEnds.length > 0 ? Math.min(...candidateEnds) : sql.replace(/;\s*$/, '').length
|
|
return sql.slice(start, end).trim()
|
|
}
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method !== 'POST') {
|
|
res.setHeader('Allow', ['POST'])
|
|
return res.status(405).json({ error: `Method ${req.method} Not Allowed` })
|
|
}
|
|
|
|
try {
|
|
const { sql } = req.body
|
|
|
|
if (typeof sql !== 'string' || sql.trim().length === 0) {
|
|
return res.status(400).json({ error: 'Missing or invalid "sql" in request body' })
|
|
}
|
|
|
|
const ast = await parse(sql)
|
|
const statementCount = ast.stmts?.length ?? 0
|
|
const stmt = ast.stmts?.[0]?.stmt as Record<string, unknown> | undefined
|
|
const [stmtType, stmtNode] = stmt ? Object.entries(stmt)[0] : []
|
|
|
|
const tables = getTablesInQuery(ast)
|
|
const operation = stmt ? getOperation(stmt) : null
|
|
const whereClause =
|
|
stmtType && stmtNode
|
|
? getWhereClauseText(sql, stmtType, stmtNode as Record<string, unknown>)
|
|
: null
|
|
|
|
return res.status(200).json({ tables, operation, whereClause, statementCount })
|
|
} catch (error) {
|
|
const message =
|
|
(error as { sqlDetails?: { message?: string } })?.sqlDetails?.message ??
|
|
(error instanceof Error ? error.message : 'Failed to parse SQL')
|
|
return res.status(400).json({ error: message })
|
|
}
|
|
}
|
|
|
|
const webHandler = toWebHandler(handler)
|
|
|
|
export const Route = createFileRoute('/api/parse-query')({
|
|
server: { handlers: { POST: webHandler } },
|
|
})
|