Skip to content

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

1.3 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'
import { components } from 'api-types'

import { jwtSigningKeysKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from '@/types'

export type JWTSigningKey = components['schemas']['SigningKeyResponse_Output']

export type JWTAlgorithm = components['schemas']['SigningKeyResponse_Output']['algorithm']

interface JWTSigningKeysVariables {
  projectRef?: string
}

async function getJWTSigningKeys({ projectRef }: JWTSigningKeysVariables, signal?: AbortSignal) {
  if (!projectRef) throw new Error('projectRef is required')

  const { data, error } = await get(`/v1/projects/{ref}/config/auth/signing-keys`, {
    params: { path: { ref: projectRef } },
    signal,
  })

  if (error) {
    handleError(error)
  }

  return data
}

export type JWTSigningKeysData = Awaited<ReturnType<typeof getJWTSigningKeys>>

export const useJWTSigningKeysQuery = <TData = JWTSigningKeysData>(
  { projectRef }: JWTSigningKeysVariables,
  { enabled, ...options }: UseCustomQueryOptions<JWTSigningKeysData, ResponseError, TData> = {}
) =>
  useQuery<JWTSigningKeysData, ResponseError, TData>({
    queryKey: jwtSigningKeysKeys.list(projectRef),
    queryFn: ({ signal }) => getJWTSigningKeys({ projectRef }, signal),
    enabled: enabled && !!projectRef,
    ...options,
  })