Skip to content

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

Update RQ templates to use methods from data/fetchers

Joshen Lim0282250bdebc
diff --git a/studio/data/__templates/resource-query.ts b/studio/data/__templates/resource-query.ts
index 022d625a744..25889a82fd0 100644
--- a/studio/data/__templates/resource-query.ts
+++ b/studio/data/__templates/resource-query.ts
@@ -1,7 +1,8 @@
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
-import { get, isResponseOk } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
import { useCallback } from 'react'
+
+import { get } from 'data/fetchers'
+import { ResponseError } from 'types'
import { resourceKeys } from './keys'
export type ResourceVariables = {
@@ -14,28 +15,20 @@ export type ResourceResponse = {
}
export async function getResource({ projectRef, id }: ResourceVariables, signal?: AbortSignal) {
- if (!projectRef) {
- throw new Error('projectRef is required')
- }
- if (!id) {
- throw new Error('id is required')
- }
+ if (!projectRef) throw new Error('projectRef is required')
- const response = await get<ResourceResponse>(
- `${API_URL}/projects/${projectRef}/resources/${id}`,
- {
- signal,
- }
- )
- if (!isResponseOk(response)) {
- throw response.error
- }
+ // @ts-ignore Just a sample here, TS lint will validate if the endpoint is valid
+ const { data, error } = await get(`/platform/projects/{ref}/resources/{id}`, {
+ params: { path: { ref: projectRef, id } },
+ signal,
+ })
+ if (error) throw error
- return response
+ return data
}
export type ResourceData = Awaited<ReturnType<typeof getResource>>
-export type ResourceError = unknown
+export type ResourceError = ResponseError
export const useResourceQuery = <TData = ResourceData>(
{ projectRef, id }: ResourceVariables,
@@ -52,6 +45,7 @@ export const useResourceQuery = <TData = ResourceData>(
/**
* useResourcePrefetch is used for prefetching data. For example, starting a query loading before a page is navigated to.
+ * Feel free to omit if not required
*
* @example
* const prefetch = useResourcePrefetch({ projectRef, id })
diff --git a/studio/data/__templates/resource-update-mutation.ts b/studio/data/__templates/resource-update-mutation.ts
index 0543cdaaa18..150e99ff370 100644
--- a/studio/data/__templates/resource-update-mutation.ts
+++ b/studio/data/__templates/resource-update-mutation.ts
@@ -1,8 +1,7 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
-import { patch } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { patch } from 'data/fetchers'
import { ResponseError } from 'types'
import { resourceKeys } from './keys'
@@ -13,14 +12,14 @@ export type ResourceUpdateVariables = {
}
export async function updateResource({ projectRef, id, updatedParam }: ResourceUpdateVariables) {
- const response = await patch(`${API_URL}/projects/${projectRef}/resources/${id}`, {
- updated_param: updatedParam,
+ // @ts-ignore Just sample, TS lint will validate if the endpoint is valid
+ const { data, error } = await patch('/platform/projects/{ref}/resources/{id}', {
+ params: { path: { ref: projectRef, id } },
+ body: { updatedParam },
})
- if (response.error) {
- throw response.error
- }
- return response
+ if (error) throw error
+ return data
}
type ResourceUpdateData = Awaited<ReturnType<typeof updateResource>>
diff --git a/studio/data/__templates/resources-query.ts b/studio/data/__templates/resources-query.ts
index 4a46ac33af4..4167d032a1a 100644
--- a/studio/data/__templates/resources-query.ts
+++ b/studio/data/__templates/resources-query.ts
@@ -1,8 +1,9 @@
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'data/fetchers'
import { useCallback } from 'react'
-import { resourceKeys } from './keys'
+
+import { get } from 'data/fetchers'
import { ResponseError } from 'types'
+import { resourceKeys } from './keys'
export type ResourcesVariables = {
projectRef?: string