( integration: Airtable )
| 265 | }; |
| 266 | |
| 267 | export function createWebhookSource( |
| 268 | integration: Airtable |
| 269 | ): WebhookSource< |
| 270 | Airtable, |
| 271 | { baseId: string; tableId?: string }, |
| 272 | { dataTypes: WebhookDataType[]; fromSources?: WebhookFromSource[] } |
| 273 | > { |
| 274 | return new WebhookSource({ |
| 275 | id: "airtable.webhook", |
| 276 | schemas: { |
| 277 | params: z.object({ baseId: z.string(), tableId: z.string().optional() }), |
| 278 | config: z.object({ |
| 279 | dataTypes: z.array(WebhookDataTypeSchema), |
| 280 | fromSources: z.array(WebhookFromSourceSchema).optional(), |
| 281 | }), |
| 282 | }, |
| 283 | version: "0.1.0", |
| 284 | integration, |
| 285 | filter: (params, options) => ({ |
| 286 | actionMetadata: { |
| 287 | source: options?.fromSources ?? ["client", "anonymousUser", "formSubmission"], |
| 288 | }, |
| 289 | }), |
| 290 | key: (params) => |
| 291 | `airtable.webhook.${params.baseId}${params.tableId ? `.${params.tableId}` : ""}`, |
| 292 | crud: { |
| 293 | create: async ({ io, ctx }) => { |
| 294 | const webhook = await io.integration.webhooks().create("create-webhook", { |
| 295 | url: ctx.url, |
| 296 | baseId: ctx.params?.baseId, |
| 297 | options: getSpecification(ctx.config.desired, ctx.params), |
| 298 | }); |
| 299 | |
| 300 | await io.store.job.set("set-id", "webhook-id", webhook.id); |
| 301 | await io.store.job.set("set-secret", "webhook-secret-base64", webhook.macSecretBase64); |
| 302 | }, |
| 303 | read: async ({ io, ctx }) => { |
| 304 | const listResponse = await io.integration.webhooks().list("list-webhooks", { |
| 305 | baseId: ctx.params?.baseId, |
| 306 | }); |
| 307 | |
| 308 | const existingWebhook = listResponse.webhooks.find((w) => w.notificationUrl === ctx.url); |
| 309 | |
| 310 | if (!existingWebhook) { |
| 311 | return await io.store.job.delete("delete-stale-webhook-id", "webhook-id"); |
| 312 | } |
| 313 | |
| 314 | await io.store.job.set("set-webhook-id", "webhook-id", existingWebhook.id); |
| 315 | }, |
| 316 | delete: async ({ io, ctx }) => { |
| 317 | const webhookId = await io.store.job.get<string>("get-webhook-id", "webhook-id"); |
| 318 | |
| 319 | if (!webhookId) { |
| 320 | throw new Error("Missing webhook ID for delete operation."); |
| 321 | } |
| 322 | |
| 323 | await io.integration.webhooks().delete("delete-webhook", { |
| 324 | baseId: ctx.params?.baseId, |
no test coverage detected
searching dependent graphs…