diff --git a/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.test.ts b/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.test.ts index f39388b27dc..a94fceac858 100644 --- a/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.test.ts +++ b/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.test.ts @@ -13,7 +13,7 @@ const makeDatabase = ( const makeLoadBalancer = (endpoint: string): Pick => ({ endpoint }) describe('getApiEndpoint', () => { - it('returns custom domain URL when custom domain is active and primary database is selected', () => { + it('returns custom domain URL with /rest/v1/ when custom domain is active and primary database is selected', () => { expect( getApiEndpoint({ selectedDatabaseId: 'project-ref', @@ -22,10 +22,10 @@ describe('getApiEndpoint', () => { loadBalancers: undefined, selectedDatabase: makeDatabase( 'project-ref', - 'https://project-ref.supabase.co/rest/v1' + 'https://project-ref.supabase.co/rest/v1/' ) as Database, }) - ).toBe('https://api.example.com') + ).toBe('https://api.example.com/rest/v1/') }) it('returns database restUrl when custom domain is active but a replica is selected', () => { @@ -35,15 +35,30 @@ describe('getApiEndpoint', () => { projectRef: 'project-ref', resolvedEndpoint: 'https://api.example.com', loadBalancers: undefined, + selectedDatabase: makeDatabase( + 'replica-1', + 'https://replica-1.supabase.co/rest/v1/' + ) as Database, + }) + ).toBe('https://replica-1.supabase.co/rest/v1/') + }) + + it('normalizes a replica restUrl without a trailing slash', () => { + expect( + getApiEndpoint({ + selectedDatabaseId: 'replica-1', + projectRef: 'project-ref', + resolvedEndpoint: undefined, + loadBalancers: undefined, selectedDatabase: makeDatabase( 'replica-1', 'https://replica-1.supabase.co/rest/v1' ) as Database, }) - ).toBe('https://replica-1.supabase.co/rest/v1') + ).toBe('https://replica-1.supabase.co/rest/v1/') }) - it('returns load balancer endpoint when load balancer is selected', () => { + it('returns load balancer endpoint with /rest/v1/ when load balancer is selected', () => { expect( getApiEndpoint({ selectedDatabaseId: 'load-balancer', @@ -52,7 +67,7 @@ describe('getApiEndpoint', () => { loadBalancers: [makeLoadBalancer('https://lb.supabase.co') as LoadBalancer], selectedDatabase: undefined, }) - ).toBe('https://lb.supabase.co') + ).toBe('https://lb.supabase.co/rest/v1/') }) it('returns empty string when load balancer is selected but none exist', () => { @@ -76,10 +91,10 @@ describe('getApiEndpoint', () => { loadBalancers: undefined, selectedDatabase: makeDatabase( 'replica-2', - 'https://replica-2.supabase.co/rest/v1' + 'https://replica-2.supabase.co/rest/v1/' ) as Database, }) - ).toBe('https://replica-2.supabase.co/rest/v1') + ).toBe('https://replica-2.supabase.co/rest/v1/') }) }) diff --git a/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.ts b/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.ts index da0fd5d6be1..060b05c619f 100644 --- a/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.ts +++ b/apps/studio/components/interfaces/Integrations/DataApi/DataApi.utils.ts @@ -5,7 +5,8 @@ import { snakeToCamel } from '@/lib/helpers' /** * Resolves the API endpoint URL based on the selected database, custom domain - * status, and load balancer configuration. + * status, and load balancer configuration. The returned URL is normalized to + * end with `/rest/v1/` to match the Data API base path documented elsewhere. */ export function getApiEndpoint({ selectedDatabaseId, @@ -23,14 +24,20 @@ export function getApiEndpoint({ const loadBalancerSelected = selectedDatabaseId === 'load-balancer' if (selectedDatabaseId === projectRef && !!resolvedEndpoint) { - return resolvedEndpoint + return withDataApiPath(resolvedEndpoint) } if (loadBalancerSelected) { - return loadBalancers?.[0]?.endpoint ?? '' + return withDataApiPath(loadBalancers?.[0]?.endpoint) } - return selectedDatabase?.restUrl ?? '' + return withDataApiPath(selectedDatabase?.restUrl) +} + +function withDataApiPath(url: string | undefined): string { + if (!url) return '' + const trimmed = url.replace(/\/+$/, '') + return /\/rest\/v1$/.test(trimmed) ? `${trimmed}/` : `${trimmed}/rest/v1/` } export type EnrichedEntity = { id: string; displayName: string; camelCase: string }