File
Blob: src/worker/api/webhook-shared.ts
| 1 | import { type WebhookProviderConfig, WebhookProvider, type WebhookProvider as WebhookProviderType } from "@/contracts"; |
| 2 | import { |
| 3 | toCodecIssueDetails, |
| 4 | getWebhookProviderCatalogEntry, |
| 5 | validateWebhookConfigForUpsert as validateConfig, |
| 6 | } from "@/lib"; |
| 7 | import type { AppContext } from "@/worker/hono"; |
| 8 | import { HttpError } from "@/worker/http"; |
| 9 | import { normalizeWebhookInstanceUrl } from "@/worker/validation"; |
| 10 | const decodeWebhookProvider = ( |
| 11 | provider: string, |
| 12 | options?: { |
| 13 | includeCodecDetails?: boolean; |
| 14 | }, |
| 15 | ): WebhookProviderType => { |
| 16 | try { |
| 17 | return WebhookProvider.assertDecode(provider); |
| 18 | } catch (error) { |
| 19 | throw new HttpError( |
| 20 | 404, |
| 21 | "webhook_not_found", |
| 22 | "Webhook was not found.", |
| 23 | options?.includeCodecDetails ? toCodecIssueDetails(error) : undefined, |
| 24 | ); |
| 25 | } |
| 26 | }; |
| 27 | export const requireWebhookProvider = ( |
| 28 | provider: string | undefined, |
| 29 | options?: { |
| 30 | includeCodecDetails?: boolean; |
| 31 | }, |
| 32 | ): WebhookProviderType => { |
| 33 | if (!provider) { |
| 34 | throw new HttpError(404, "webhook_not_found", "Webhook was not found."); |
| 35 | } |
| 36 | return decodeWebhookProvider(provider, options); |
| 37 | }; |
| 38 | export const requireWebhookProviderParam = (c: Pick<AppContext, "req">): WebhookProviderType => |
| 39 | requireWebhookProvider(c.req.param("provider"), { includeCodecDetails: true }); |
| 40 | export const normalizeWebhookConfigIfPresent = ( |
| 41 | config: WebhookProviderConfig | null | undefined, |
| 42 | ): WebhookProviderConfig | null | undefined => |
| 43 | config !== undefined && config !== null ? { instanceUrl: normalizeWebhookInstanceUrl(config.instanceUrl) } : config; |
| 44 | export const resolveWebhookConfigForUpsert = (input: { |
| 45 | provider: WebhookProviderType; |
| 46 | projectRepoUrl: string; |
| 47 | incomingConfig: WebhookProviderConfig | null | undefined; |
| 48 | existingConfig: WebhookProviderConfig | null; |
| 49 | creating: boolean; |
| 50 | }): WebhookProviderConfig | null => { |
| 51 | const normalizedIncoming = normalizeWebhookConfigIfPresent(input.incomingConfig); |
| 52 | const result = validateConfig({ |
| 53 | ...input, |
| 54 | incomingConfig: normalizedIncoming, |
| 55 | }); |
| 56 | if (!result.ok) { |
| 57 | throw new HttpError(result.status as 400 | 500, result.code, result.message); |
| 58 | } |
| 59 | return result.config; |
| 60 | }; |
| 61 | export const resolveExpectedWebhookInstanceUrl = ( |
| 62 | provider: WebhookProviderType, |
| 63 | config: WebhookProviderConfig | null, |
| 64 | ): string | null => { |
| 65 | const providerEntry = getWebhookProviderCatalogEntry(provider); |
| 66 | if (config !== null) { |
| 67 | return config.instanceUrl; |
| 68 | } |
| 69 | if (providerEntry.configMode === "required") { |
| 70 | throw new HttpError(500, "invalid_webhook_config", "Webhook config is invalid."); |
| 71 | } |
| 72 | return providerEntry.defaultInstanceUrl; |
| 73 | }; |