(ctx: SubscriptionContext)
| 151 | }, |
| 152 | |
| 153 | async createSubscription(ctx: SubscriptionContext): Promise<SubscriptionResult | undefined> { |
| 154 | const { webhook, requestId } = ctx |
| 155 | try { |
| 156 | const providerConfig = getProviderConfig(webhook) |
| 157 | const apiKey = providerConfig.apiKey as string | undefined |
| 158 | const triggerId = providerConfig.triggerId as string | undefined |
| 159 | |
| 160 | if (!apiKey) { |
| 161 | logger.warn(`[${requestId}] Missing apiKey for Resend webhook creation.`, { |
| 162 | webhookId: webhook.id, |
| 163 | }) |
| 164 | throw new Error( |
| 165 | 'Resend API Key is required. Please provide your Resend API Key in the trigger configuration.' |
| 166 | ) |
| 167 | } |
| 168 | |
| 169 | const events = |
| 170 | triggerId === 'resend_webhook' |
| 171 | ? RESEND_ALL_WEBHOOK_EVENT_TYPES |
| 172 | : triggerId && RESEND_TRIGGER_TO_EVENT_TYPE[triggerId] |
| 173 | ? [RESEND_TRIGGER_TO_EVENT_TYPE[triggerId]] |
| 174 | : null |
| 175 | |
| 176 | if (!events?.length) { |
| 177 | throw new Error(`Unknown or unsupported Resend trigger type: ${triggerId ?? '(missing)'}`) |
| 178 | } |
| 179 | |
| 180 | const notificationUrl = getNotificationUrl(webhook) |
| 181 | |
| 182 | logger.info(`[${requestId}] Creating Resend webhook`, { |
| 183 | triggerId, |
| 184 | events, |
| 185 | webhookId: webhook.id, |
| 186 | }) |
| 187 | |
| 188 | const resendResponse = await fetch('https://api.resend.com/webhooks', { |
| 189 | method: 'POST', |
| 190 | headers: { |
| 191 | Authorization: `Bearer ${apiKey}`, |
| 192 | 'Content-Type': 'application/json', |
| 193 | }, |
| 194 | body: JSON.stringify({ |
| 195 | endpoint: notificationUrl, |
| 196 | events, |
| 197 | }), |
| 198 | }) |
| 199 | |
| 200 | const responseBody = (await resendResponse.json()) as Record<string, unknown> |
| 201 | |
| 202 | if (!resendResponse.ok) { |
| 203 | const errorMessage = |
| 204 | (responseBody.message as string) || |
| 205 | (responseBody.name as string) || |
| 206 | 'Unknown Resend API error' |
| 207 | logger.error( |
| 208 | `[${requestId}] Failed to create webhook in Resend for webhook ${webhook.id}. Status: ${resendResponse.status}`, |
| 209 | { message: errorMessage, response: responseBody } |
| 210 | ) |
nothing calls this directly
no test coverage detected