Files
supabase/apps/studio/lib/pingPostgrest.ts
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

66 lines
1.7 KiB
TypeScript

import semver from 'semver'
import { headWithTimeout, getWithTimeout } from './common/fetch'
import { API_URL } from './constants'
const DEFAULT_TIMEOUT_MILLISECONDS = 2000
/**
* Ping Postgrest for health check. Default timeout in 2s.
*
* Project with version gte 'kps-v3.8.6', we can ping the health-check api
* else ping the OpenApi url
*
* @param restUrl project rest url
* @param apikey project internal api key
* @param options optional, include project kpsVersion or custom timeout in milliseconds
*
* @return true if ping is successful else false
*/
async function pingPostgrest(
projectRef: string,
options?: {
kpsVersion?: string
timeout?: number
}
) {
if (projectRef === undefined) return false
const { kpsVersion, timeout } = options ?? {}
const healthCheckApiEnable = semver.gte(
// @ts-ignore
semver.coerce(kpsVersion ?? 'kps-v0.0.1'),
semver.coerce('kps-v3.8.6')
)
if (healthCheckApiEnable) {
return pingHealthCheckApi(projectRef, timeout)
} else {
return pingOpenApi(projectRef, timeout)
}
}
export default pingPostgrest
/**
* Send a HEAD request to postgrest OpenAPI.
*
* @return true if there's no error else false
*/
async function pingOpenApi(ref: string, timeout?: number) {
const { error } = await headWithTimeout(`${API_URL}/projects/${ref}/api/rest`, [], {
timeout: timeout ?? DEFAULT_TIMEOUT_MILLISECONDS,
})
return error === undefined
}
/**
* Send a GET request to postgrest health check api.
*
* @return true if there's no error else false
*/
async function pingHealthCheckApi(ref: string, timeout?: number) {
const { error } = await getWithTimeout(`${API_URL}/projects/${ref}/live`, {
timeout: timeout ?? DEFAULT_TIMEOUT_MILLISECONDS,
})
return error === undefined
}