Fix: Allow errors and special values on Airtable field set (#697)
* Allow errors and special values on field set * Add changeset
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@trigger.dev/airtable": patch
|
||||
---
|
||||
|
||||
Extend the Airtable field set to accept formula errors and special values
|
||||
@@ -38,6 +38,8 @@ export class Table<TFields extends AirtableFieldSet> {
|
||||
async (client) => {
|
||||
const result = await client
|
||||
.base(this.baseId)
|
||||
// official types are wrong - we need to use our extended AirtableFieldSet here
|
||||
// @ts-ignore
|
||||
.table<TFields>(this.tableName)
|
||||
.select(params)
|
||||
.all();
|
||||
@@ -55,6 +57,8 @@ export class Table<TFields extends AirtableFieldSet> {
|
||||
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<TFields>(this.tableName).find(recordId);
|
||||
return toSerializableRecord<TFields>(result);
|
||||
},
|
||||
@@ -75,6 +79,8 @@ export class Table<TFields extends AirtableFieldSet> {
|
||||
async (client) => {
|
||||
const result = await client
|
||||
.base(this.baseId)
|
||||
// official types are wrong - we need to use our extended AirtableFieldSet here
|
||||
// @ts-ignore
|
||||
.table<TFields>(this.tableName)
|
||||
.create(records);
|
||||
return result.map((record) => toSerializableRecord<TFields>(record));
|
||||
@@ -96,6 +102,8 @@ export class Table<TFields extends AirtableFieldSet> {
|
||||
async (client) => {
|
||||
const result = await client
|
||||
.base(this.baseId)
|
||||
// official types are wrong - we need to use our extended AirtableFieldSet here
|
||||
// @ts-ignore
|
||||
.table<TFields>(this.tableName)
|
||||
.update(records);
|
||||
return result.map((record) => toSerializableRecord<TFields>(record));
|
||||
@@ -117,6 +125,8 @@ export class Table<TFields extends AirtableFieldSet> {
|
||||
async (client) => {
|
||||
const result = await client
|
||||
.base(this.baseId)
|
||||
// official types are wrong - we need to use our extended AirtableFieldSet here
|
||||
// @ts-ignore
|
||||
.table<TFields>(this.tableName)
|
||||
.destroy(recordIds);
|
||||
return result.map((record) => toSerializableRecord<TFields>(record));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Formulas>(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",
|
||||
|
||||
Reference in New Issue
Block a user