|
| 1 | +import type { AppContext } from "../mod.ts"; |
| 2 | +import type { |
| 3 | + AirtableRecord, |
| 4 | + CreateRecordsBody, |
| 5 | + FieldSet, |
| 6 | +} from "../utils/types.ts"; |
| 7 | + |
| 8 | +interface RecordToCreate { |
| 9 | + fields: FieldSet; |
| 10 | + typecast?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +interface Props { |
| 14 | + /** |
| 15 | + * @title Base ID |
| 16 | + * @description The ID of the Airtable base |
| 17 | + */ |
| 18 | + baseId: string; |
| 19 | + |
| 20 | + /** |
| 21 | + * @title Table ID |
| 22 | + * @description The ID of the table within the base |
| 23 | + */ |
| 24 | + tableId: string; |
| 25 | + |
| 26 | + /** |
| 27 | + * @title Records to Create |
| 28 | + * @description An array of records to create. Each record must have a fields object. |
| 29 | + */ |
| 30 | + records: RecordToCreate[]; |
| 31 | + |
| 32 | + /** |
| 33 | + * @title Typecast |
| 34 | + * @description Optional. If true, Airtable will attempt to convert cell values to the appropriate type. |
| 35 | + */ |
| 36 | + typecast?: boolean; |
| 37 | +} |
| 38 | + |
| 39 | +interface ErrorObject { |
| 40 | + message: string; |
| 41 | + code?: string; |
| 42 | +} |
| 43 | + |
| 44 | +interface ResponsePayload { |
| 45 | + data: AirtableRecord[]; |
| 46 | + error?: ErrorObject | null; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @title Create Airtable Records |
| 51 | + * @description Creates one or more records in a specified table using OAuth. |
| 52 | + */ |
| 53 | +const action = async ( |
| 54 | + props: Props, |
| 55 | + _req: Request, |
| 56 | + ctx: AppContext, |
| 57 | +): Promise<ResponsePayload> => { |
| 58 | + try { |
| 59 | + if (!ctx.client) { |
| 60 | + return { |
| 61 | + data: [], |
| 62 | + error: { message: "OAuth authentication is required" }, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + const validationResult = await ctx.invoke["airtable"].loaders.permissioning |
| 67 | + .validatePermissions({ |
| 68 | + mode: "check", |
| 69 | + baseId: props.baseId, |
| 70 | + tableIdOrName: props.tableId, |
| 71 | + }); |
| 72 | + |
| 73 | + if ( |
| 74 | + "hasPermission" in validationResult && !validationResult.hasPermission |
| 75 | + ) { |
| 76 | + return { |
| 77 | + data: [], |
| 78 | + error: { |
| 79 | + message: validationResult.message || "Access denied", |
| 80 | + }, |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + const { baseId, tableId, records, typecast } = props; |
| 85 | + |
| 86 | + if (!records || records.length === 0) { |
| 87 | + return { |
| 88 | + data: [], |
| 89 | + error: { message: "At least one record is required" }, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + if (records.length > 10) { |
| 94 | + return { |
| 95 | + data: [], |
| 96 | + error: { message: "Maximum 10 records can be created at once" }, |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + const invalidRecords = records.filter((record) => !record.fields); |
| 101 | + if (invalidRecords.length > 0) { |
| 102 | + return { |
| 103 | + data: [], |
| 104 | + error: { message: "All records must have a 'fields' property" }, |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + // Transform records to the format expected by Airtable API |
| 109 | + const requestBody: CreateRecordsBody = { |
| 110 | + records: records.map((record) => ({ |
| 111 | + fields: record.fields, |
| 112 | + ...(record.typecast !== undefined && { typecast: record.typecast }), |
| 113 | + })), |
| 114 | + }; |
| 115 | + |
| 116 | + // Apply global typecast if provided |
| 117 | + if ( |
| 118 | + typecast !== undefined && !records.some((r) => r.typecast !== undefined) |
| 119 | + ) { |
| 120 | + requestBody.typecast = typecast; |
| 121 | + } |
| 122 | + |
| 123 | + // deno-lint-ignore no-explicit-any |
| 124 | + const response = await (ctx.client as any)["POST /v0/:baseId/:tableId"]( |
| 125 | + { baseId, tableId }, |
| 126 | + { body: requestBody }, |
| 127 | + ); |
| 128 | + |
| 129 | + if (!response.ok) { |
| 130 | + const errorText = await response.text(); |
| 131 | + return { |
| 132 | + data: [], |
| 133 | + error: { message: `Error creating records: ${errorText}` }, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + const result = await response.json(); |
| 138 | + const createdRecords = result.records || []; |
| 139 | + |
| 140 | + return { |
| 141 | + data: createdRecords, |
| 142 | + }; |
| 143 | + } catch (error) { |
| 144 | + const errorMessage = error instanceof Error |
| 145 | + ? error.message |
| 146 | + : "Unknown error occurred"; |
| 147 | + |
| 148 | + return { |
| 149 | + data: [], |
| 150 | + error: { message: errorMessage }, |
| 151 | + }; |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +export default action; |
0 commit comments