Skip to content

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

1.7 KB
Raw Blame
import type { WebhookScope } from './PlatformWebhooks.types'

export const shouldHandleEndpointNotFound = ({
  endpointId,
  hasSelectedEndpoint,
  pendingCreatedEndpointId,
}: {
  endpointId?: string
  hasSelectedEndpoint: boolean
  pendingCreatedEndpointId: string | null
}) => {
  if (!endpointId) return false
  if (hasSelectedEndpoint) return false
  return endpointId !== pendingCreatedEndpointId
}

/**
 * Only redirect away once the flags have loaded and the route params are populated; before
 * that, the flag reads `false` and `slug`/`ref` are undefined even for enabled users.
 */
export const shouldRedirectFromWebhooks = ({
  areFlagsReady,
  isScopeReady,
  isEnabled,
}: {
  areFlagsReady: boolean
  isScopeReady: boolean
  isEnabled: boolean
}) => areFlagsReady && isScopeReady && !isEnabled

interface PendingSigningSecretReveal {
  endpointId: string
  signingSecret: string
}

const pendingSigningSecretRevealByScope: Partial<Record<WebhookScope, PendingSigningSecretReveal>> =
  {}

export const setPendingSigningSecretReveal = (
  scope: WebhookScope,
  value: PendingSigningSecretReveal
) => {
  pendingSigningSecretRevealByScope[scope] = value
}

export const getPendingSigningSecretReveal = (scope: WebhookScope, endpointId?: string) => {
  const pending = pendingSigningSecretRevealByScope[scope]
  if (!pending) return null
  if (endpointId && pending.endpointId !== endpointId) return null
  return pending
}

export const clearPendingSigningSecretReveal = (scope: WebhookScope) => {
  delete pendingSigningSecretRevealByScope[scope]
}

export const resetPendingSigningSecretRevealForTests = () => {
  for (const key of Object.keys(pendingSigningSecretRevealByScope) as WebhookScope[]) {
    delete pendingSigningSecretRevealByScope[key]
  }
}