ref: Unify /v1 api requests, remove API_ADMIN_URL constant (#29529)

* ref: Unify /v1 api requests, remove API_ADMIN_URL constant

* remove custom types

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
This commit is contained in:
Kamil OgórekandAlaister Young authored and GitHub committed 2024-09-26 11:26:59 +00:00
1 parent c69ac64ad5
commit 0aa4793de8
7 files changed
+46 -59

No files matched your search

@@ -58,7 +58,11 @@ const SSLConfiguration = () => {
sslEnforcementConfiguration.currentConfig.database
: false
const hasAccessToSSLEnforcement = !sslEnforcementConfiguration?.isNotAllowed
const hasAccessToSSLEnforcement = !(
sslEnforcementConfiguration !== undefined &&
'isNotAllowed' in sslEnforcementConfiguration &&
sslEnforcementConfiguration.isNotAllowed
)
const env = process.env.NEXT_PUBLIC_ENVIRONMENT === 'prod' ? 'prod' : 'staging'
const hasSSLCertificate =
projectSettings?.project !== undefined &&
@@ -1,11 +1,7 @@
import { DatabaseUpgradeStatus } from '@supabase/shared-types/out/events'
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
import {
DatabaseUpgradeError,
DatabaseUpgradeStatus,
DatabaseUpgradeProgress,
} from '@supabase/shared-types/out/events'
import { get } from 'lib/common/fetch'
import { API_ADMIN_URL, PROJECT_STATUS } from 'lib/constants'
import { get, handleError } from 'data/fetchers'
import { PROJECT_STATUS } from 'lib/constants'
import { configKeys } from './keys'
export type ProjectUpgradingStatusVariables = {
@@ -13,26 +9,19 @@ export type ProjectUpgradingStatusVariables = {
projectStatus?: string
}
export type ProjectUpgradingStatusResponse = {
databaseUpgradeStatus: {
error?: DatabaseUpgradeError
progress?: DatabaseUpgradeProgress
status: DatabaseUpgradeStatus
initiated_at: string
target_version: number
} | null
}
export async function getProjectUpgradingStatus(
{ projectRef }: ProjectUpgradingStatusVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const response = await get(`${API_ADMIN_URL}/projects/${projectRef}/upgrade/status`, { signal })
if (response.error) throw response.error
const { data, error } = await get(`/v1/projects/{ref}/upgrade/status`, {
params: { path: { ref: projectRef } },
signal,
})
if (error) handleError(error)
return response as ProjectUpgradingStatusResponse
return data
}
export type ProjectUpgradingStatusData = Awaited<ReturnType<typeof getProjectUpgradingStatus>>
@@ -53,7 +42,7 @@ export const useProjectUpgradingStatusQuery = <TData = ProjectUpgradingStatusDat
{
enabled: enabled && typeof projectRef !== 'undefined',
refetchInterval(data) {
const response = data as unknown as ProjectUpgradingStatusResponse
const response = data as unknown as ProjectUpgradingStatusData
if (!response) return false
const interval =
@@ -68,7 +57,7 @@ export const useProjectUpgradingStatusQuery = <TData = ProjectUpgradingStatusDat
return interval
},
onSuccess(data) {
const response = data as unknown as ProjectUpgradingStatusResponse
const response = data as unknown as ProjectUpgradingStatusData
if (response.databaseUpgradeStatus?.status === DatabaseUpgradeStatus.Upgraded) {
client.invalidateQueries(configKeys.upgradeEligibility(projectRef))
}
@@ -2,10 +2,9 @@ import type { OAuthScope } from '@supabase/shared-types/out/constants'
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { put } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import type { ResponseError } from 'types'
import { oauthAppKeys } from './keys'
import { handleError, put } from 'data/fetchers'
export type OAuthAppUpdateVariables = {
id: string
@@ -32,15 +31,18 @@ export async function updateOAuthApp({
if (!website) throw new Error('OAuth app URL is required')
if (!redirect_uris || redirect_uris.length === 0) throw new Error('Redirect URIs are required')
const response = await put(`${API_URL}/organizations/${slug}/oauth/apps/${id}`, {
name,
website,
icon,
scopes,
redirect_uris,
const { data, error } = await put(`/platform/organizations/{slug}/oauth/apps/{id}`, {
params: { path: { id, slug } },
body: {
name,
website,
icon: icon as undefined, // Generated type is incorrect an allows for `string | undefined` only, while we need `null` here.
scopes,
redirect_uris,
},
})
if (response.error) throw response.error
return response
if (error) throw handleError(error)
return data
}
type OAuthAppUpdateData = Awaited<ReturnType<typeof updateOAuthApp>>
@@ -1,46 +1,39 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
import { get, handleError } from 'data/fetchers'
import { sslEnforcementKeys } from './keys'
export type SSLEnforcementVariables = { projectRef?: string }
export type SSLEnforcementResponse = {
appliedSuccessfully: boolean
currentConfig: { database: boolean }
error?: any
isNotAllowed?: boolean
}
export async function getSSLEnforcementConfiguration(
{ projectRef }: SSLEnforcementVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const response = (await get(`${API_ADMIN_URL}/projects/${projectRef}/ssl-enforcement`, {
const { data, error } = await get(`/v1/projects/{ref}/ssl-enforcement`, {
params: { path: { ref: projectRef } },
signal,
})) as SSLEnforcementResponse
})
// Not allowed error is a valid response to denote if a project
// has access to the SSL enforcement UI, so we'll handle it here
if (response.error) {
if (error) {
const isNotAllowedError =
(response.error as any)?.code === 400 &&
(response.error as any)?.message?.includes('not allowed to configure SSL enforcements')
(error as any)?.code === 400 &&
(error as any)?.message?.includes('not allowed to configure SSL enforcements')
if (isNotAllowedError) {
return {
appliedSuccessfully: false,
currentConfig: { database: false },
isNotAllowed: true,
} as SSLEnforcementResponse
} as const
} else {
throw response.error
handleError(error)
}
}
return response as SSLEnforcementResponse
return data
}
export type SSLEnforcementData = Awaited<ReturnType<typeof getSSLEnforcementConfiguration>>
@@ -1,10 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { put } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
import type { ResponseError } from 'types'
import { sslEnforcementKeys } from './keys'
import { handleError, put } from 'data/fetchers'
export type SSLEnforcementUpdateVariables = {
projectRef: string
@@ -23,12 +22,13 @@ export async function updateSSLEnforcement({
}: SSLEnforcementUpdateVariables) {
if (!projectRef) throw new Error('projectRef is required')
const response = (await put(`${API_ADMIN_URL}/projects/${projectRef}/ssl-enforcement`, {
requestedConfig,
})) as SSLEnforcementUpdateResponse
if (response.error) throw response.error
const { data, error } = await put(`/v1/projects/{ref}/ssl-enforcement`, {
params: { path: { ref: projectRef } },
body: { requestedConfig },
})
return response
if (error) handleError(error)
return data
}
type SSLEnforcementUpdateData = Awaited<ReturnType<typeof updateSSLEnforcement>>
-1
View File
@@ -5,7 +5,6 @@ export * from './infrastructure'
export const IS_PLATFORM = process.env.NEXT_PUBLIC_IS_PLATFORM === 'true'
export const DEFAULT_HOME = IS_PLATFORM ? '/projects' : '/project/default'
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
+1 -1
View File
@@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.