From 0aa4793de8bb9838419b014d35bea73a3dc527bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Thu, 26 Sep 2024 13:26:59 +0200 Subject: [PATCH] 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 --- .../Settings/Database/SSLConfiguration.tsx | 6 +++- .../config/project-upgrade-status-query.ts | 33 +++++++------------ .../data/oauth/oauth-app-update-mutation.ts | 22 +++++++------ .../ssl-enforcement/ssl-enforcement-query.ts | 27 ++++++--------- .../ssl-enforcement-update-mutation.ts | 14 ++++---- apps/studio/lib/constants/index.ts | 1 - apps/studio/next-env.d.ts | 2 +- 7 files changed, 46 insertions(+), 59 deletions(-) diff --git a/apps/studio/components/interfaces/Settings/Database/SSLConfiguration.tsx b/apps/studio/components/interfaces/Settings/Database/SSLConfiguration.tsx index edf240567ec..b9621087efb 100644 --- a/apps/studio/components/interfaces/Settings/Database/SSLConfiguration.tsx +++ b/apps/studio/components/interfaces/Settings/Database/SSLConfiguration.tsx @@ -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 && diff --git a/apps/studio/data/config/project-upgrade-status-query.ts b/apps/studio/data/config/project-upgrade-status-query.ts index 24315943109..fcaba8cba71 100644 --- a/apps/studio/data/config/project-upgrade-status-query.ts +++ b/apps/studio/data/config/project-upgrade-status-query.ts @@ -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> @@ -53,7 +42,7 @@ export const useProjectUpgradingStatusQuery = > diff --git a/apps/studio/data/ssl-enforcement/ssl-enforcement-query.ts b/apps/studio/data/ssl-enforcement/ssl-enforcement-query.ts index 2828f01cd3b..142074740e3 100644 --- a/apps/studio/data/ssl-enforcement/ssl-enforcement-query.ts +++ b/apps/studio/data/ssl-enforcement/ssl-enforcement-query.ts @@ -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> diff --git a/apps/studio/data/ssl-enforcement/ssl-enforcement-update-mutation.ts b/apps/studio/data/ssl-enforcement/ssl-enforcement-update-mutation.ts index a301885b600..626c7009814 100644 --- a/apps/studio/data/ssl-enforcement/ssl-enforcement-update-mutation.ts +++ b/apps/studio/data/ssl-enforcement/ssl-enforcement-update-mutation.ts @@ -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> diff --git a/apps/studio/lib/constants/index.ts b/apps/studio/lib/constants/index.ts index f0f27fc4fb4..6b298c47a23 100644 --- a/apps/studio/lib/constants/index.ts +++ b/apps/studio/lib/constants/index.ts @@ -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 diff --git a/apps/studio/next-env.d.ts b/apps/studio/next-env.d.ts index 4f11a03dc6c..a4a7b3f5cfa 100644 --- a/apps/studio/next-env.d.ts +++ b/apps/studio/next-env.d.ts @@ -2,4 +2,4 @@ /// // 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.