Skip to content

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

refactor: SupaResponse types

Greg Richardsonbfe1d17fa2dc
diff --git a/studio/data/content/content-create-mutation.ts b/studio/data/content/content-create-mutation.ts
index a7235fd70ec..d6646f3d9e1 100644
--- a/studio/data/content/content-create-mutation.ts
+++ b/studio/data/content/content-create-mutation.ts
@@ -17,7 +17,10 @@ export async function createContent(
signal,
})
- if (response.error) throw response.error
+ if ('error' in response) {
+ throw response.error
+ }
+
return { content: response }
}
diff --git a/studio/data/content/content-delete-mutation.ts b/studio/data/content/content-delete-mutation.ts
index 14653436e14..c382590edc7 100644
--- a/studio/data/content/content-delete-mutation.ts
+++ b/studio/data/content/content-delete-mutation.ts
@@ -16,7 +16,10 @@ export async function deleteContent(
{ signal }
)
- if (response.error) throw response.error
+ if ('error' in response) {
+ throw response.error
+ }
+
return response
}
diff --git a/studio/data/content/content-update-mutation.ts b/studio/data/content/content-update-mutation.ts
index a6c4deb843f..7c87ba22d94 100644
--- a/studio/data/content/content-update-mutation.ts
+++ b/studio/data/content/content-update-mutation.ts
@@ -19,7 +19,7 @@ export async function updateContent(
content,
{ signal }
)
- if (created.error) {
+ if ('error' in created) {
throw created.error
}
diff --git a/studio/data/integrations/integrations-vercel-installed-connection-delete-mutation.ts b/studio/data/integrations/integrations-vercel-installed-connection-delete-mutation.ts
index 58ab5c10fee..aebf6012ef5 100644
--- a/studio/data/integrations/integrations-vercel-installed-connection-delete-mutation.ts
+++ b/studio/data/integrations/integrations-vercel-installed-connection-delete-mutation.ts
@@ -24,7 +24,10 @@ export async function deleteConnection(
{ signal }
)
- if (response.error) throw response.error
+ if ('error' in response) {
+ throw response.error
+ }
+
return response
}
diff --git a/studio/data/subscriptions/org-subscription-update-mutation.ts b/studio/data/subscriptions/org-subscription-update-mutation.ts
index d2592bbcc8f..9c032b3c776 100644
--- a/studio/data/subscriptions/org-subscription-update-mutation.ts
+++ b/studio/data/subscriptions/org-subscription-update-mutation.ts
@@ -2,7 +2,7 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { put } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { subscriptionKeys } from './keys'
-import { SupaResponseV2 } from 'types/base'
+import { SupaResponse } from 'types/base'
export type SubscriptionTier =
| 'tier_free'
@@ -28,10 +28,7 @@ export async function updateOrgSubscription({
const payload: { tier: string; payment_method?: string } = { tier }
if (paymentMethod !== undefined) payload.payment_method = paymentMethod
- const response = (await put(
- `${API_URL}/organizations/${slug}/billing/subscription`,
- payload
- )) as SupaResponseV2<void>
+ const response = await put<void>(`${API_URL}/organizations/${slug}/billing/subscription`, payload)
if (typeof response === 'object' && response !== null && 'error' in response) throw response.error
return response
}
diff --git a/studio/hooks/analytics/useLogsPreview.tsx b/studio/hooks/analytics/useLogsPreview.tsx
index 4957a16e00c..9846b4dec53 100644
--- a/studio/hooks/analytics/useLogsPreview.tsx
+++ b/studio/hooks/analytics/useLogsPreview.tsx
@@ -81,15 +81,12 @@ function useLogsPreview(
// don't overwrite unless user has already clicked on load older
iso_timestamp_end: pageParam || params.iso_timestamp_end,
} as any)}`
- return get<Logs>(
- uri,
- { signal }
- )
+ return get<Logs>(uri, { signal })
},
{
refetchOnWindowFocus: false,
getNextPageParam(lastPage) {
- if ((lastPage?.result?.length ?? 0) === 0) {
+ if ('error' in lastPage || (lastPage?.result?.length ?? 0) === 0) {
return undefined
}
const len = lastPage.result.length
@@ -124,7 +121,7 @@ function useLogsPreview(
}
)
- const newCount = countData?.result?.[0]?.count ?? 0
+ const newCount = countData && !('error' in countData) ? countData.result?.[0]?.count ?? 0 : 0
// chart data
@@ -158,7 +155,7 @@ function useLogsPreview(
let error: null | string | object = rqError ? (rqError as any).message : null
data?.pages.forEach((response) => {
- if (!error && response?.result) {
+ if (!('error' in response) && !error && response?.result) {
logData = [...logData, ...response.result]
}
if (!error && response && response.error) {
@@ -180,7 +177,7 @@ function useLogsPreview(
}
const normalizedEventChartData = useTimeseriesUnixToIso(
- eventChartResponse?.result || [],
+ (eventChartResponse && !('error' in eventChartResponse) && eventChartResponse.result) || [],
'timestamp'
)
diff --git a/studio/hooks/analytics/useLogsQuery.tsx b/studio/hooks/analytics/useLogsQuery.tsx
index 22553558efd..bfd6da5ba61 100644
--- a/studio/hooks/analytics/useLogsQuery.tsx
+++ b/studio/hooks/analytics/useLogsQuery.tsx
@@ -68,7 +68,7 @@ const useLogsQuery = (
return {
params,
isLoading: (enabled && isLoading) || isRefetching,
- logData: data?.result ? data?.result : [],
+ logData: data && !('error' in data) && data.result ? data?.result : [],
error,
changeQuery,
runQuery: () => refetch(),
diff --git a/studio/lib/api/apiWrapper.ts b/studio/lib/api/apiWrapper.ts
index a3bd3484998..9542bd88d68 100644
--- a/studio/lib/api/apiWrapper.ts
+++ b/studio/lib/api/apiWrapper.ts
@@ -17,7 +17,7 @@ export default async function apiWrapper(
if (IS_PLATFORM && withAuth) {
const response = await apiAuthenticate(req, res)
- if (response.error) {
+ if ('error' in response) {
return res.status(401).json({
error: {
message: `Unauthorized: ${response.error.message}`,
diff --git a/studio/lib/integration-utils.ts b/studio/lib/integration-utils.ts
index b16b53efdfc..c6013df73b5 100644
--- a/studio/lib/integration-utils.ts
+++ b/studio/lib/integration-utils.ts
@@ -1,7 +1,7 @@
-import { SupaResponseV2 } from 'types'
+import { SupaResponse } from 'types'
import { Integration } from 'data/integrations/integrations.types'
-async function fetchGitHub<T = any>(url: string, responseJson = true): Promise<SupaResponseV2<T>> {
+async function fetchGitHub<T = any>(url: string, responseJson = true): Promise<SupaResponse<T>> {
const response = await fetch(url)
if (!response.ok) {
return {
diff --git a/studio/stores/common/PostgresMetaInterface.ts b/studio/stores/common/PostgresMetaInterface.ts
index 2d03eb347b7..e12ace8b098 100644
--- a/studio/stores/common/PostgresMetaInterface.ts
+++ b/studio/stores/common/PostgresMetaInterface.ts
@@ -95,7 +95,9 @@ export default class PostgresMetaInterface<T> implements IPostgresMetaInterface<
async fetchData() {
const headers = { 'Content-Type': 'application/json', ...this.headers }
const response = await get<T[]>(this.url, { headers })
- if (response.error) throw response.error
+ if ('error' in response) {
+ throw response.error
+ }
this.setDataArray(response)
return response
@@ -213,7 +215,9 @@ export default class PostgresMetaInterface<T> implements IPostgresMetaInterface<
const headers = { 'Content-Type': 'application/json', ...this.headers }
const url = `${this.url}?id=${id}`
const response = await patch<T>(url, payload, { headers })
- if (response.error) throw response.error
+ if (response && typeof response === 'object' && 'error' in response) {
+ throw response.error
+ }
this.data[id] = response
return response
@@ -227,7 +231,9 @@ export default class PostgresMetaInterface<T> implements IPostgresMetaInterface<
const headers = { 'Content-Type': 'application/json', ...this.headers }
const url = cascade ? `${this.url}?id=${id}&cascade=${cascade}` : `${this.url}?id=${id}`
const response = await delete_<T>(url, {}, { headers })
- if (response.error) throw response.error
+ if (response && typeof response === 'object' && 'error' in response) {
+ throw response.error
+ }
delete this.data[id]
return true
diff --git a/studio/stores/pgmeta/OpenApiStore.ts b/studio/stores/pgmeta/OpenApiStore.ts
index c66a2095989..30fed0a6f81 100644
--- a/studio/stores/pgmeta/OpenApiStore.ts
+++ b/studio/stores/pgmeta/OpenApiStore.ts
@@ -53,7 +53,9 @@ export default class OpenApiStore implements IOpenApiStore {
async fetchData() {
const response = await get<OpenAPIV2.Document>(this.url)
- if (response.error) throw response.error
+ if ('error' in response) {
+ throw response.error
+ }
const tables = response.definitions
? Object.entries(response.definitions).map(([key, table]) => ({
diff --git a/studio/stores/project/ProjectContentStore.ts b/studio/stores/project/ProjectContentStore.ts
index a7d88bb1bb6..c567458647f 100644
--- a/studio/stores/project/ProjectContentStore.ts
+++ b/studio/stores/project/ProjectContentStore.ts
@@ -268,11 +268,13 @@ export default class ProjectContentStore implements IProjectContentStore {
'Content-Type': 'application/json',
}
const created = await post<UserContent>(this.baseUrl, payload, { headers })
- if (created.error) throw created.error
+ if ('error' in created) {
+ throw created.error
+ }
this.savingState = 'IDLE'
- return { data: created as UserContent, error: null }
+ return { data: created, error: null }
} catch (error) {
this.savingState = 'CREATING_FAILED'
@@ -325,7 +327,9 @@ export default class ProjectContentStore implements IProjectContentStore {
const url = `${this.baseUrl}?id=${id}`
const updated = await patch<UserContent[]>(url, payload, { headers })
- if (updated.error) throw updated.error
+ if ('error' in updated) {
+ throw updated.error
+ }
const localUpdate = { ...this.data[id], ...payload }
this.data[id] = localUpdate
diff --git a/studio/types/base.ts b/studio/types/base.ts
index 53bef164213..eb61191087d 100644
--- a/studio/types/base.ts
+++ b/studio/types/base.ts
@@ -83,10 +83,7 @@ export interface ResponseFailure {
error: ResponseError
}
-// [Joshen] This should ideally be | but not changing now as its gonna cause a lot of build errors
-export type SupaResponse<T> = T & ResponseFailure
-// [Joshen] Use this for now, eventually deprecate SupaResponse<T> above
-export type SupaResponseV2<T> = T | ResponseFailure
+export type SupaResponse<T> = T | ResponseFailure
export interface ResponseError {
code: number