pingPostgrest.ts 1001 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { API_URL } from './constants'
  2. import { fetchHeadWithTimeout } from '@/data/fetchers'
  3. const DEFAULT_TIMEOUT_MILLISECONDS = 2000
  4. /**
  5. * Ping Postgrest for health check. Default timeout in 2s.
  6. *
  7. * @param restUrl project rest url
  8. * @param apikey project internal api key
  9. * @param options optional, include custom timeout in milliseconds
  10. *
  11. * @return true if ping is successful else false
  12. */
  13. async function pingPostgrest(
  14. projectRef: string,
  15. options?: {
  16. timeout?: number
  17. }
  18. ) {
  19. if (projectRef === undefined) return false
  20. const { timeout } = options ?? {}
  21. return pingOpenApi(projectRef, timeout)
  22. }
  23. export default pingPostgrest
  24. /**
  25. * Send a HEAD request to postgrest OpenAPI.
  26. *
  27. * @return true if there's no error else false
  28. */
  29. async function pingOpenApi(ref: string, timeout?: number) {
  30. const { error } = await fetchHeadWithTimeout(`${API_URL}/projects/${ref}/api/rest`, [], {
  31. timeout: timeout ?? DEFAULT_TIMEOUT_MILLISECONDS,
  32. })
  33. return error === undefined
  34. }