Skip to content

Commit 6015e53

Browse files
authored
Chore/download types schemas (supabase#29480)
Read which schemas to download types for
1 parent c970e44 commit 6015e53

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

apps/studio/components/interfaces/Docs/GeneratingTypes.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Link from 'next/link'
22
import { useState } from 'react'
33
import { toast } from 'sonner'
4-
4+
import { useProjectPostgrestConfigQuery } from 'data/config/project-postgrest-config-query'
55
import { useParams } from 'common'
66
import CodeSnippet from 'components/interfaces/Docs/CodeSnippet'
77
import { generateTypes } from 'data/projects/project-type-generation-query'
@@ -16,10 +16,12 @@ export default function GeneratingTypes({ selectedLang }: Props) {
1616
const { ref } = useParams()
1717
const [isGeneratingTypes, setIsGeneratingTypes] = useState(false)
1818

19+
const { data: config } = useProjectPostgrestConfigQuery({ projectRef: ref })
20+
1921
const onClickGenerateTypes = async () => {
2022
try {
2123
setIsGeneratingTypes(true)
22-
const res = await generateTypes({ ref })
24+
const res = await generateTypes({ ref, included_schemas: config?.db_schema })
2325
let element = document.createElement('a')
2426
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(res.types))
2527
element.setAttribute('download', 'supabase.ts')

apps/studio/components/interfaces/ProjectAPIDocs/Content/Entities.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ import ContentSnippet from '../ContentSnippet'
99
import { DOCS_CONTENT } from '../ProjectAPIDocs.constants'
1010
import type { ContentProps } from './Content.types'
1111
import { ExternalLink, Download } from 'lucide-react'
12+
import { useProjectPostgrestConfigQuery } from 'data/config/project-postgrest-config-query'
1213

1314
const Entities = ({ language }: ContentProps) => {
1415
const { ref } = useParams()
1516
const [isGeneratingTypes, setIsGeneratingTypes] = useState(false)
1617

18+
const { data: config } = useProjectPostgrestConfigQuery({ projectRef: ref })
19+
1720
const onClickGenerateTypes = async () => {
1821
try {
1922
setIsGeneratingTypes(true)
20-
const res = await generateTypes({ ref })
23+
const res = await generateTypes({ ref, included_schemas: config?.db_schema })
2124
let element = document.createElement('a')
2225
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(res.types))
2326
element.setAttribute('download', 'supabase.ts')

apps/studio/data/projects/project-type-generation-query.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { get, handleError } from 'data/fetchers'
44
import type { ResponseError } from 'types'
55
import { projectKeys } from './keys'
66

7-
export type GenerateTypesVariables = { ref?: string }
7+
export type GenerateTypesVariables = { ref?: string; included_schemas?: string }
88

9-
export async function generateTypes({ ref }: GenerateTypesVariables, signal?: AbortSignal) {
9+
export async function generateTypes(
10+
{ ref, included_schemas }: GenerateTypesVariables,
11+
signal?: AbortSignal
12+
) {
1013
if (!ref) throw new Error('Project ref is required')
1114

1215
const { data, error } = await get(`/v1/projects/{ref}/types/typescript`, {
13-
params: { path: { ref } },
16+
params: { path: { ref }, query: { included_schemas } },
1417
signal,
1518
})
1619

@@ -22,11 +25,11 @@ export type GenerateTypesData = Awaited<ReturnType<typeof generateTypes>>
2225
export type GenerateTypesError = ResponseError
2326

2427
export const useGenerateTypesQuery = <TData = GenerateTypesData>(
25-
{ ref }: GenerateTypesVariables,
28+
{ ref, included_schemas }: GenerateTypesVariables,
2629
{ enabled = true, ...options }: UseQueryOptions<GenerateTypesData, GenerateTypesError, TData> = {}
2730
) =>
2831
useQuery<GenerateTypesData, GenerateTypesError, TData>(
2932
projectKeys.types(ref),
30-
({ signal }) => generateTypes({ ref }, signal),
33+
({ signal }) => generateTypes({ ref, included_schemas }, signal),
3134
{ enabled: enabled && typeof ref !== 'undefined', ...options }
3235
)

0 commit comments

Comments
 (0)