Skip to content

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

chore: Remove OpenApi mobx Store (#19778)

Ivan Vasilov2c990284faea
diff --git a/apps/studio/components/layouts/DocsLayout/DocsLayout.tsx b/apps/studio/components/layouts/DocsLayout/DocsLayout.tsx
index ad531768f7a..f67a6976615 100644
--- a/apps/studio/components/layouts/DocsLayout/DocsLayout.tsx
+++ b/apps/studio/components/layouts/DocsLayout/DocsLayout.tsx
@@ -1,22 +1,28 @@
import { observer } from 'mobx-react-lite'
import { useRouter } from 'next/router'
-import { ReactElement, useEffect } from 'react'
+import { ReactElement } from 'react'
+import { useParams } from 'common'
+import { useIsAPIDocsSidePanelEnabled } from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'
import Error from 'components/ui/Error'
import ProductMenu from 'components/ui/ProductMenu'
-import { useIsFeatureEnabled, useSelectedProject, useStore, withAuth } from 'hooks'
+import { useOpenAPISpecQuery } from 'data/open-api/api-spec-query'
+import { useIsFeatureEnabled, useSelectedProject, withAuth } from 'hooks'
import { PROJECT_STATUS } from 'lib/constants'
import ProjectLayout from '../'
import { generateDocsMenu } from './DocsLayout.utils'
-import { useIsAPIDocsSidePanelEnabled } from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'
function DocsLayout({ title, children }: { title: string; children: ReactElement }) {
const router = useRouter()
- const { ui, meta } = useStore()
- const { data, isLoading, error } = meta.openApi
+ const { ref } = useParams()
const selectedProject = useSelectedProject()
const isPaused = selectedProject?.status === PROJECT_STATUS.INACTIVE
+ const { data, isLoading, error } = useOpenAPISpecQuery(
+ { projectRef: ref },
+ { enabled: !isPaused }
+ )
+
const isNewAPIDocsEnabled = useIsAPIDocsSidePanelEnabled()
const hideMenu = isNewAPIDocsEnabled && router.pathname.endsWith('/graphiql')
@@ -30,12 +36,6 @@ function DocsLayout({ title, children }: { title: string; children: ReactElement
return (page || resource || '') as string
}
- useEffect(() => {
- if (ui.selectedProjectRef && !isPaused) {
- meta.openApi.load()
- }
- }, [ui.selectedProjectRef, isPaused])
-
if (error) {
return (
<ProjectLayout product="API Docs">
diff --git a/apps/studio/stores/pgmeta/MetaStore.ts b/apps/studio/stores/pgmeta/MetaStore.ts
index ca42f8796ed..9be48411471 100644
--- a/apps/studio/stores/pgmeta/MetaStore.ts
+++ b/apps/studio/stores/pgmeta/MetaStore.ts
@@ -17,7 +17,6 @@ import { ResponseError } from 'types'
import { IPostgresMetaInterface } from '../common/PostgresMetaInterface'
import { IRootStore } from '../RootStore'
-import OpenApiStore, { IOpenApiStore } from './OpenApiStore'
import TableStore, { ITableStore } from './TableStore'
import {
@@ -44,7 +43,6 @@ const BATCH_SIZE = 1000
const CHUNK_SIZE = 1024 * 1024 * 0.1 // 0.1MB
export interface IMetaStore {
- openApi: IOpenApiStore
tables: ITableStore
columns: IPostgresMetaInterface<PostgresColumn>
views: IViewStore
@@ -117,7 +115,6 @@ export interface IMetaStore {
}
export default class MetaStore implements IMetaStore {
rootStore: IRootStore
- openApi: OpenApiStore
tables: TableStore
columns: PostgresMetaInterface<PostgresColumn>
views: ViewStore
@@ -139,10 +136,6 @@ export default class MetaStore implements IMetaStore {
this.headers['x-connection-encrypted'] = connectionString
}
- this.openApi = new OpenApiStore(
- this.rootStore,
- `${API_URL}/projects/${this.projectRef}/api/rest`
- )
this.tables = new TableStore(this.rootStore, `${this.baseUrl}/tables`, this.headers)
this.columns = new PostgresMetaInterface(
this.rootStore,
@@ -834,9 +827,6 @@ export default class MetaStore implements IMetaStore {
this.headers['x-connection-encrypted'] = connectionString
}
- this.openApi.setUrl(`${API_URL}/projects/${this.projectRef}/api/rest`)
- this.openApi.setHeaders(this.headers)
-
this.tables.setUrl(`${this.baseUrl}/tables`)
this.tables.setHeaders(this.headers)
diff --git a/apps/studio/stores/pgmeta/OpenApiStore.ts b/apps/studio/stores/pgmeta/OpenApiStore.ts
deleted file mode 100644
index 08cf0e505da..00000000000
--- a/apps/studio/stores/pgmeta/OpenApiStore.ts
+++ /dev/null
@@ -1,137 +0,0 @@
-import { makeAutoObservable } from 'mobx'
-import { get, isResponseOk } from 'lib/common/fetch'
-import { IRootStore } from '../RootStore'
-import { OpenAPIV2 } from 'openapi-types'
-
-export interface IOpenApiStore {
- data?: {
- data: OpenAPIV2.Document
- tables: { [key: string]: any }[]
- functions: { [key: string]: any }[]
- }
- error: Error | null
-
- hasError: boolean
- isLoading: boolean
-
- load: () => void
-
- setUrl: (url: string) => void
- setHeaders: (headers: { [prop: string]: any }) => void
-}
-export default class OpenApiStore implements IOpenApiStore {
- STATES = {
- INITIAL: 'initial',
- LOADING: 'loading',
- ERROR: 'error',
- LOADED: 'loaded',
- }
- rootStore: IRootStore
- url: string
- error: Error | null = null
- state = this.STATES.INITIAL
- data?: {
- data: OpenAPIV2.Document
- tables: { [key: string]: any }[]
- functions: { [key: string]: any }[]
- }
- headers: any = {}
-
- constructor(
- rootStore: IRootStore,
- dataUrl: string,
- headers?: {
- [prop: string]: any
- }
- ) {
- this.rootStore = rootStore
- this.url = dataUrl
- this.headers = headers
-
- makeAutoObservable(this)
- }
-
- async fetchData() {
- const response = await get<OpenAPIV2.Document>(this.url)
- if (!isResponseOk(response)) {
- throw response.error
- }
-
- const tables = response.definitions
- ? Object.entries(response.definitions).map(([key, table]) => ({
- ...table,
- name: key,
- fields: Object.entries(table.properties || {}).map(([key, field]) => ({
- ...field,
- name: key,
- })),
- }))
- : []
-
- const functions = response.paths
- ? Object.entries(response.paths)
- .map(([path, value]: any) => ({
- ...value,
- path,
- name: path.replace('/rpc/', ''),
- }))
- .filter((x) => x.path.includes('/rpc'))
- .sort((a, b) => a.name.localeCompare(b.name))
- : []
-
- const payload = { data: response, tables, functions }
- this.setData(payload)
- return payload
- }
-
- async load() {
- let { LOADING, ERROR, LOADED } = this.STATES
- try {
- this.setError(null)
- this.setState(LOADING)
- await this.fetchData()
- this.setState(LOADED)
- } catch (e: any) {
- console.error('load e.message', e.message)
- this.setError(e)
- this.setState(ERROR)
- }
- }
-
- get hasError() {
- return this.state === this.STATES.ERROR
- }
-
- get isLoading() {
- return this.state === this.STATES.INITIAL || this.state === this.STATES.LOADING
- }
-
- setState(value: string) {
- this.state = value
- }
-
- setData(value: {
- data: OpenAPIV2.Document
- tables: { [key: string]: any }[]
- functions: { [key: string]: any }[]
- }) {
- this.data = value
- }
-
- setError(value: any) {
- this.error = value
- }
-
- setUrl(url: string) {
- this.url = url
-
- // if the url changes, we need to reset the state
- this.state = this.STATES.INITIAL
- this.data = undefined
- this.error = null
- }
-
- setHeaders(headers: { [prop: string]: any }) {
- this.headers = headers
- }
-}