Skip to content

feat: allow overriding default PostgREST route prefix #1045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/SupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already is a settings.db project, although that targets database access-related modifiers.
This addition simply targets how the lib interfaces with PostgREST

}

const settings = applySettingDefaults(options ?? {}, DEFAULTS)
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ export const DEFAULT_AUTH_OPTIONS: SupabaseAuthClientOptions = {
}

export const DEFAULT_REALTIME_OPTIONS: RealtimeClientOptions = {}

export const DEFAULT_POSTGREST_OPTIONS = {
routePrefix: '/rest/v1',
}
6 changes: 6 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -54,5 +56,9 @@ export function applySettingDefaults<
...DEFAULT_GLOBAL_OPTIONS,
...globalOptions,
},
postgrest: {
...DEFAULT_POSTGREST_OPTIONS,
...postgrestOptions,
},
}
}
6 changes: 6 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export type SupabaseClientOptions<SchemaName> = {
*/
headers?: Record<string, string>
}
postgrest?: {
/**
* The route prefix for your PostgREST endpoint. Defaults to `/rest/v1`.
*/
routePrefix?: string
}
}

export type GenericTable = {
Expand Down
22 changes: 22 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ describe('Dynamic schema', () => {
})
})

describe('PostgREST prefix', () => {
test('is /rest/v1 by default', async () => {
const client = createClient<Database>('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<Database>('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

Expand Down
Loading