The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Shift generate types in API docs to RQ
Joshen Lima80fd1a9126c
diff --git a/studio/components/interfaces/Docs/GeneratingTypes.tsx b/studio/components/interfaces/Docs/GeneratingTypes.tsxindex 0ce44019d84..6e09a181cf3 100644--- a/studio/components/interfaces/Docs/GeneratingTypes.tsx+++ b/studio/components/interfaces/Docs/GeneratingTypes.tsx@@ -1,12 +1,12 @@-import { useParams } from 'common/hooks'-import CodeSnippet from 'components/to-be-cleaned/Docs/CodeSnippet'-import { useStore } from 'hooks'-import { get } from 'lib/common/fetch'-import { API_ADMIN_URL } from 'lib/constants'import Link from 'next/link'import { useState } from 'react'import { Button, IconDownload, IconExternalLink } from 'ui'+import { useParams } from 'common/hooks'+import CodeSnippet from 'components/to-be-cleaned/Docs/CodeSnippet'+import { generateTypes } from 'data/projects/project-type-generation-query'+import { useStore } from 'hooks'+interface Props {selectedLang: string}@@ -16,11 +16,10 @@ export default function GeneratingTypes({ selectedLang }: Props) {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) {+ const onClickGenerateTypes = async () => {+ try {+ setIsGeneratingTypes(true)+ const res = await generateTypes({ ref })let element = document.createElement('a')element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(res.types))element.setAttribute('download', 'supabase.ts')@@ -32,16 +31,17 @@ export default function GeneratingTypes({ selectedLang }: Props) {category: 'success',message: `Successfully generated types! File is being downloaded`,})- } else {+ } catch (error: any) {ui.setNotification({+ error,category: 'error',- message: `Failed to generate types: ${res.error.message}`,- error: res.error,+ message: `Failed to generate types: ${error.message}`,})+ } finally {+ setIsGeneratingTypes(false)}-- setIsGeneratingTypes(false)}+return (<><h2 className="doc-heading flex items-center justify-between">@@ -80,13 +80,13 @@ export default function GeneratingTypes({ selectedLang }: Props) {disabled={isGeneratingTypes}loading={isGeneratingTypes}icon={<IconDownload strokeWidth={1.5} />}- onClick={generateTypes}+ onClick={onClickGenerateTypes}>Generate and download types</Button>)}</p>- <p className="text-xs text-scale-1100 bg-scale-200 p-4 mt-2">+ <p className="text-xs text-scale-1100 bg-scale-200 p-4">Remember to re-generate and download this file as you make changes to your tables.</p></div>diff --git a/studio/data/projects/keys.ts b/studio/data/projects/keys.tsindex 0575110e536..c0043e00294 100644--- a/studio/data/projects/keys.ts+++ b/studio/data/projects/keys.ts@@ -1,5 +1,6 @@export const projectKeys = {list: () => ['all-projects'] as const,+ types: (projectRef: string | undefined) => ['project', projectRef, 'types'] as const,detail: (projectRef: string | undefined) => ['project', projectRef, 'detail'] as const,readonlyStatusList: () => ['projects', 'readonly-statuses'] as const,readonlyStatus: (projectRef: string | undefined) =>diff --git a/studio/data/projects/project-detail-query.ts b/studio/data/projects/project-detail-query.tsindex a64643541cf..d6d6e195045 100644--- a/studio/data/projects/project-detail-query.ts+++ b/studio/data/projects/project-detail-query.ts@@ -1,8 +1,9 @@import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'+import { useCallback } from 'react'+import { get, isResponseOk } from 'lib/common/fetch'import { API_URL } from 'lib/constants'-import { useCallback } from 'react'-import { Project } from 'types'+import { Project, ResponseError } from 'types'import { projectKeys } from './keys'export type ProjectDetailVariables = { ref?: string }@@ -16,7 +17,7 @@ export async function getProjectDetail({ ref }: ProjectDetailVariables, signal?:}export type ProjectDetailData = Awaited<ReturnType<typeof getProjectDetail>>-export type ProjectDetailError = unknown+export type ProjectDetailError = ResponseErrorexport const useProjectDetailQuery = <TData = ProjectDetailData>({ ref }: ProjectDetailVariables,diff --git a/studio/data/projects/project-type-generation-query.ts b/studio/data/projects/project-type-generation-query.tsnew file mode 100644index 00000000000..f950459d43f--- /dev/null+++ b/studio/data/projects/project-type-generation-query.ts@@ -0,0 +1,32 @@+import { useQuery, UseQueryOptions } from '@tanstack/react-query'++import { get } from 'data/fetchers'+import { ResponseError } from 'types'+import { projectKeys } from './keys'++export type GenerateTypesVariables = { ref?: string }++export async function generateTypes({ ref }: GenerateTypesVariables, signal?: AbortSignal) {+ if (!ref) throw new Error('Project ref is required')++ const { data, error } = await get(`/v1/projects/{ref}/types/typescript`, {+ params: { path: { ref } },+ signal,+ })++ if (error) throw error+ return data+}++export type GenerateTypesData = Awaited<ReturnType<typeof generateTypes>>+export type GenerateTypesError = ResponseError++export const useGenerateTypesQuery = <TData = GenerateTypesData>(+ { ref }: GenerateTypesVariables,+ { enabled = true, ...options }: UseQueryOptions<GenerateTypesData, GenerateTypesError, TData> = {}+) =>+ useQuery<GenerateTypesData, GenerateTypesError, TData>(+ projectKeys.types(ref),+ ({ signal }) => generateTypes({ ref }, signal),+ { enabled: enabled && typeof ref !== 'undefined', ...options }+ )