mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem With all Studio API routes migrated to native TanStack Start handlers (or removed when unused) in parts 1–6, the Next Pages Router API adapter and legacy `NextApiRequest`/`NextApiResponse` wrappers are no longer used. Part 7 of 7 for [FE-5114](https://linear.app/supabase/issue/FE-5114/rewrite-api-routes-as-native-tanstack-start-handlers-and-remove-the) tears them down. ## Solution **Removed:** - `compat/next/api.ts` and `compat/next/api.test.ts` (`toWebHandler`) - Legacy `apiWrapper`, `apiAuthenticate`, and `fetchUserClaims` (`NextApiRequest`/`NextApiResponse`) functions and their legacy tests in `lib/api/apiWrapper.ts`, `lib/api/apiAuthenticate.ts`, `lib/api/apiWrappers.test.ts`, and `lib/api/apiAuthenticate.test.ts` - The now-unused `fromNodeHeaders` helper (and its `node:http` import) and its tests in `lib/api/apiHelpers.ts` / `lib/api/apiHelpers.test.ts` **Changed:** - Updated comment in `lib/server/configcat.ts` from `apiAuthenticate` to `authenticateRequest` ## Review instructions 1. Confirm `git grep -E "NextApiRequest|NextApiResponse|compat/next/api|toWebHandler|fromNodeHeaders|from 'next'" apps/studio/` returns no matches. 2. Run `pnpm --filter studio run typecheck`, `pnpm knip --workspace apps/studio`, and `pnpm --filter studio exec vitest run lib/api/ tests/routes/api/ scripts/__tests__/server-source-maps.test.ts`. ## 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>
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import { getClient, PollingMode, User } from '@configcat/sdk/node'
|
|
import { IS_PLATFORM } from 'common'
|
|
|
|
let serverClient: ReturnType<typeof getClient>
|
|
|
|
export type TrustedUserEmail = string & { readonly __trustedUserEmailBrand: never }
|
|
|
|
// Promotes an email to TrustedUserEmail. Only call this with an email whose origin is already
|
|
// verified — e.g. `claims.email` from a JWT that authenticateRequest has confirmed — never with a
|
|
// raw request body/query param.
|
|
export function trustedUserEmail(email: string | undefined): TrustedUserEmail | undefined {
|
|
return email as TrustedUserEmail | undefined
|
|
}
|
|
|
|
const STAFF_EMAIL_DOMAINS = ['supabase.com', 'supabase.io']
|
|
|
|
function isStaffEmail(email: string): boolean {
|
|
const domain = email.slice(email.lastIndexOf('@') + 1).toLowerCase()
|
|
return STAFF_EMAIL_DOMAINS.includes(domain)
|
|
}
|
|
|
|
function buildUser(userEmail?: TrustedUserEmail, customAttributes?: Record<string, string>) {
|
|
const _customAttributes = {
|
|
...customAttributes,
|
|
is_staff: (!!userEmail && isStaffEmail(userEmail)).toString(),
|
|
}
|
|
|
|
return new User(userEmail ?? 'anonymous', undefined, undefined, _customAttributes)
|
|
}
|
|
|
|
function getServerClient() {
|
|
if (serverClient) return serverClient
|
|
|
|
const proxyUrl = process.env.NEXT_PUBLIC_CONFIGCAT_PROXY_URL
|
|
const sdkKey = process.env.NEXT_PUBLIC_CONFIGCAT_SDK_KEY
|
|
|
|
if (!proxyUrl && !sdkKey) {
|
|
console.log('Skipping server ConfigCat set up as env vars are not present')
|
|
return undefined
|
|
}
|
|
|
|
try {
|
|
if (proxyUrl) {
|
|
serverClient = getClient('configcat-proxy/frontend-v2', PollingMode.LazyLoad, {
|
|
baseUrl: proxyUrl,
|
|
})
|
|
return serverClient
|
|
}
|
|
|
|
if (sdkKey) {
|
|
serverClient = getClient(sdkKey, PollingMode.LazyLoad)
|
|
return serverClient
|
|
}
|
|
|
|
return undefined
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error)
|
|
console.error(`Failed to get server ConfigCat client: ${message}`)
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
export async function getServerFlags(
|
|
userEmail?: TrustedUserEmail,
|
|
customAttributes?: Record<string, string>
|
|
) {
|
|
const client = getServerClient()
|
|
|
|
if (!client) {
|
|
return []
|
|
}
|
|
|
|
return client.getAllValuesAsync(buildUser(userEmail, customAttributes))
|
|
}
|
|
|
|
export async function isServerFlagEnabled(name: string): Promise<boolean> {
|
|
if (!IS_PLATFORM) return false
|
|
|
|
const flags = await getServerFlags()
|
|
return flags.some((flag) => flag.settingKey === name && flag.settingValue === true)
|
|
}
|