Files
supabase/apps/studio/data/platform-webhooks/platform-webhooks-fetchers.ts
kemal.earth 6d8af8e950 chore(api-types): regenerate types from production specs (#54472)
## Problem

`packages/api-types` has drifted from the production OpenAPI specs.
#54394 regenerated them but was reverted in #54404, because the API had
rolled out ahead of its OpenFGA model. That has since been fixed on the
backend, so the regen can land.

...plus I need the new spec for #54391 and didn't want to blow up my
branch.

## Solution

Regenerated with `pnpm --filter=api-types codegen:prod` (production
specs, not the localhost `codegen`), plus the type fixes the new specs
require:

- `platform-webhooks-fetchers.ts` — `page` is now a deep object in the
spec, so the query params move from `page[limit]` keys to a nested
`page` object with a `deepObject` serializer.
- `InvoicesSettings.test.tsx` — four newly-required invoice fields in
the mock.
- `packages/shared-data` — `@supabase/shared-types` 0.1.98 → 0.1.102,
matching `apps/studio`.

Notable additions: the `environment_*` FGA scopes and `GET
/v2/projects/{ref}/analytics/traces/{trace_id}`.

## Review instructions

1. `pnpm --filter=api-types run verify-production-types` — passes.
2. `pnpm typecheck` and `pnpm --filter=studio test` — pass.
3. Only the three non-generated files need reading; the rest is
generated output.

Note: `verify-production-api-types` was removed from the required checks
during the #54394 incident and hasn't been restored. Worth putting back
after this merges.
2026-12-02 13:51:52 +00:00

253 lines
7.9 KiB
TypeScript

import type { operations } from 'api-types'
import { del, get, patch, post, handleError as sharedHandleError } from '@/data/fetchers'
// The real webhooks API nests error details under an `error` key
// (`{ error: { code, message } }`), but the shared `handleError` only reads a
// top-level `message`/`msg`. Flatten the nested fields up so it finds one.
// Rate limiting is expected and already has dedicated UI handling, so it's the
// only case excluded from Sentry capture.
const handleError = (error: unknown): never => {
if (error && typeof error === 'object' && 'error' in error) {
const { error: nested, ...rest } = error
if (nested && typeof nested === 'object') {
const isRateLimit = 'code' in nested && nested.code === 'too_many_requests'
return sharedHandleError({ ...rest, ...nested }, { alwaysCapture: !isRateLimit })
}
}
return sharedHandleError(error)
}
// The spec declares `page` as a deep object, which must serialize as `page[limit]=…` on the wire.
const pageQuerySerializer = { object: { style: 'deepObject', explode: true } } as const
export type WebhookScopeParams =
| { scope: 'organization'; orgSlug: string }
| { scope: 'project'; projectRef: string }
export type WebhookEventType =
operations['v2-organizations-slug-webhooks-endpoints-post']['requestBody']['content']['application/json']['data']['attributes']['event_types'][number]['type']
export interface WebhookEndpointAttributesInput {
url: string
description?: string | null
enabled?: boolean
event_types: { type: WebhookEventType }[]
custom_headers?: Record<string, string>
signing_secret?: string
}
interface ListPageParams {
limit?: number
offset?: number
}
// Every function below is the single place that branches on organization vs project —
// hooks never need to know which URL family they're calling.
export async function listWebhookEndpoints(
scope: WebhookScopeParams,
page?: ListPageParams,
signal?: AbortSignal
) {
const query = {
page: { limit: page?.limit?.toString(), offset: page?.offset?.toString() },
}
const { data, error } =
scope.scope === 'organization'
? await get('/v2/organizations/{slug}/webhooks/endpoints', {
params: { path: { slug: scope.orgSlug }, query },
querySerializer: pageQuerySerializer,
signal,
})
: await get('/v2/projects/{ref}/webhooks/endpoints', {
params: { path: { ref: scope.projectRef }, query },
querySerializer: pageQuerySerializer,
signal,
})
if (error) handleError(error)
return data
}
export async function createWebhookEndpoint(
scope: WebhookScopeParams,
attributes: WebhookEndpointAttributesInput & { signing_secret: string }
) {
const body = { data: { type: 'endpoint' as const, attributes } }
const { data, error } =
scope.scope === 'organization'
? await post('/v2/organizations/{slug}/webhooks/endpoints', {
params: { path: { slug: scope.orgSlug } },
body,
})
: await post('/v2/projects/{ref}/webhooks/endpoints', {
params: { path: { ref: scope.projectRef } },
body,
})
if (error) handleError(error)
return data
}
export async function updateWebhookEndpoint(
scope: WebhookScopeParams,
id: string,
attributes: WebhookEndpointAttributesInput
) {
const body = { data: { type: 'endpoint' as const, attributes } }
const { data, error } =
scope.scope === 'organization'
? await patch('/v2/organizations/{slug}/webhooks/endpoints/{id}', {
params: { path: { slug: scope.orgSlug, id } },
body,
})
: await patch('/v2/projects/{ref}/webhooks/endpoints/{id}', {
params: { path: { ref: scope.projectRef, id } },
body,
})
if (error) handleError(error)
return data
}
export async function regenerateWebhookEndpointSecret(
scope: WebhookScopeParams,
id: string,
signingSecret: string
) {
const body = {
data: { type: 'endpoint' as const, attributes: { signing_secret: signingSecret } },
}
const { data, error } =
scope.scope === 'organization'
? await patch('/v2/organizations/{slug}/webhooks/endpoints/{id}', {
params: { path: { slug: scope.orgSlug, id } },
body,
})
: await patch('/v2/projects/{ref}/webhooks/endpoints/{id}', {
params: { path: { ref: scope.projectRef, id } },
body,
})
if (error) handleError(error)
return data
}
export async function deleteWebhookEndpoint(scope: WebhookScopeParams, id: string) {
const { data, error } =
scope.scope === 'organization'
? await del('/v2/organizations/{slug}/webhooks/endpoints/{id}', {
params: { path: { slug: scope.orgSlug, id } },
})
: await del('/v2/projects/{ref}/webhooks/endpoints/{id}', {
params: { path: { ref: scope.projectRef, id } },
})
if (error) handleError(error)
return data
}
export async function listWebhookDeliveries(
scope: WebhookScopeParams,
endpointId: string,
page?: { size?: number; after?: string; before?: string },
signal?: AbortSignal
) {
const query = {
page: { size: page?.size?.toString(), after: page?.after, before: page?.before },
}
const { data, error } =
scope.scope === 'organization'
? await get('/v2/organizations/{slug}/webhooks/endpoints/{id}/deliveries', {
params: { path: { slug: scope.orgSlug, id: endpointId }, query },
querySerializer: pageQuerySerializer,
signal,
})
: await get('/v2/projects/{ref}/webhooks/endpoints/{id}/deliveries', {
params: { path: { ref: scope.projectRef, id: endpointId }, query },
querySerializer: pageQuerySerializer,
signal,
})
if (error) handleError(error)
return data
}
export async function getWebhookDelivery(
scope: WebhookScopeParams,
id: string,
signal?: AbortSignal
) {
const { data, error } =
scope.scope === 'organization'
? await get('/v2/organizations/{slug}/webhooks/deliveries/{id}', {
params: { path: { slug: scope.orgSlug, id } },
signal,
})
: await get('/v2/projects/{ref}/webhooks/deliveries/{id}', {
params: { path: { ref: scope.projectRef, id } },
signal,
})
if (error) handleError(error)
return data
}
export async function sendTestWebhookEvent(
scope: WebhookScopeParams,
id: string,
eventType?: string
) {
const body = eventType
? {
data: {
type: 'event' as const,
attributes: { type: eventType as Exclude<WebhookEventType, '*'> },
},
}
: {}
const { data, error } =
scope.scope === 'organization'
? await post('/v2/organizations/{slug}/webhooks/endpoints/{id}/test', {
params: { path: { slug: scope.orgSlug, id } },
body,
})
: await post('/v2/projects/{ref}/webhooks/endpoints/{id}/test', {
params: { path: { ref: scope.projectRef, id } },
body,
})
if (error) handleError(error)
return data
}
// Retry and test-event are rate-limited to 10 requests/60s. The documented
// message (api_v2_openapi.json) is "Too Many Requests", but the locally
// running API currently sends "Rate limit exceeded" instead. Accept both
// until that's reconciled.
const RATE_LIMIT_MESSAGES = ['Too Many Requests', 'Rate limit exceeded']
export const isWebhookRateLimitError = (error: { message?: string }) =>
!!error.message && RATE_LIMIT_MESSAGES.includes(error.message)
export async function retryWebhookDelivery(scope: WebhookScopeParams, id: string) {
const { data, error } =
scope.scope === 'organization'
? await post('/v2/organizations/{slug}/webhooks/deliveries/{id}/retry', {
params: { path: { slug: scope.orgSlug, id } },
})
: await post('/v2/projects/{ref}/webhooks/deliveries/{id}/retry', {
params: { path: { ref: scope.projectRef, id } },
})
if (error) handleError(error)
return data
}