mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem Studio's `routes/api/platform/storage/**` routes still run legacy Next Pages Router handlers through `compat/next/api.ts`, and `buckets/$id/objects/download.ts` has no remaining callers. Part 6 of 7 for [FE-5114](https://linear.app/supabase/issue/FE-5114/rewrite-api-routes-as-native-tanstack-start-handlers-and-remove-the) migrates the 14 active storage routes to native TanStack Start handlers and removes the unused download route. ## Solution **Changed:** - Migrated 14 `buckets/**` and `vector-buckets/**` routes to `createApiHandlers` and `parseJsonBody` - Replaced the `parseAsInt`/`parseAsString`/`parseAsStringEnum` helpers in `buckets/index.ts` with a zod query schema that drops invalid values, keeping the conditional spreads so unset keys aren't sent to storage-js - Forwarded upstream `StorageError` status codes across all storage and vector-bucket routes via `storageErrorResponse` / `getStorageErrorStatus` (falling back to the route's default 400/500 when no valid HTTP status is present) - Guarded missing/invalid `SUPABASE_PUBLIC_URL` with a `500` error response in `objects/public-url`, `objects/sign`, and `objects/sign-multi` - Typed the `vector-buckets/$id/indexes` POST body from storage-js's `createIndex` options - Corrected stale `Allow` headers on `vector-buckets/$id` (`GET, DELETE`) and `vector-buckets/$id/indexes/$indexName` (`DELETE`) via `createApiHandlers` **Added:** - `lib/api/self-hosted/storage.ts` with `getStorageErrorStatus`, `storageErrorResponse`, `getStoragePublicOrigin`, `missingStoragePublicUrlResponse`, and `remapStoragePublicUrl` - Unit tests for `buckets/index` query parsing, `buckets/$id/objects/sign-multi` (`SUPABASE_PUBLIC_URL` guard, origin remapping, and upstream error status), and `vector-buckets` POST (exercising the real storage client through MSW) **Removed:** - Removed unused `routes/api/platform/storage/$ref/buckets/$id/objects/download.ts` and regenerated `routeTree.gen.ts` **Behavior changes vs master:** - `buckets` GET drops malformed `limit`/`offset`/sort values instead of `parseInt`-truncating them (e.g. `offset=1.5` is ignored rather than sent as `1`) - `vector-buckets` POST validates the body with zod and returns `400` when `bucketName` is missing or not a string - Storage routes forward the upstream HTTP status code from `StorageApiError` instead of hardcoding `400`/`500` - `objects/public-url`, `objects/sign`, and `objects/sign-multi` return a `500` JSON error when `SUPABASE_PUBLIC_URL` is unset or invalid instead of throwing an uncaught `TypeError` - Unused `buckets/$id/objects/download` route has been removed ## Review instructions 1. Run `pnpm --filter studio exec vitest run tests/routes/api/platform/storage/`. 2. On self-hosted Studio, create/list/empty/delete a storage bucket, upload/download/move/sign/delete an object, and check vector bucket operations. ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
type StorageErrorLike = {
|
|
message: string
|
|
status?: number
|
|
statusCode?: string
|
|
}
|
|
|
|
/**
|
|
* Resolve the HTTP status code from a storage-js `StorageError`, falling back when the error
|
|
* carries no valid 4xx/5xx status (for example `StorageUnknownError` on network failures).
|
|
*/
|
|
export function getStorageErrorStatus(error: StorageErrorLike, fallbackStatus = 400): number {
|
|
if (
|
|
typeof error.status === 'number' &&
|
|
Number.isInteger(error.status) &&
|
|
error.status >= 400 &&
|
|
error.status <= 599
|
|
) {
|
|
return error.status
|
|
}
|
|
|
|
const parsedStatusCode = error.statusCode ? Number(error.statusCode) : NaN
|
|
if (Number.isInteger(parsedStatusCode) && parsedStatusCode >= 400 && parsedStatusCode <= 599) {
|
|
return parsedStatusCode
|
|
}
|
|
|
|
return fallbackStatus
|
|
}
|
|
|
|
/**
|
|
* Error response for a failed storage-js call, forwarding the upstream HTTP status when present.
|
|
*/
|
|
export function storageErrorResponse(error: StorageErrorLike, fallbackStatus = 400): Response {
|
|
return Response.json(
|
|
{ error: { message: error.message } },
|
|
{ status: getStorageErrorStatus(error, fallbackStatus) }
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Parse `SUPABASE_PUBLIC_URL` for rewriting internal storage URLs into client-reachable ones.
|
|
* Returns `null` when the env var is missing or not a valid URL.
|
|
*/
|
|
export function getStoragePublicOrigin(): URL | null {
|
|
const raw = process.env.SUPABASE_PUBLIC_URL
|
|
if (!raw) return null
|
|
try {
|
|
return new URL(raw)
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function missingStoragePublicUrlResponse(): Response {
|
|
return Response.json(
|
|
{ error: { message: 'SUPABASE_PUBLIC_URL is not configured' } },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
export function remapStoragePublicUrl(url: string, publicOrigin: URL): string {
|
|
const remapped = new URL(url)
|
|
remapped.protocol = publicOrigin.protocol
|
|
remapped.host = publicOrigin.host
|
|
remapped.port = publicOrigin.port
|
|
return remapped.href
|
|
}
|