PlatformWebhooksPage.utils.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import type { WebhookScope } from './PlatformWebhooks.types'
  2. export const shouldHandleEndpointNotFound = ({
  3. endpointId,
  4. hasSelectedEndpoint,
  5. pendingCreatedEndpointId,
  6. }: {
  7. endpointId?: string
  8. hasSelectedEndpoint: boolean
  9. pendingCreatedEndpointId: string | null
  10. }) => {
  11. if (!endpointId) return false
  12. if (hasSelectedEndpoint) return false
  13. return endpointId !== pendingCreatedEndpointId
  14. }
  15. interface PendingSigningSecretReveal {
  16. endpointId: string
  17. signingSecret: string
  18. }
  19. const pendingSigningSecretRevealByScope: Partial<Record<WebhookScope, PendingSigningSecretReveal>> =
  20. {}
  21. export const setPendingSigningSecretReveal = (
  22. scope: WebhookScope,
  23. value: PendingSigningSecretReveal
  24. ) => {
  25. pendingSigningSecretRevealByScope[scope] = value
  26. }
  27. export const getPendingSigningSecretReveal = (scope: WebhookScope, endpointId?: string) => {
  28. const pending = pendingSigningSecretRevealByScope[scope]
  29. if (!pending) return null
  30. if (endpointId && pending.endpointId !== endpointId) return null
  31. return pending
  32. }
  33. export const clearPendingSigningSecretReveal = (scope: WebhookScope) => {
  34. delete pendingSigningSecretRevealByScope[scope]
  35. }
  36. export const resetPendingSigningSecretRevealForTests = () => {
  37. for (const key of Object.keys(pendingSigningSecretRevealByScope) as WebhookScope[]) {
  38. delete pendingSigningSecretRevealByScope[key]
  39. }
  40. }