()
| 8 | * Results are cached in memory after the first fetch. |
| 9 | */ |
| 10 | export async function getWebhookEvents(): Promise<string[]> { |
| 11 | if (cachedEvents) { |
| 12 | return cachedEvents; |
| 13 | } |
| 14 | |
| 15 | const cache = new OpenApiCache(); |
| 16 | const loaded = await cache.load(); |
| 17 | |
| 18 | if (!loaded) { |
| 19 | output.debug('Failed to load OpenAPI spec for webhook events'); |
| 20 | return []; |
| 21 | } |
| 22 | |
| 23 | const endpoints = cache.getEndpoints(); |
| 24 | const createWebhookEndpoint = endpoints.find( |
| 25 | e => e.path === '/v1/webhooks' && e.method === 'POST' |
| 26 | ); |
| 27 | |
| 28 | if (!createWebhookEndpoint) { |
| 29 | output.debug('Could not find POST /v1/webhooks endpoint in OpenAPI spec'); |
| 30 | return []; |
| 31 | } |
| 32 | |
| 33 | const bodyFields = cache.getBodyFields(createWebhookEndpoint); |
| 34 | const eventsField = bodyFields.find(f => f.name === 'events'); |
| 35 | |
| 36 | if (!eventsField?.enumValues) { |
| 37 | output.debug('Could not find events enum in webhook endpoint'); |
| 38 | return []; |
| 39 | } |
| 40 | |
| 41 | cachedEvents = eventsField.enumValues.filter( |
| 42 | (v): v is string => typeof v === 'string' |
| 43 | ); |
| 44 | return cachedEvents; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Validates webhook events against the OpenAPI spec. |
no test coverage detected