The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Add generating types to api preview
Terry Sutton805fbff496a5
diff --git a/studio/components/interfaces/Docs/GeneratingTypes.tsx b/studio/components/interfaces/Docs/GeneratingTypes.tsxnew file mode 100644index 00000000000..0a6d54579d2--- /dev/null+++ b/studio/components/interfaces/Docs/GeneratingTypes.tsx@@ -0,0 +1,112 @@+import { FC, useState } from 'react'+import Link from 'next/link'+import React from 'react'+import { Button, IconDownload, IconExternalLink } from 'ui'+import { API_ADMIN_URL } from 'lib/constants'+import { get } from 'lib/common/fetch'+import { useParams, useStore } from 'hooks'+import CodeSnippet from 'components/to-be-cleaned/Docs/CodeSnippet'++interface Props {+ selectedLang: string+}++export default function GeneratingTypes({ selectedLang }: Props) {+ 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 flex items-center justify-between">+ <span>Generating types</span>+ <Link href="https://supabase.com/docs/guides/database/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/database/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>+ </>+ )+}++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+`,+ },+ }),+}diff --git a/studio/components/to-be-cleaned/Docs/LangSelector.tsx b/studio/components/interfaces/Docs/LangSelector.tsxsimilarity index 100%rename from studio/components/to-be-cleaned/Docs/LangSelector.tsxrename to studio/components/interfaces/Docs/LangSelector.tsxdiff --git a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsxindex 30d3e925357..a084a3ea1b9 100644--- a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx+++ b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx@@ -17,9 +17,10 @@ import { GeneralContent, ResourceContent } from '../Docs'import { useProjectApiQuery } from 'data/config/project-api-query'import { useProjectJsonSchemaQuery } from 'data/docs/project-json-schema-query'import { snakeToCamel } from 'lib/helpers'-import LangSelector from 'components/to-be-cleaned/Docs/LangSelector'import { JsonEditValue } from './SidePanelEditor/RowEditor/RowEditor.types'import Introduction from 'components/to-be-cleaned/Docs/Pages/Tables/Introduction'+import LangSelector from '../Docs/LangSelector'+import GeneratingTypes from '../Docs/GeneratingTypes'interface Props {/** Theme for the editor */@@ -70,7 +71,7 @@ const TableGridEditor: FC<Props> = ({const { meta, ui, vault } = useStore()const router = useRouter()const { query } = router- const { ref: projectRef } = useParams()+ const { ref: projectRef, page } = useParams()const gridRef = useRef<SupabaseGridRef>(null)const tables = meta.tables.list()@@ -330,9 +331,11 @@ const TableGridEditor: FC<Props> = ({autoApiService={autoApiService}selectedLang={selectedLang}showApiKey={true}- page="table-editor"+ page={page}/>+ <GeneratingTypes selectedLang={selectedLang} />+{jsonSchema?.definitions && (<ResourceContentautoApiService={autoApiService}diff --git a/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.tsx b/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.tsxindex 8ef26409da3..ba2441ad011 100644--- a/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.tsx+++ b/studio/components/to-be-cleaned/Docs/Pages/Tables/Introduction.tsx@@ -1,10 +1,8 @@-import { FC, useState } from 'react'+import { FC } 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 { useParams } from 'hooks'import CodeSnippet from '../../CodeSnippet'+import GeneratingTypes from 'components/interfaces/Docs/GeneratingTypes'interface Props {selectedLang: string@@ -12,35 +10,6 @@ interface Props {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 (<>@@ -65,48 +34,7 @@ const Introduction: FC<Props> = ({ selectedLang }) => {<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/database/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/database/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>+ <GeneratingTypes selectedLang={selectedLang} /><h2 className="doc-heading">GraphQL <span className="lowercase">vs</span> Supabase@@ -141,22 +69,6 @@ const Introduction: FC<Props> = ({ selectedLang }) => {}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: {diff --git a/studio/pages/project/[ref]/api/index.tsx b/studio/pages/project/[ref]/api/index.tsxindex 7e8216d627f..5f4e22b9031 100644--- a/studio/pages/project/[ref]/api/index.tsx+++ b/studio/pages/project/[ref]/api/index.tsx@@ -10,7 +10,7 @@ import { DocsLayout } from 'components/layouts'import { GeneralContent, ResourceContent, RpcContent } from 'components/interfaces/Docs'import { PermissionAction } from '@supabase/shared-types/out/constants'import { useProjectJsonSchemaQuery } from 'data/docs/project-json-schema-query'-import LangSelector from 'components/to-be-cleaned/Docs/LangSelector'+import LangSelector from 'components/interfaces/Docs/LangSelector'const PageContext = createContext(null)@@ -79,11 +79,6 @@ const DocView: FC<any> = observer(({}) => {: undefinedconst swaggerUrl = data?.autoApiService?.restUrl- const canReadServiceKey = checkPermissions(- PermissionAction.READ,- 'service_api_keys.service_role_key'- )-const {data: jsonSchema,error: jsonSchemaError,