@automate.ax/integration-contracts 0.105.0 → 0.109.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/airtable/api.js +6 -4
  2. package/dist/airtable/index.d.ts +997 -19
  3. package/dist/airtable/index.js +30 -2
  4. package/dist/airtable/schemas.d.ts +2034 -44
  5. package/dist/airtable/schemas.js +231 -47
  6. package/dist/brevo/api.d.ts +41 -0
  7. package/dist/brevo/api.js +74 -4
  8. package/dist/close/events.d.ts +13 -13
  9. package/dist/close/schemas.d.ts +9 -9
  10. package/dist/cloudflare/api.d.ts +93 -0
  11. package/dist/cloudflare/api.js +250 -0
  12. package/dist/cloudflare/events.d.ts +1340 -0
  13. package/dist/cloudflare/events.js +178 -0
  14. package/dist/cloudflare/index.d.ts +3 -0
  15. package/dist/cloudflare/index.js +3 -0
  16. package/dist/cloudflare/schemas.d.ts +608 -0
  17. package/dist/cloudflare/schemas.js +340 -0
  18. package/dist/linear/api.d.ts +1 -1
  19. package/dist/linear/api.js +9 -6
  20. package/dist/linear/index.d.ts +3479 -782
  21. package/dist/linear/index.js +249 -13
  22. package/dist/linear/schemas.d.ts +2724 -31
  23. package/dist/linear/schemas.js +515 -2
  24. package/dist/notion/schemas.d.ts +112 -112
  25. package/dist/outlook/schemas.d.ts +2 -2
  26. package/dist/resend/action-schemas.d.ts +9 -9
  27. package/dist/resend/index.d.ts +36 -36
  28. package/dist/resend/schemas.d.ts +40 -40
  29. package/dist/teams/schemas.d.ts +5 -5
  30. package/dist/triggers.d.ts +2 -1
  31. package/dist/whatsapp/index.d.ts +2 -2
  32. package/dist/whatsapp/schemas.d.ts +5 -5
  33. package/package.json +8 -2
  34. package/src/airtable/api.ts +7 -4
  35. package/src/airtable/index.ts +36 -1
  36. package/src/airtable/schemas.ts +294 -63
  37. package/src/brevo/api.ts +103 -8
  38. package/src/cloudflare/api.ts +340 -0
  39. package/src/cloudflare/events.ts +212 -0
  40. package/src/cloudflare/index.ts +3 -0
  41. package/src/cloudflare/schemas.ts +359 -0
  42. package/src/linear/api.ts +10 -6
  43. package/src/linear/index.ts +249 -26
  44. package/src/linear/schemas.ts +688 -2
  45. package/src/triggers.ts +2 -0
