Files
supabase/apps/studio/data/platform-webhooks/platform-webhooks-mappers.ts
Monica Khoury d0cf8dbc50 fix: align webhook FE with updated API changes (#51800)
## What
Aligns the webhooks FE with backend changes confirmed by Pawel
(FE-4621):

1. `custom_headers` is optional but never null, matches the updated
contract.
2. Removed the client-only `name` field (never persisted by the API).
Display name is now the endpoint's URL plus its subscribed event types.
3. The deliveries list endpoint now returns the full `event` object, so
`eventType`/`eventPayload` are always populated. Added an Event column
to the deliveries table.

Also regenerates `packages/api-types` from production to pick up these
changes.

While testing, found and fixed two more related issues:

4. A delivery can briefly exist before the backend finishes attaching
its event data, regardless of what triggered it (test, retry, or real
traffic). The deliveries list now polls every 5s while open, so new or
still-settling deliveries fill in automatically instead of requiring a
manual refresh.
5. "Send test event" always defaulted to an unrelated sample event type.
The test endpoint actually accepts a specific `event_type`, so this now
sends the endpoint's own subscribed type automatically, or shows a
picker when there's more than one to choose from.

## Testing
All webhooks component/unit tests pass (30/30), typecheck and lint
clean. Manually verified on localhost against a live backend.

## Note
This may clash with #51614 (`custom_headers: null` in a test fixture, no
longer a valid value once this merges). Will follow up there after this
lands.
2026-10-15 20:17:56 +03:00

76 lines
2.9 KiB
TypeScript

import type { operations } from 'api-types'
import * as z from 'zod'
import type { WebhookEndpointAttributesInput, WebhookEventType } from './platform-webhooks-fetchers'
import { PLATFORM_WEBHOOK_EVENT_TYPES } from '@/components/interfaces/Platform/Webhooks/PlatformWebhooks.constants'
import type {
UpsertWebhookEndpointInput,
WebhookDelivery,
WebhookEndpoint,
} from '@/components/interfaces/Platform/Webhooks/PlatformWebhooks.types'
// '*' (subscribe to all) is handled separately by the form's "subscribe all" toggle,
// so it's not part of the selectable catalog, but it's still a value the API accepts.
const webhookEventTypeSchema = z.enum([...PLATFORM_WEBHOOK_EVENT_TYPES, '*'])
const toWebhookEventTypes = (eventTypes: string[]): { type: WebhookEventType }[] =>
eventTypes
.map((eventType) => webhookEventTypeSchema.safeParse(eventType))
.filter((result) => result.success)
.map((result) => ({ type: result.data }))
const toCustomHeaders = (
headers: Array<{ key: string; value: string }>
): Record<string, string> | undefined =>
headers.length > 0
? Object.fromEntries(headers.map((header) => [header.key, header.value]))
: undefined
export const toWebhookEndpointAttributes = (
input: UpsertWebhookEndpointInput
): WebhookEndpointAttributesInput => ({
url: input.url,
description: input.description || null,
enabled: input.enabled,
event_types: toWebhookEventTypes(input.eventTypes),
custom_headers: toCustomHeaders(input.customHeaders),
signing_secret: input.signingSecret || undefined,
})
type WebhookEndpointResource =
operations['v2-organizations-slug-webhooks-endpoints-id-get']['responses'][200]['content']['application/json']['data']
type WebhookDeliveryResource =
operations['v2-organizations-slug-webhooks-deliveries-id-get']['responses'][200]['content']['application/json']['data']
export const toWebhookEndpoint = (resource: WebhookEndpointResource): WebhookEndpoint => ({
id: resource.id,
url: resource.attributes.url,
description: resource.attributes.description ?? '',
enabled: resource.attributes.enabled,
eventTypes: resource.attributes.event_types.map((eventType) => eventType.type),
customHeaders: Object.entries(resource.attributes.custom_headers ?? {}).map(([key, value]) => ({
id: key,
key,
value,
})),
createdBy: resource.attributes.created_by,
createdAt: resource.attributes.created_at,
})
export const toWebhookDelivery = (
resource: WebhookDeliveryResource,
endpointId: string
): WebhookDelivery => ({
id: resource.id,
endpointId,
eventId: resource.attributes.event_id,
eventType: resource.attributes.event?.type ?? '',
eventPayload: resource.attributes.event?.payload ?? {},
status: resource.attributes.status,
responseCode: resource.attributes.response_code,
responseBody: resource.attributes.response_body,
responseHeaders: resource.attributes.response_headers,
attemptAt: resource.attributes.attempt_timestamp,
})