Skip to content

Commit b371f6e

Browse files
committed
Reworking event body format with additional info
1 parent cab4af5 commit b371f6e

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

components/airtable_oauth/sources/common/common-webhook.mjs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ export default {
9797
getSpecificationOptions() {
9898
throw new Error("getSpecificationOptions is not implemented");
9999
},
100-
generateMeta(payload) {
100+
generateMeta(payload, summary) {
101101
return {
102102
id: payload.baseTransactionNumber,
103-
summary: `New Event ${payload.baseTransactionNumber}`,
103+
summary: summary ?? `New Event ${payload.baseTransactionNumber}`,
104104
ts: Date.parse(payload.timestamp),
105105
};
106106
},
@@ -110,6 +110,15 @@ export default {
110110
getChangeTypes() {
111111
return this.changeTypes;
112112
},
113+
emitDefaultEvent(payload) {
114+
const meta = this.generateMeta(payload);
115+
this.$emit(payload, meta);
116+
},
117+
async saveAdditionalData() {
118+
// some sources may call this to fetch additional data (e.g. field schemas)
119+
// and it can be silently ignored when not required
120+
return true;
121+
}
113122
},
114123
async run() {
115124
this.http.respond({
@@ -119,6 +128,12 @@ export default {
119128
const webhookId = this._getHookId();
120129
let hasMore = false;
121130
const params = {};
131+
try {
132+
await this.saveAdditionalData();
133+
} catch (err) {
134+
console.log("Error fetching additional data, proceeding to event emission");
135+
console.log(err);
136+
}
122137
do {
123138
const {
124139
cursor, mightHaveMore, payloads,
@@ -128,8 +143,13 @@ export default {
128143
params,
129144
});
130145
for (const payload of payloads) {
131-
const meta = this.generateMeta(payload);
132-
this.$emit(payload, meta);
146+
try {
147+
this.emitEvent(payload);
148+
} catch (err) {
149+
console.log('Error emitting event, defaulting to default emission');
150+
console.log(err);
151+
this.emitDefaultEvent(payload);
152+
}
133153
}
134154
params.cursor = cursor;
135155
hasMore = mightHaveMore;

components/airtable_oauth/sources/new-or-modified-field/new-or-modified-field.mjs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
name: "New or Modified Field (Instant)",
66
description: "Emit new event when a field is created or updated in the selected table",
77
key: "airtable_oauth-new-or-modified-field",
8-
version: "1.0.0",
8+
version: "1.0.{{ts}}",
99
type: "source",
1010
dedupe: "unique",
1111
methods: {
@@ -16,5 +16,46 @@ export default {
1616
"update",
1717
];
1818
},
19+
async saveAdditionalData() {
20+
const tableData = await this.airtable.listTables({ baseId: this.baseId });
21+
const filteredData = tableData?.tables?.map(({ id, name, fields}) => ({
22+
id,
23+
name,
24+
fields,
25+
}));
26+
if (filteredData?.length) {
27+
this.db.set("tableSchemas", filteredData);
28+
}
29+
},
30+
async emitEvent(payload) {
31+
const [tableId, tableData] = Object.entries(payload.changedTablesById)[0];
32+
const [operation, fieldObj] = Object.entries(tableData)[0];
33+
const [fieldId, fieldUpdateInfo] = Object.entries(fieldObj)[0];
34+
35+
const updateType = operation === "createdFieldsById" ? "created" : "updated";
36+
37+
let table = {
38+
id: tableId,
39+
}
40+
let field = {
41+
id: fieldId,
42+
}
43+
44+
const tableSchemas = this.db.get("tableSchemas");
45+
if (tableSchemas) {
46+
table = tableSchemas.find(({ id }) => id === tableId)
47+
field = table?.fields.find(({ id }) => id === fieldId);
48+
delete table.fields;
49+
}
50+
51+
const summary = `Field ${updateType}: ${field.name ?? fieldUpdateInfo?.name ?? field.id}`
52+
53+
this.$emit({
54+
originalPayload: payload,
55+
table,
56+
field,
57+
fieldUpdateInfo,
58+
}, this.generateMeta(payload, summary));
59+
}
1960
},
2061
};

0 commit comments

Comments
 (0)