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