@@ -324,8 +324,8 @@ export declare const whatsappTriggerContracts: {
324
324
  type: z.ZodEnum<{
325
325
  unknown: "unknown";
326
326
  text: "text";
327
- document: "document";
328
327
  audio: "audio";
328
+ document: "document";
329
329
  image: "image";
330
330
  video: "video";
331
331
  location: "location";
@@ -419,8 +419,8 @@ export declare const whatsappTriggerContracts: {
419
419
  type: z.ZodEnum<{
420
420
  unknown: "unknown";
421
421
  text: "text";
422
- document: "document";
423
422
  audio: "audio";
423
+ document: "document";
424
424
  image: "image";
425
425
  video: "video";
426
426
  location: "location";
@@ -90,8 +90,8 @@ export declare const WHATSAPP_WEBHOOK_SCHEMA: z.ZodObject<{
90
90
  type: z.ZodEnum<{
91
91
  unknown: "unknown";
92
92
  text: "text";
93
- document: "document";
94
93
  audio: "audio";
94
+ document: "document";
95
95
  image: "image";
96
96
  video: "video";
97
97
  location: "location";
@@ -183,8 +183,8 @@ export declare const WHATSAPP_MESSAGE_RECEIVED_EVENT_SCHEMA: z.ZodObject<{
183
183
  type: z.ZodEnum<{
184
184
  unknown: "unknown";
185
185
  text: "text";
186
- document: "document";
187
186
  audio: "audio";
187
+ document: "document";
188
188
  image: "image";
189
189
  video: "video";
190
190
  location: "location";
@@ -278,8 +278,8 @@ export declare const WHATSAPP_MESSAGE_RECEIVED_EVENT_SCHEMA: z.ZodObject<{
278
278
  type: z.ZodEnum<{
279
279
  unknown: "unknown";
280
280
  text: "text";
281
- document: "document";
282
281
  audio: "audio";
282
+ document: "document";
283
283
  image: "image";
284
284
  video: "video";
285
285
  location: "location";
@@ -768,7 +768,7 @@ export declare function normalizeWhatsAppWebhook(input: unknown): {
768
768
  message: {
769
769
  id: string;
770
770
  timestamp: string;
771
- type: "unknown" | "text" | "document" | "audio" | "image" | "video" | "location" | "system" | "order" | "reaction" | "button" | "contacts" | "interactive" | "sticker" | "unsupported";
771
+ type: "unknown" | "text" | "audio" | "document" | "image" | "video" | "location" | "system" | "order" | "reaction" | "button" | "contacts" | "interactive" | "sticker" | "unsupported";
772
772
  content?: z.core.util.JSONType | undefined;
773
773
  context?: {
774
774
  messageId: string;
@@ -781,7 +781,7 @@ export declare function normalizeWhatsAppWebhook(input: unknown): {
781
781
  from: string;
782
782
  id: string;
783
783
  timestamp: string;
784
- type: "unknown" | "text" | "document" | "audio" | "image" | "video" | "location" | "system" | "order" | "reaction" | "button" | "contacts" | "interactive" | "sticker" | "unsupported";
784
+ type: "unknown" | "text" | "audio" | "document" | "image" | "video" | "location" | "system" | "order" | "reaction" | "button" | "contacts" | "interactive" | "sticker" | "unsupported";
785
785
  audio?: {
786
786
  id: string;
787
787
  mime_type?: string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automate.ax/integration-contracts",
3
- "version": "0.105.0",
3
+ "version": "0.109.0",
4
4
  "description": "Shared integration payload contracts and provider primitives for Automate.ax.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,6 +22,7 @@
22
22
  "./automate": "./src/automate/index.ts",
23
23
  "./asana": "./src/asana/index.ts",
24
24
  "./brevo": "./src/brevo/index.ts",
25
+ "./cloudflare": "./src/cloudflare/index.ts",
25
26
  "./convex": "./src/convex/index.ts",
26
27
  "./close": "./src/close/index.ts",
27
28
  "./gmail": "./src/gmail/index.ts",
@@ -49,7 +50,7 @@
49
50
  }
50
51
  },
51
52
  "dependencies": {
52
- "@automate.ax/codec": "0.105.0",
53
+ "@automate.ax/codec": "0.109.0",
53
54
  "@cfworker/json-schema": "^4.1.1",
54
55
  "@googleapis/calendar": "^15.0.0",
55
56
  "@googleapis/forms": "^6.0.1",
@@ -120,6 +121,11 @@
120
121
  "types": "./dist/brevo/index.d.ts",
121
122
  "default": "./dist/brevo/index.js"
122
123
  },
124
+ "./cloudflare": {
125
+ "bun": "./src/cloudflare/index.ts",
126
+ "types": "./dist/cloudflare/index.d.ts",
127
+ "default": "./dist/cloudflare/index.js"
128
+ },
123
129
  "./convex": {
124
130
  "bun": "./src/convex/index.ts",
125
131
  "types": "./dist/convex/index.d.ts",
@@ -2,9 +2,10 @@ import * as z from "zod"
2
2
 
3
3
  export const AIRTABLE_API_ORIGIN = "https://api.airtable.com/v0/"
4
4
 
5
- const AIRTABLE_SECRET_SCHEMA = z.object({
6
- accessToken: z.string().min(1),
7
- })
5
+ const AIRTABLE_SECRET_SCHEMA = z.union([
6
+ z.object({ accessToken: z.string().min(1) }),
7
+ z.object({ apiKey: z.string().min(1) }),
8
+ ])
8
9
  const AIRTABLE_ERROR_SCHEMA = z.object({
9
10
  error: z.union([
10
11
  z.string(),
@@ -34,7 +35,9 @@ export interface AirtableApi {
34
35
  * @param secret - Refreshed Airtable integration secret.
35
36
  */
36
37
  export function getAirtableApi(secret: Record<string, unknown>): AirtableApi {
37
- const { accessToken } = AIRTABLE_SECRET_SCHEMA.parse(secret)
38
+ const credential = AIRTABLE_SECRET_SCHEMA.parse(secret)
39
+ const accessToken =
40
+ "accessToken" in credential ? credential.accessToken : credential.apiKey
38
41
 
39
42
  return {
40
43
  request: async (path, init) => {
@@ -1,10 +1,17 @@
1
1
  import * as z from "zod"
2
2
  import {
3
+ AIRTABLE_BASE_EVENT_SCHEMA,
4
+ AIRTABLE_FIELD_CREATED_EVENT_SCHEMA,
5
+ AIRTABLE_FIELD_DELETED_EVENT_SCHEMA,
6
+ AIRTABLE_FIELD_UPDATED_EVENT_SCHEMA,
3
7
  AIRTABLE_RECORD_CREATED_EVENT_SCHEMA,
4
8
  AIRTABLE_RECORD_DELETED_EVENT_SCHEMA,
5
9
  AIRTABLE_RECORD_MOVED_INTO_VIEW_EVENT_SCHEMA,
6
10
  AIRTABLE_RECORD_MOVED_OUT_OF_VIEW_EVENT_SCHEMA,
7
11
  AIRTABLE_RECORD_UPDATED_EVENT_SCHEMA,
12
+ AIRTABLE_TABLE_CREATED_EVENT_SCHEMA,
13
+ AIRTABLE_TABLE_DELETED_EVENT_SCHEMA,
14
+ AIRTABLE_TABLE_UPDATED_EVENT_SCHEMA,
8
15
  } from "./schemas"
9
16
 
10
17
  export * from "./api"
@@ -14,10 +21,26 @@ export * from "./schemas"
14
21
  export const AIRTABLE_TRIGGER_CONFIG_SCHEMA = z.object({
15
22
  baseId: z.string(),
16
23
  scopeId: z.string(),
17
- scopeType: z.enum(["table", "view"]),
24
+ scopeType: z.enum(["base", "table", "view"]),
18
25
  })
19
26
 
20
27
  export const airtableTriggerContracts = {
28
+ "airtable.base.event": {
29
+ configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
30
+ eventSchema: AIRTABLE_BASE_EVENT_SCHEMA,
31
+ },
32
+ "airtable.field.created": {
33
+ configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
34
+ eventSchema: AIRTABLE_FIELD_CREATED_EVENT_SCHEMA,
35
+ },
36
+ "airtable.field.deleted": {
37
+ configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
38
+ eventSchema: AIRTABLE_FIELD_DELETED_EVENT_SCHEMA,
39
+ },
40
+ "airtable.field.updated": {
41
+ configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
42
+ eventSchema: AIRTABLE_FIELD_UPDATED_EVENT_SCHEMA,
43
+ },
21
44
  "airtable.record.created": {
22
45
  configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
23
46
  eventSchema: AIRTABLE_RECORD_CREATED_EVENT_SCHEMA,
@@ -38,4 +61,16 @@ export const airtableTriggerContracts = {
38
61
  configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
39
62
  eventSchema: AIRTABLE_RECORD_UPDATED_EVENT_SCHEMA,
40
63
  },
64
+ "airtable.table.created": {
65
+ configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
66
+ eventSchema: AIRTABLE_TABLE_CREATED_EVENT_SCHEMA,
67
+ },
68
+ "airtable.table.deleted": {
69
+ configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
70
+ eventSchema: AIRTABLE_TABLE_DELETED_EVENT_SCHEMA,
71
+ },
72
+ "airtable.table.updated": {
73
+ configSchema: AIRTABLE_TRIGGER_CONFIG_SCHEMA,
74
+ eventSchema: AIRTABLE_TABLE_UPDATED_EVENT_SCHEMA,
75
+ },
41
76
  } as const
@@ -34,7 +34,28 @@ const AIRTABLE_VIEW_CHANGE_SCHEMA = z.looseObject({
34
34
  .optional(),
35
35
  destroyedRecordIds: z.string().array().optional(),
36
36
  })
37
+ const AIRTABLE_FIELD_DEFINITION_SCHEMA = z.object({
38
+ name: z.string(),
39
+ type: z.string(),
40
+ })
41
+ const AIRTABLE_FIELD_CHANGE_SCHEMA = z.looseObject({
42
+ current: AIRTABLE_FIELD_DEFINITION_SCHEMA.partial(),
43
+ previous: AIRTABLE_FIELD_DEFINITION_SCHEMA.partial().optional(),
44
+ })
45
+ const AIRTABLE_TABLE_METADATA_SCHEMA = z.object({
46
+ description: z.string().nullable(),
47
+ name: z.string(),
48
+ })
37
49
  const AIRTABLE_TABLE_CHANGE_SCHEMA = z.looseObject({
50
+ changedFieldsById: z
51
+ .record(z.string(), AIRTABLE_FIELD_CHANGE_SCHEMA)
52
+ .optional(),
53
+ changedMetadata: z
54
+ .looseObject({
55
+ current: AIRTABLE_TABLE_METADATA_SCHEMA.partial(),
56
+ previous: AIRTABLE_TABLE_METADATA_SCHEMA.partial(),
57
+ })
58
+ .optional(),
38
59
  changedRecordsById: z
39
60
  .record(z.string(), AIRTABLE_CHANGED_RECORD_SCHEMA)
40
61
  .optional(),
@@ -44,8 +65,17 @@ const AIRTABLE_TABLE_CHANGE_SCHEMA = z.looseObject({
44
65
  createdRecordsById: z
45
66
  .record(z.string(), AIRTABLE_CREATED_RECORD_SCHEMA)
46
67
  .optional(),
68
+ createdFieldsById: z
69
+ .record(z.string(), AIRTABLE_FIELD_DEFINITION_SCHEMA)
70
+ .optional(),
71
+ destroyedFieldIds: z.string().array().optional(),
47
72
  destroyedRecordIds: z.string().array().optional(),
48
73
  })
74
+ const AIRTABLE_CREATED_TABLE_SCHEMA = z.looseObject({
75
+ fieldsById: z.record(z.string(), AIRTABLE_FIELD_DEFINITION_SCHEMA).optional(),
76
+ metadata: AIRTABLE_TABLE_METADATA_SCHEMA.optional(),
77
+ recordsById: z.record(z.string(), AIRTABLE_CREATED_RECORD_SCHEMA).optional(),
78
+ })
49
79
 
50
80
  /** One provider-native Airtable webhook transaction payload. */
51
81
  export const AIRTABLE_WEBHOOK_PAYLOAD_SCHEMA = z
@@ -55,13 +85,84 @@ export const AIRTABLE_WEBHOOK_PAYLOAD_SCHEMA = z
55
85
  changedTablesById: z
56
86
  .record(z.string(), AIRTABLE_TABLE_CHANGE_SCHEMA)
57
87
  .optional(),
88
+ createdTablesById: z
89
+ .record(z.string(), AIRTABLE_CREATED_TABLE_SCHEMA)
90
+ .optional(),
58
91
  code: z.string().optional(),
59
92
  error: z.literal(true).optional(),
93
+ destroyedTableIds: z.string().array().optional(),
60
94
  payloadFormat: z.string(),
61
95
  timestamp: z.iso.datetime({ offset: true }),
62
96
  })
63
97
  .catchall(z.json())
64
98
 
99
+ export const AIRTABLE_BASE_EVENT_SCHEMA = z.object({
100
+ /** Airtable base that emitted the transaction. */
101
+ baseId: z.string(),
102
+
103
+ /** Complete provider webhook transaction. */
104
+ event: AIRTABLE_WEBHOOK_PAYLOAD_SCHEMA,
105
+
106
+ /** Time at which Airtable committed the transaction. */
107
+ occurredAt: z.iso.datetime({ offset: true }),
108
+
109
+ /** Airtable system that caused the transaction. */
110
+ source: z.string(),
111
+
112
+ /** Provider-specific actor or source details. */
113
+ sourceMetadata: z.record(z.string(), z.json()).optional(),
114
+
115
+ /** Base-scoped transaction number assigned by Airtable. */
116
+ transactionNumber: z.number().int().nonnegative(),
117
+ })
118
+
119
+ const AIRTABLE_TABLE_EVENT_SCHEMA = AIRTABLE_BASE_EVENT_SCHEMA.extend({
120
+ /** Stable Airtable table ID. */
121
+ tableId: z.string(),
122
+ })
123
+
124
+ export const AIRTABLE_TABLE_CREATED_EVENT_SCHEMA =
125
+ AIRTABLE_TABLE_EVENT_SCHEMA.extend({
126
+ /** Field definitions keyed by stable field ID. */
127
+ fields: z.record(z.string(), AIRTABLE_FIELD_DEFINITION_SCHEMA),
128
+
129
+ /** Created table metadata when included by Airtable. */
130
+ metadata: AIRTABLE_TABLE_METADATA_SCHEMA.optional(),
131
+ })
132
+
133
+ export const AIRTABLE_TABLE_UPDATED_EVENT_SCHEMA =
134
+ AIRTABLE_TABLE_EVENT_SCHEMA.extend({
135
+ /** Current table metadata fields changed by the transaction. */
136
+ current: AIRTABLE_TABLE_METADATA_SCHEMA.partial(),
137
+
138
+ /** Previous table metadata fields changed by the transaction. */
139
+ previous: AIRTABLE_TABLE_METADATA_SCHEMA.partial(),
140
+ })
141
+
142
+ export const AIRTABLE_TABLE_DELETED_EVENT_SCHEMA = AIRTABLE_TABLE_EVENT_SCHEMA
143
+
144
+ const AIRTABLE_FIELD_EVENT_SCHEMA = AIRTABLE_TABLE_EVENT_SCHEMA.extend({
145
+ /** Stable Airtable field ID. */
146
+ fieldId: z.string(),
147
+ })
148
+
149
+ export const AIRTABLE_FIELD_CREATED_EVENT_SCHEMA =
150
+ AIRTABLE_FIELD_EVENT_SCHEMA.extend({
151
+ /** Created field definition. */
152
+ field: AIRTABLE_FIELD_DEFINITION_SCHEMA,
153
+ })
154
+
155
+ export const AIRTABLE_FIELD_UPDATED_EVENT_SCHEMA =
156
+ AIRTABLE_FIELD_EVENT_SCHEMA.extend({
157
+ /** Current field definition values changed by the transaction. */
158
+ current: AIRTABLE_FIELD_DEFINITION_SCHEMA.partial(),
159
+
160
+ /** Previous field definition values changed by the transaction. */
161
+ previous: AIRTABLE_FIELD_DEFINITION_SCHEMA.partial().optional(),
162
+ })
163
+
164
+ export const AIRTABLE_FIELD_DELETED_EVENT_SCHEMA = AIRTABLE_FIELD_EVENT_SCHEMA
165
+
65
166
  const AIRTABLE_RECORD_EVENT_SCHEMA = z.object({
66
167
  /** Airtable base containing the affected record. */
67
168
  baseId: z.string(),
@@ -123,7 +224,14 @@ export const AIRTABLE_RECORD_MOVED_OUT_OF_VIEW_EVENT_SCHEMA =
123
224
  viewId: z.string(),
124
225
  })
125
226
 
126
- export const AIRTABLE_RECORD_EVENT_TYPES = [
227
+ export const AIRTABLE_EVENT_TYPES = [
228
+ "airtable.base.event",
229
+ "airtable.table.created",
230
+ "airtable.table.updated",
231
+ "airtable.table.deleted",
232
+ "airtable.field.created",
233
+ "airtable.field.updated",
234
+ "airtable.field.deleted",
127
235
  "airtable.record.created",
128
236
  "airtable.record.updated",
129
237
  "airtable.record.deleted",
@@ -131,77 +239,206 @@ export const AIRTABLE_RECORD_EVENT_TYPES = [
131
239
  "airtable.record.movedOutOfView",
132
240
  ] as const
133
241
 
134
- export type AirtableRecordEventType =
135
- (typeof AIRTABLE_RECORD_EVENT_TYPES)[number]
242
+ export type AirtableEventType = (typeof AIRTABLE_EVENT_TYPES)[number]
136
243
 
137
244
  /**
138
- * Expands one Airtable webhook transaction into semantic record events.
245
+ * Expands one Airtable webhook transaction into broad and semantic events.
139
246
  *
140
247
  * @param value - Untrusted provider payload returned by Airtable.
141
248
  * @param baseId - Stable ID of the base that owns the webhook.
142
249
  */
143
- export function normalizeAirtableRecordEvents(value: unknown, baseId: string) {
250
+ export function normalizeAirtableEvents(value: unknown, baseId: string) {
144
251
  const event = AIRTABLE_WEBHOOK_PAYLOAD_SCHEMA.parse(value)
145
252
 
146
- return Object.entries(event.changedTablesById ?? {}).flatMap(
147
- ([tableId, table]) => [
148
- ...Object.entries(table.createdRecordsById ?? {}).map(
149
- ([recordId, record]) => ({
150
- eventType: "airtable.record.created" as const,
151
- payload: AIRTABLE_RECORD_CREATED_EVENT_SCHEMA.parse({
152
- ...recordEventBase(event, baseId, tableId, recordId),
153
- createdAt: record.createdTime,
154
- fields: record.cellValuesByFieldId,
253
+ return [
254
+ {
255
+ eventType: "airtable.base.event" as const,
256
+ payload: AIRTABLE_BASE_EVENT_SCHEMA.parse(baseEventBase(event, baseId)),
257
+ },
258
+ ...Object.entries(event.createdTablesById ?? {}).flatMap(
259
+ ([tableId, table]) => [
260
+ {
261
+ eventType: "airtable.table.created" as const,
262
+ payload: AIRTABLE_TABLE_CREATED_EVENT_SCHEMA.parse({
263
+ ...tableEventBase(event, baseId, tableId),
264
+ fields: table.fieldsById ?? {},
265
+ metadata: table.metadata,
155
266
  }),
156
- }),
157
- ),
158
- ...Object.entries(table.changedRecordsById ?? {}).map(
159
- ([recordId, record]) => ({
160
- eventType: "airtable.record.updated" as const,
161
- payload: AIRTABLE_RECORD_UPDATED_EVENT_SCHEMA.parse({
162
- ...recordEventBase(event, baseId, tableId, recordId),
163
- changedFieldIds: Object.keys(record.current.cellValuesByFieldId),
164
- fields: {
165
- ...record.unchanged?.cellValuesByFieldId,
166
- ...record.current.cellValuesByFieldId,
167
- },
168
- previousFields: {
169
- ...record.unchanged?.cellValuesByFieldId,
170
- ...record.previous?.cellValuesByFieldId,
171
- },
267
+ },
268
+ ...Object.entries(table.fieldsById ?? {}).map(([fieldId, field]) => ({
269
+ eventType: "airtable.field.created" as const,
270
+ payload: AIRTABLE_FIELD_CREATED_EVENT_SCHEMA.parse({
271
+ ...fieldEventBase(event, baseId, tableId, fieldId),
272
+ field,
172
273
  }),
173
- }),
274
+ })),
275
+ ...Object.entries(table.recordsById ?? {}).map(
276
+ ([recordId, record]) => ({
277
+ eventType: "airtable.record.created" as const,
278
+ payload: AIRTABLE_RECORD_CREATED_EVENT_SCHEMA.parse({
279
+ ...recordEventBase(event, baseId, tableId, recordId),
280
+ createdAt: record.createdTime,
281
+ fields: record.cellValuesByFieldId,
282
+ }),
283
+ }),
284
+ ),
285
+ ],
286
+ ),
287
+ ...(event.destroyedTableIds ?? []).map((tableId) => ({
288
+ eventType: "airtable.table.deleted" as const,
289
+ payload: AIRTABLE_TABLE_DELETED_EVENT_SCHEMA.parse(
290
+ tableEventBase(event, baseId, tableId),
174
291
  ),
175
- ...(table.destroyedRecordIds ?? []).map((recordId) => ({
176
- eventType: "airtable.record.deleted" as const,
177
- payload: AIRTABLE_RECORD_DELETED_EVENT_SCHEMA.parse(
178
- recordEventBase(event, baseId, tableId, recordId),
292
+ })),
293
+ ...Object.entries(event.changedTablesById ?? {}).flatMap(
294
+ ([tableId, table]) => [
295
+ ...(table.changedMetadata
296
+ ? [
297
+ {
298
+ eventType: "airtable.table.updated" as const,
299
+ payload: AIRTABLE_TABLE_UPDATED_EVENT_SCHEMA.parse({
300
+ ...tableEventBase(event, baseId, tableId),
301
+ current: table.changedMetadata.current,
302
+ previous: table.changedMetadata.previous,
303
+ }),
304
+ },
305
+ ]
306
+ : []),
307
+ ...Object.entries(table.createdFieldsById ?? {}).map(
308
+ ([fieldId, field]) => ({
309
+ eventType: "airtable.field.created" as const,
310
+ payload: AIRTABLE_FIELD_CREATED_EVENT_SCHEMA.parse({
311
+ ...fieldEventBase(event, baseId, tableId, fieldId),
312
+ field,
313
+ }),
314
+ }),
179
315
  ),
180
- })),
181
- ...Object.entries(table.changedViewsById ?? {}).flatMap(
182
- ([viewId, view]) => [
183
- ...Object.entries(view.createdRecordsById ?? {}).map(
184
- ([recordId, record]) => ({
185
- eventType: "airtable.record.movedIntoView" as const,
186
- payload: AIRTABLE_RECORD_MOVED_INTO_VIEW_EVENT_SCHEMA.parse({
187
- ...recordEventBase(event, baseId, tableId, recordId),
188
- createdAt: record.createdTime,
189
- fields: record.cellValuesByFieldId,
190
- viewId,
191
- }),
316
+ ...Object.entries(table.changedFieldsById ?? {}).map(
317
+ ([fieldId, field]) => ({
318
+ eventType: "airtable.field.updated" as const,
319
+ payload: AIRTABLE_FIELD_UPDATED_EVENT_SCHEMA.parse({
320
+ ...fieldEventBase(event, baseId, tableId, fieldId),
321
+ current: field.current,
322
+ previous: field.previous,
192
323
  }),
324
+ }),
325
+ ),
326
+ ...(table.destroyedFieldIds ?? []).map((fieldId) => ({
327
+ eventType: "airtable.field.deleted" as const,
328
+ payload: AIRTABLE_FIELD_DELETED_EVENT_SCHEMA.parse(
329
+ fieldEventBase(event, baseId, tableId, fieldId),
193
330
  ),
194
- ...(view.destroyedRecordIds ?? []).map((recordId) => ({
195
- eventType: "airtable.record.movedOutOfView" as const,
196
- payload: AIRTABLE_RECORD_MOVED_OUT_OF_VIEW_EVENT_SCHEMA.parse({
331
+ })),
332
+ ...Object.entries(table.createdRecordsById ?? {}).map(
333
+ ([recordId, record]) => ({
334
+ eventType: "airtable.record.created" as const,
335
+ payload: AIRTABLE_RECORD_CREATED_EVENT_SCHEMA.parse({
197
336
  ...recordEventBase(event, baseId, tableId, recordId),
198
- viewId,
337
+ createdAt: record.createdTime,
338
+ fields: record.cellValuesByFieldId,
199
339
  }),
200
- })),
201
- ],
202
- ),
203
- ],
204
- )
340
+ }),
341
+ ),
342
+ ...Object.entries(table.changedRecordsById ?? {}).map(
343
+ ([recordId, record]) => ({
344
+ eventType: "airtable.record.updated" as const,
345
+ payload: AIRTABLE_RECORD_UPDATED_EVENT_SCHEMA.parse({
346
+ ...recordEventBase(event, baseId, tableId, recordId),
347
+ changedFieldIds: Object.keys(record.current.cellValuesByFieldId),
348
+ fields: {
349
+ ...record.unchanged?.cellValuesByFieldId,
350
+ ...record.current.cellValuesByFieldId,
351
+ },
352
+ previousFields: {
353
+ ...record.unchanged?.cellValuesByFieldId,
354
+ ...record.previous?.cellValuesByFieldId,
355
+ },
356
+ }),
357
+ }),
358
+ ),
359
+ ...(table.destroyedRecordIds ?? []).map((recordId) => ({
360
+ eventType: "airtable.record.deleted" as const,
361
+ payload: AIRTABLE_RECORD_DELETED_EVENT_SCHEMA.parse(
362
+ recordEventBase(event, baseId, tableId, recordId),
363
+ ),
364
+ })),
365
+ ...Object.entries(table.changedViewsById ?? {}).flatMap(
366
+ ([viewId, view]) => [
367
+ ...Object.entries(view.createdRecordsById ?? {}).map(
368
+ ([recordId, record]) => ({
369
+ eventType: "airtable.record.movedIntoView" as const,
370
+ payload: AIRTABLE_RECORD_MOVED_INTO_VIEW_EVENT_SCHEMA.parse({
371
+ ...recordEventBase(event, baseId, tableId, recordId),
372
+ createdAt: record.createdTime,
373
+ fields: record.cellValuesByFieldId,
374
+ viewId,
375
+ }),
376
+ }),
377
+ ),
378
+ ...(view.destroyedRecordIds ?? []).map((recordId) => ({
379
+ eventType: "airtable.record.movedOutOfView" as const,
380
+ payload: AIRTABLE_RECORD_MOVED_OUT_OF_VIEW_EVENT_SCHEMA.parse({
381
+ ...recordEventBase(event, baseId, tableId, recordId),
382
+ viewId,
383
+ }),
384
+ })),
385
+ ],
386
+ ),
387
+ ],
388
+ ),
389
+ ]
390
+ }
391
+
392
+ /**
393
+ * Builds fields shared by every event in one Airtable transaction.
394
+ *
395
+ * @param event - Parsed provider transaction.
396
+ * @param baseId - Stable Airtable base ID.
397
+ */
398
+ function baseEventBase(
399
+ event: z.output<typeof AIRTABLE_WEBHOOK_PAYLOAD_SCHEMA>,
400
+ baseId: string,
401
+ ) {
402
+ return {
403
+ baseId,
404
+ event,
405
+ occurredAt: event.timestamp,
406
+ source: event.actionMetadata.source,
407
+ sourceMetadata: event.actionMetadata.sourceMetadata,
408
+ transactionNumber: event.baseTransactionNumber,
409
+ }
410
+ }
411
+
412
+ /**
413
+ * Adds a stable table ID to the shared Airtable event fields.
414
+ *
415
+ * @param event - Parsed provider transaction.
416
+ * @param baseId - Stable Airtable base ID.
417
+ * @param tableId - Stable Airtable table ID.
418
+ */
419
+ function tableEventBase(
420
+ event: z.output<typeof AIRTABLE_WEBHOOK_PAYLOAD_SCHEMA>,
421
+ baseId: string,
422
+ tableId: string,
423
+ ) {
424
+ return { ...baseEventBase(event, baseId), tableId }
425
+ }
426
+
427
+ /**
428
+ * Adds stable table and field IDs to the shared Airtable event fields.
429
+ *
430
+ * @param event - Parsed provider transaction.
431
+ * @param baseId - Stable Airtable base ID.
432
+ * @param tableId - Stable Airtable table ID.
433
+ * @param fieldId - Stable Airtable field ID.
434
+ */
435
+ function fieldEventBase(
436
+ event: z.output<typeof AIRTABLE_WEBHOOK_PAYLOAD_SCHEMA>,
437
+ baseId: string,
438
+ tableId: string,
439
+ fieldId: string,
440
+ ) {
441
+ return { ...tableEventBase(event, baseId, tableId), fieldId }
205
442
  }
206
443
 
207
444
  /**
@@ -219,13 +456,7 @@ function recordEventBase(
219
456
  recordId: string,
220
457
  ) {
221
458
  return {
222
- baseId,
223
- event,
224
- occurredAt: event.timestamp,
459
+ ...tableEventBase(event, baseId, tableId),
225
460
  recordId,
226
- source: event.actionMetadata.source,
227
- sourceMetadata: event.actionMetadata.sourceMetadata,
228
- tableId,
229
- transactionNumber: event.baseTransactionNumber,
230
461
  }
231
462
  }