Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Allow downloading of types from the dashboard

Joshen Lim698a9a71010a
diff --git a/studio/components/interfaces/Docs/GeneralContent.tsx b/studio/components/interfaces/Docs/GeneralContent.tsx
index 7ca969ab2ed..8823d00c030 100644
--- a/studio/components/interfaces/Docs/GeneralContent.tsx
+++ b/studio/components/interfaces/Docs/GeneralContent.tsx
@@ -24,8 +24,7 @@ const GeneralContent = ({ autoApiService, selectedLang, page, showApiKey }: any)
showApiKey={showApiKey}
/>
)
- if (selected == 'tables-intro')
- return <TablesIntroduction autoApiService={autoApiService} selectedLang={selectedLang} />
+ if (selected == 'tables-intro') return <TablesIntroduction selectedLang={selectedLang} />
if (selected == 'rpc-intro') return <RpcIntroduction />
else
return (
diff --git a/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.js b/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.js
deleted file mode 100644
index d4fb7b86819..00000000000
--- a/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.js
+++ /dev/null
@@ -1,122 +0,0 @@
-import CodeSnippet from '../../CodeSnippet'
-import Link from 'next/link'
-import { useRouter } from 'next/router'
-
-export default function Introduction({ autoApiService, selectedLang }) {
- const router = useRouter()
- let ref = router.query.ref
- return (
- <>
- <h2 className="doc-heading">Introduction</h2>
- <div className="doc-section">
- <article className="text ">
- <p>
- All views and tables in the <code>public</code> schema and accessible by the active
- database role for a request are available for querying.
- </p>
- </article>
- </div>
- <h2 className="doc-heading">Non-exposed tables</h2>
- <div className="doc-section">
- <article className="text ">
- <p>
- If you don't want to expose tables in your API, simply add them to a different schema
- (not the <code>public</code> schema).
- </p>
- </article>
- <article className="code"></article>
- </div>
- <h2 className="doc-heading">
- GraphQL <span className="lowercase">vs</span> Supabase
- </h2>
- <div className="doc-section">
- <article className="text ">
- <p>
- If you have a GraphQL background, you might be wondering if you can fetch your data in a
- single round-trip. The answer is yes!
- </p>
- <p>
- The syntax is very similar. This example shows how you might achieve the same thing with
- Apollo GraphQL and Supabase.
- <br />
- <br />
- </p>
- <h4>Still want GraphQL?</h4>
- <p>
- If you still want to use GraphQL, you can. Supabase provides you with a full Postgres
- database, so as long as your middleware can connect to the database then you can still
- use the tools you love. You can find the database connection details{' '}
- <Link href={`/project/${ref}/settings/database`}>in the settings.</Link>
- </p>
- </article>
- <article className="code">
- <CodeSnippet selectedLang={selectedLang} snippet={localSnippets.withApollo()} />
- <CodeSnippet selectedLang={selectedLang} snippet={localSnippets.withSupabase()} />
- </article>
- </div>
- </>
- )
-}
-
-const localSnippets = {
- withApollo: () => ({
- title: 'With Apollo GraphQL',
- bash: {
- language: 'js',
- code: `
-const { loading, error, data } = useQuery(gql\`
- query GetDogs {
- dogs {
- id
- breed
- owner {
- id
- name
- }
- }
- }
-\`)`,
- },
- js: {
- language: 'js',
- code: `
-const { loading, error, data } = useQuery(gql\`
- query GetDogs {
- dogs {
- id
- breed
- owner {
- id
- name
- }
- }
- }
-\`)`,
- },
- }),
- withSupabase: () => ({
- title: 'With Supabase',
- bash: {
- language: 'js',
- code: `
-const { data, error } = await supabase
- .from('dogs')
- .select(\`
- id, breed,
- owner (id, name)
- \`)
-`,
- },
- js: {
- language: 'js',
- code: `
-const { data, error } = await supabase
- .from('dogs')
- .select(\`
- id, breed,
- owner (id, name)
- \`)
-`,
- },
- }),
-}
diff --git a/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.tsx b/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.tsx
new file mode 100644
index 00000000000..c525baaffe0
--- /dev/null
+++ b/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.tsx
@@ -0,0 +1,222 @@
+import { FC, useState } from 'react'
+import Link from 'next/link'
+import { Button, IconDownload, IconExternalLink } from 'ui'
+import { useParams, useStore } from 'hooks'
+import { API_ADMIN_URL } from 'lib/constants'
+import { get } from 'lib/common/fetch'
+import CodeSnippet from '../../CodeSnippet'
+
+interface Props {
+ selectedLang: string
+}
+
+const Introduction: FC<Props> = ({ selectedLang }) => {
+ const { ref } = useParams()
+ const { ui } = useStore()
+ const [isGeneratingTypes, setIsGeneratingTypes] = useState(false)
+
+ const generateTypes = async () => {
+ setIsGeneratingTypes(true)
+ const res = await get(`${API_ADMIN_URL}/projects/${ref}/types/typescript`)
+
+ if (!res.error) {
+ let element = document.createElement('a')
+ element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(res.types))
+ element.setAttribute('download', 'supabase.ts')
+ element.style.display = 'none'
+ document.body.appendChild(element)
+ element.click()
+ document.body.removeChild(element)
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully generated types! File is being downloaded`,
+ })
+ } else {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to generate types: ${res.error.message}`,
+ error: res.error,
+ })
+ }
+
+ setIsGeneratingTypes(false)
+ }
+
+ return (
+ <>
+ <h2 className="doc-heading">Introduction</h2>
+ <div className="doc-section">
+ <article className="text ">
+ <p>
+ All views and tables in the <code>public</code> schema and accessible by the active
+ database role for a request are available for querying.
+ </p>
+ </article>
+ </div>
+
+ <h2 className="doc-heading">Non-exposed tables</h2>
+ <div className="doc-section">
+ <article className="text ">
+ <p>
+ If you don't want to expose tables in your API, simply add them to a different schema
+ (not the <code>public</code> schema).
+ </p>
+ </article>
+ <article className="code"></article>
+ </div>
+
+ <h2 className="doc-heading flex items-center justify-between">
+ <span>Generating types</span>
+ <Link href="https://supabase.com/docs/guides/api/generating-types">
+ <a target="_blank">
+ <Button type="default" icon={<IconExternalLink />}>
+ Documentation
+ </Button>
+ </a>
+ </Link>
+ </h2>
+ <div className="doc-section">
+ <article className="text ">
+ <p>
+ Supabase APIs are generated from your database, which means that we can use database
+ introspection to generate type-safe API definitions.
+ </p>
+ <p>
+ You can generate types from your database either through the{' '}
+ <Link href="https://supabase.com/docs/guides/api/generating-types">Supabase CLI</Link>,
+ or by downloading the types file via the button on the right and importing it in your
+ application within <code>src/index.ts</code>.
+ </p>
+ </article>
+ <article className={`code ${selectedLang === 'js' ? 'flex items-center' : ''}`}>
+ {selectedLang === 'js' && (
+ <Button
+ type="default"
+ disabled={isGeneratingTypes}
+ loading={isGeneratingTypes}
+ icon={<IconDownload strokeWidth={1.5} />}
+ onClick={generateTypes}
+ >
+ Generate and download types
+ </Button>
+ )}
+ <CodeSnippet selectedLang={selectedLang} snippet={localSnippets.cliLogin()} />
+ <CodeSnippet
+ selectedLang={selectedLang}
+ snippet={localSnippets.generateTypes(ref ?? '')}
+ />
+ </article>
+ </div>
+
+ <h2 className="doc-heading">
+ GraphQL <span className="lowercase">vs</span> Supabase
+ </h2>
+ <div className="doc-section">
+ <article className="text ">
+ <p>
+ If you have a GraphQL background, you might be wondering if you can fetch your data in a
+ single round-trip. The answer is yes!
+ </p>
+ <p>
+ The syntax is very similar. This example shows how you might achieve the same thing with
+ Apollo GraphQL and Supabase.
+ <br />
+ <br />
+ </p>
+ <h4>Still want GraphQL?</h4>
+ <p>
+ If you still want to use GraphQL, you can. Supabase provides you with a full Postgres
+ database, so as long as your middleware can connect to the database then you can still
+ use the tools you love. You can find the database connection details{' '}
+ <Link href={`/project/${ref}/settings/database`}>in the settings.</Link>
+ </p>
+ </article>
+ <article className="code">
+ <CodeSnippet selectedLang={selectedLang} snippet={localSnippets.withApollo()} />
+ <CodeSnippet selectedLang={selectedLang} snippet={localSnippets.withSupabase()} />
+ </article>
+ </div>
+ </>
+ )
+}
+
+const localSnippets = {
+ cliLogin: () => ({
+ title: 'Login via the CLI with your Personal Access Token',
+ bash: {
+ code: `
+npx supabase login
+`,
+ },
+ }),
+ generateTypes: (ref: string) => ({
+ title: 'Generate types',
+ bash: {
+ code: `
+npx supabase gen types typescript --project-id "${ref}" --schema public > types/supabase.ts
+`,
+ },
+ }),
+ withApollo: () => ({
+ title: 'With Apollo GraphQL',
+ bash: {
+ language: 'js',
+ code: `
+const { loading, error, data } = useQuery(gql\`
+ query GetDogs {
+ dogs {
+ id
+ breed
+ owner {
+ id
+ name
+ }
+ }
+ }
+\`)`,
+ },
+ js: {
+ language: 'js',
+ code: `
+const { loading, error, data } = useQuery(gql\`
+ query GetDogs {
+ dogs {
+ id
+ breed
+ owner {
+ id
+ name
+ }
+ }
+ }
+\`)`,
+ },
+ }),
+ withSupabase: () => ({
+ title: 'With Supabase',
+ bash: {
+ language: 'js',
+ code: `
+const { data, error } = await supabase
+ .from('dogs')
+ .select(\`
+ id, breed,
+ owner (id, name)
+ \`)
+`,
+ },
+ js: {
+ language: 'js',
+ code: `
+const { data, error } = await supabase
+ .from('dogs')
+ .select(\`
+ id, breed,
+ owner (id, name)
+ \`)
+`,
+ },
+ }),
+}
+
+export default Introduction
diff --git a/studio/data/custom-domains/custom-domains-activate-mutation.ts b/studio/data/custom-domains/custom-domains-activate-mutation.ts
index 2fc29e5d231..25e2b1f4a35 100644
--- a/studio/data/custom-domains/custom-domains-activate-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-activate-mutation.ts
@@ -1,5 +1,6 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { post } from 'lib/common/fetch'
+import { API_ADMIN_URL } from 'lib/constants'
import { customDomainKeys } from './keys'
export type CustomDomainActivateVariables = {
@@ -12,7 +13,7 @@ export async function activateCustomDomain({ projectRef }: CustomDomainActivateV
}
const response = await post(
- `${process.env.NEXT_PUBLIC_API_ADMIN_URL}/projects/${projectRef}/custom-hostname/activate`,
+ `${API_ADMIN_URL}/projects/${projectRef}/custom-hostname/activate`,
{}
)
if (response.error) {
diff --git a/studio/data/custom-domains/custom-domains-create-mutation.ts b/studio/data/custom-domains/custom-domains-create-mutation.ts
index 689ed60af47..d3819d521fd 100644
--- a/studio/data/custom-domains/custom-domains-create-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-create-mutation.ts
@@ -1,5 +1,6 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { post } from 'lib/common/fetch'
+import { API_ADMIN_URL } from 'lib/constants'
import { customDomainKeys } from './keys'
export type CustomDomainCreateVariables = {
@@ -16,7 +17,7 @@ export async function createCustomDomain({
}
const response = await post(
- `${process.env.NEXT_PUBLIC_API_ADMIN_URL}/projects/${projectRef}/custom-hostname/initialize`,
+ `${API_ADMIN_URL}/projects/${projectRef}/custom-hostname/initialize`,
{
custom_hostname: customDomain,
}
diff --git a/studio/data/custom-domains/custom-domains-delete-mutation.ts b/studio/data/custom-domains/custom-domains-delete-mutation.ts
index 50e992f4831..7d6ca20f112 100644
--- a/studio/data/custom-domains/custom-domains-delete-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-delete-mutation.ts
@@ -1,5 +1,6 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { delete_ } from 'lib/common/fetch'
+import { API_ADMIN_URL } from 'lib/constants'
import { customDomainKeys } from './keys'
export type CustomDomainDeleteVariables = {
@@ -11,10 +12,7 @@ export async function deleteCustomDomain({ projectRef }: CustomDomainDeleteVaria
throw new Error('projectRef is required')
}
- const response = await delete_(
- `${process.env.NEXT_PUBLIC_API_ADMIN_URL}/projects/${projectRef}/custom-hostname`,
- {}
- )
+ const response = await delete_(`${API_ADMIN_URL}/projects/${projectRef}/custom-hostname`, {})
if (response.error) {
throw response.error
}
diff --git a/studio/data/custom-domains/custom-domains-query.ts b/studio/data/custom-domains/custom-domains-query.ts
index 645f756b8be..182e701a7da 100644
--- a/studio/data/custom-domains/custom-domains-query.ts
+++ b/studio/data/custom-domains/custom-domains-query.ts
@@ -1,5 +1,6 @@
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'lib/common/fetch'
+import { API_ADMIN_URL } from 'lib/constants'
import { useCallback } from 'react'
import { customDomainKeys } from './keys'
@@ -72,12 +73,9 @@ export async function getCustomDomains(
throw new Error('projectRef is required')
}
- const response = (await get(
- `${process.env.NEXT_PUBLIC_API_ADMIN_URL}/projects/${projectRef}/custom-hostname`,
- {
- signal,
- }
- )) as {
+ const response = (await get(`${API_ADMIN_URL}/projects/${projectRef}/custom-hostname`, {
+ signal,
+ })) as {
data: {
errors: any[]
result: CustomDomainResponse
diff --git a/studio/data/custom-domains/custom-domains-reverify-mutation.ts b/studio/data/custom-domains/custom-domains-reverify-mutation.ts
index 8b7e2c8c671..7e4e48113bf 100644
--- a/studio/data/custom-domains/custom-domains-reverify-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-reverify-mutation.ts
@@ -1,5 +1,6 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { post } from 'lib/common/fetch'
+import { API_ADMIN_URL } from 'lib/constants'
import { customDomainKeys } from './keys'
export type CustomDomainReverifyVariables = {
@@ -12,7 +13,7 @@ export async function reverifyCustomDomain({ projectRef }: CustomDomainReverifyV
}
const response = await post(
- `${process.env.NEXT_PUBLIC_API_ADMIN_URL}/projects/${projectRef}/custom-hostname/reverify`,
+ `${API_ADMIN_URL}/projects/${projectRef}/custom-hostname/reverify`,
{}
)
if (response.error) {
diff --git a/studio/lib/constants/index.ts b/studio/lib/constants/index.ts
index 1460874f25d..dfcbaed0ef6 100644
--- a/studio/lib/constants/index.ts
+++ b/studio/lib/constants/index.ts
@@ -3,6 +3,7 @@ export * from './metrics'
export const IS_PLATFORM = process.env.NEXT_PUBLIC_IS_PLATFORM === 'true'
export const API_URL = IS_PLATFORM ? process.env.NEXT_PUBLIC_API_URL : '/api'
+export const API_ADMIN_URL = IS_PLATFORM ? process.env.NEXT_PUBLIC_API_ADMIN_URL : undefined
export const PG_META_URL = IS_PLATFORM
? process.env.PLATFORM_PG_META_URL
: process.env.STUDIO_PG_META_URL
diff --git a/studio/pages/project/[ref]/index.tsx b/studio/pages/project/[ref]/index.tsx
index 59eaea022d1..1c334d1e856 100644
--- a/studio/pages/project/[ref]/index.tsx
+++ b/studio/pages/project/[ref]/index.tsx
@@ -1,6 +1,6 @@
import { observer } from 'mobx-react-lite'
-import { useStore } from 'hooks'
+import { useParams, useStore } from 'hooks'
import { NextPageWithLayout } from 'types'
import { IS_PLATFORM, PROJECT_STATUS } from 'lib/constants'
import { ProjectLayoutWithAuth } from 'components/layouts'
@@ -12,6 +12,7 @@ import OveragesBanner from 'components/ui/OveragesBanner/OveragesBanner'
const Home: NextPageWithLayout = () => {
const { ui } = useStore()
+ const { ref } = useParams()
const project = ui.selectedProject
const projectTier = ui.selectedProject?.subscription_tier
diff --git a/studio/stores/project/ProjectFunctionsStore.ts b/studio/stores/project/ProjectFunctionsStore.ts
index 5e1ae7e4485..9f1297596fd 100644
--- a/studio/stores/project/ProjectFunctionsStore.ts
+++ b/studio/stores/project/ProjectFunctionsStore.ts
@@ -4,6 +4,7 @@ import { keyBy } from 'lodash'
import { get, post, patch, delete_ } from 'lib/common/fetch'
import { UserContent, UserContentMap } from 'types'
import { IRootStore } from '../RootStore'
+import { API_ADMIN_URL } from 'lib/constants'
export interface IProjectFunctionsStore {
isLoading: boolean
@@ -196,7 +197,7 @@ export default class ProjectFunctionsStore implements IProjectFunctionsStore {
setProjectRef(ref?: string) {
if (ref) {
this.projectRef = ref
- this.baseUrl = `${process.env.NEXT_PUBLIC_API_ADMIN_URL}/projects/${ref}/functions`
+ this.baseUrl = `${API_ADMIN_URL}/projects/${ref}/functions`
}
}
}