diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index 26683233..cda21706 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -17,6 +17,7 @@ import { DEFAULT_DB_OPTIONS, DEFAULT_AUTH_OPTIONS, DEFAULT_REALTIME_OPTIONS, + DEFAULT_POSTGREST_OPTIONS, } from './lib/constants' import { fetchWithAuth } from './lib/fetch' import { stripTrailingSlash, applySettingDefaults } from './lib/helpers' @@ -88,6 +89,7 @@ export default class SupabaseClient< realtime: DEFAULT_REALTIME_OPTIONS, auth: { ...DEFAULT_AUTH_OPTIONS, storageKey: defaultStorageKey }, global: DEFAULT_GLOBAL_OPTIONS, + postgrest: DEFAULT_POSTGREST_OPTIONS, } const settings = applySettingDefaults(options ?? {}, DEFAULTS) @@ -103,7 +105,7 @@ export default class SupabaseClient< this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.global?.fetch) this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime }) - this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, { + this.rest = new PostgrestClient(`${_supabaseUrl}${settings.postgrest?.routePrefix}`, { headers: this.headers, schema: settings.db?.schema, fetch: this.fetch, diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 101927de..52a6bfd0 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -33,3 +33,7 @@ export const DEFAULT_AUTH_OPTIONS: SupabaseAuthClientOptions = { } export const DEFAULT_REALTIME_OPTIONS: RealtimeClientOptions = {} + +export const DEFAULT_POSTGREST_OPTIONS = { + routePrefix: '/rest/v1', +} diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index b5243a0c..4b73c20e 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -29,12 +29,14 @@ export function applySettingDefaults< auth: authOptions, realtime: realtimeOptions, global: globalOptions, + postgrest: postgrestOptions, } = options const { db: DEFAULT_DB_OPTIONS, auth: DEFAULT_AUTH_OPTIONS, realtime: DEFAULT_REALTIME_OPTIONS, global: DEFAULT_GLOBAL_OPTIONS, + postgrest: DEFAULT_POSTGREST_OPTIONS, } = defaults return { @@ -54,5 +56,9 @@ export function applySettingDefaults< ...DEFAULT_GLOBAL_OPTIONS, ...globalOptions, }, + postgrest: { + ...DEFAULT_POSTGREST_OPTIONS, + ...postgrestOptions, + }, } } diff --git a/src/lib/types.ts b/src/lib/types.ts index 9621c7d6..c891a5d0 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -66,6 +66,12 @@ export type SupabaseClientOptions = { */ headers?: Record } + postgrest?: { + /** + * The route prefix for your PostgREST endpoint. Defaults to `/rest/v1`. + */ + routePrefix?: string + } } export type GenericTable = { diff --git a/test/client.test.ts b/test/client.test.ts index 38f97e8e..e7112764 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -67,6 +67,28 @@ describe('Dynamic schema', () => { }) }) +describe('PostgREST prefix', () => { + test('is /rest/v1 by default', async () => { + const client = createClient('HTTP://localhost:3000', KEY) + + // @ts-ignore + const restUrl = client.rest.url + + expect(restUrl).toEqual('HTTP://localhost:3000/rest/v1') + }) + + test('is set to custom value when provided', async () => { + const client = createClient('HTTP://localhost:3000', KEY, { + postgrest: { routePrefix: '/custom' }, + }) + + // @ts-ignore + const restUrl = client.rest.url + + expect(restUrl).toEqual('HTTP://localhost:3000/custom') + }) +}) + // Socket should close when there are no open connections // https://github.com/supabase/supabase-js/issues/44