Files
supabase/apps/studio/data/misc/user-ip-address-query.ts
Joshen Lim 232a26a68d Add custom error handler around fetch method (#35562)
* Add custom error handler for fetch error 'Failed to fetch'

* Refactor EdgeFunctionTesterSheet to use react query instead of manually calling fetch

* Use new fetchHandler for where we're calling fetch

* Revert
2025-05-09 15:22:55 +08:00

29 lines
930 B
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { fetchHandler } from 'data/fetchers'
import { BASE_PATH, IS_PLATFORM } from 'lib/constants'
import type { ResponseError } from 'types'
import { miscKeys } from './keys'
export async function getUserIPAddress() {
try {
const data = await fetchHandler(`${BASE_PATH}/api/get-ip-address`).then((res) => res.json())
return data.ipAddress
} catch (error) {
throw error
}
}
export type UserIPAddressData = Awaited<ReturnType<typeof getUserIPAddress>>
export type UserIPAddressError = ResponseError
export const useUserIPAddressQuery = <TData = UserIPAddressData>({
enabled = true,
...options
}: UseQueryOptions<UserIPAddressData, UserIPAddressError, TData> = {}) =>
useQuery<UserIPAddressData, UserIPAddressError, TData>(
miscKeys.ipAddress(),
() => getUserIPAddress(),
{ enabled: enabled && !IS_PLATFORM, ...options }
)