diff --git a/.changeset/silent-snails-float.md b/.changeset/silent-snails-float.md new file mode 100644 index 000000000..003bac869 --- /dev/null +++ b/.changeset/silent-snails-float.md @@ -0,0 +1,5 @@ +--- +"@trigger.dev/airtable": patch +--- + +Extend the Airtable field set to accept formula errors and special values diff --git a/integrations/airtable/src/base.ts b/integrations/airtable/src/base.ts index b354bc136..858bbf84e 100644 --- a/integrations/airtable/src/base.ts +++ b/integrations/airtable/src/base.ts @@ -38,6 +38,8 @@ export class Table { async (client) => { const result = await client .base(this.baseId) + // official types are wrong - we need to use our extended AirtableFieldSet here + // @ts-ignore .table(this.tableName) .select(params) .all(); @@ -55,6 +57,8 @@ export class Table { return this.runTask( key, async (client) => { + // official types are wrong - we need to use our extended AirtableFieldSet here + // @ts-ignore const result = await client.base(this.baseId).table(this.tableName).find(recordId); return toSerializableRecord(result); }, @@ -75,6 +79,8 @@ export class Table { async (client) => { const result = await client .base(this.baseId) + // official types are wrong - we need to use our extended AirtableFieldSet here + // @ts-ignore .table(this.tableName) .create(records); return result.map((record) => toSerializableRecord(record)); @@ -96,6 +102,8 @@ export class Table { async (client) => { const result = await client .base(this.baseId) + // official types are wrong - we need to use our extended AirtableFieldSet here + // @ts-ignore .table(this.tableName) .update(records); return result.map((record) => toSerializableRecord(record)); @@ -117,6 +125,8 @@ export class Table { async (client) => { const result = await client .base(this.baseId) + // official types are wrong - we need to use our extended AirtableFieldSet here + // @ts-ignore .table(this.tableName) .destroy(recordIds); return result.map((record) => toSerializableRecord(record)); diff --git a/integrations/airtable/src/types.ts b/integrations/airtable/src/types.ts index 8e917b72c..b108326c1 100644 --- a/integrations/airtable/src/types.ts +++ b/integrations/airtable/src/types.ts @@ -9,7 +9,8 @@ export type AirtableFieldSet = { | Collaborator | Collaborator[] | string[] - | Attachment[]; + | Attachment[] + | Formula; }; export type Collaborator = { @@ -31,6 +32,16 @@ export type Attachment = { }; }; +export type FormulaError = { + error: string; +}; + +export type FormulaSpecialValue = { + specialValue: "Infinity" | "NaN"; +}; + +export type Formula = string | number | FormulaError | FormulaSpecialValue; + export type Thumbnail = { url: string; width: number; diff --git a/references/job-catalog/src/airtable.ts b/references/job-catalog/src/airtable.ts index 703046bf3..6016a77c3 100644 --- a/references/job-catalog/src/airtable.ts +++ b/references/job-catalog/src/airtable.ts @@ -1,7 +1,7 @@ import { TriggerClient, eventTrigger } from "@trigger.dev/sdk"; import { createExpressServer } from "@trigger.dev/express"; import { z } from "zod"; -import { Airtable, Collaborator } from "@trigger.dev/airtable"; +import { Airtable, Collaborator, Formula } from "@trigger.dev/airtable"; export const client = new TriggerClient({ id: "job-catalog", @@ -77,6 +77,55 @@ client.defineJob({ }, }); +type Formulas = { + ValueOne?: number; + ValueTwo?: number; + Division: Formula; // ValueOne / ValueTwo +}; + +client.defineJob({ + id: "airtable-formulas", + name: "Airtable Formulas", + version: "0.1.0", + trigger: eventTrigger({ + name: "airtable.formulas", + schema: z.object({ + baseId: z.string(), + tableName: z.string(), + }), + }), + integrations: { + airtable, + }, + run: async (payload, io, ctx) => { + const table = io.airtable.base(payload.baseId).table(payload.tableName); + + const records = await table.getRecords("muliple records", { + fields: ["ValueOne", "ValueTwo", "Division"], + }); + + const divisionField = records[0].fields.Division; + + if (typeof divisionField !== "object") { + return records; + } + + if ("specialValue" in divisionField) { + await io.logger.log( + `Special value: ${divisionField.specialValue}${ + divisionField.specialValue === "Infinity" ? " - did you divide by zero?" : "" + }` + ); + } else if ("error" in divisionField) { + await io.logger.error(`Error: ${divisionField.error}`); + } else { + await io.logger.warn("Unknown formula value!"); + } + + return divisionField; + }, +}); + //todo webhooks require batch support // client.defineJob({ // id: "airtable-delete-webhooks",