Files
supabase/apps/studio/components/interfaces/ConnectSheet/ConnectionString.utils.ts
T
Saxon FletcherandJoshen Lim b9a09d8612 Connect sheet content structure (#42373)
This is part two of a [PR
breakdown](https://github.com/supabase/supabase/pull/42276) that
introduces our connect schema and how content is retrieved. This focuses
on the Framework tab to start.

Fields are generated and content is rendered using a connect.schema
file. This schema file defines modes, fields and steps. Each mode has a
set of fields. Each field can be dependent on another field. The steps
generated are then based off the values of the modes and fields. Each
step can also render varying content dynamically using a template
{{framework}}/{{frameworkVariant}}/{{library}} which just replaces those
values with what's in state (fields selected).

[Part one needs to be merged
first.](https://github.com/supabase/supabase/pull/42367)

The next stage will add back all other tabs and content.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* New Connect Sheet UI to guide app-to-project connections with dynamic
configuration and copyable connection parameters.
* Multi-framework & mobile support with ready-to-use code examples,
install commands, and step-by-step setup for 20+ frameworks.
* Multiple PostgreSQL connection methods (direct, transaction, session)
with safe/masked connection previews.
  * Copy-prompt that aggregates step content and code for easy sharing.
* **UI**
* Multi-file code viewer with tabbed code blocks and added syntax
support (Swift, TOML, HTML).
* **Tests**
  * Comprehensive unit tests for resolver and state logic.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-02-11 13:32:45 +08:00

89 lines
2.2 KiB
TypeScript

import type { ConnectionStringPooler } from './Connect.types'
import type { ConnectionStringMethod } from './Connect.constants'
export const DEFAULT_PORT = '5432'
export const PASSWORD_PLACEHOLDER = '[YOUR-PASSWORD]'
export type ConnectionParams = {
host: string
port: string
user: string
database: string
}
export const resolveConnectionString = ({
connectionMethod,
useSharedPooler,
connectionStringPooler,
}: {
connectionMethod: ConnectionStringMethod
useSharedPooler: boolean
connectionStringPooler: ConnectionStringPooler
}) => {
if (connectionMethod === 'direct') {
return connectionStringPooler.direct ?? ''
}
if (connectionMethod === 'session') {
return connectionStringPooler.sessionShared ?? ''
}
if (useSharedPooler || !connectionStringPooler.transactionDedicated) {
return connectionStringPooler.transactionShared ?? ''
}
return connectionStringPooler.transactionDedicated ?? ''
}
export const parseConnectionParams = (connectionString: string): ConnectionParams => {
if (!connectionString) {
return {
host: 'hidden',
port: DEFAULT_PORT,
user: 'hidden',
database: 'hidden',
}
}
try {
const parsed = new URL(connectionString)
return {
host: parsed.hostname || 'hidden',
port: parsed.port || DEFAULT_PORT,
user: parsed.username || 'hidden',
database: parsed.pathname?.replace(/^\//, '') || 'hidden',
}
} catch (error) {
return {
host: 'hidden',
port: DEFAULT_PORT,
user: 'hidden',
database: 'hidden',
}
}
}
export const buildSafeConnectionString = (
connectionString: string,
params: ConnectionParams
): string => {
if (!connectionString) return ''
const search = (() => {
try {
return new URL(connectionString).search
} catch (error) {
return ''
}
})()
return `postgresql://${params.user}:${PASSWORD_PLACEHOLDER}@${params.host}:${params.port}/${params.database}${search}`
}
export const buildConnectionParameters = (params: ConnectionParams) => [
{ key: 'host', value: params.host },
{ key: 'port', value: params.port },
{ key: 'database', value: params.database },
{ key: 'user', value: params.user },
]