Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.3 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  HttpMethod,
  HttpRequest,
  httpClient,
} from '@activepieces/pieces-common';
import { airtableAuth } from '../auth';
import { AirtableTable } from '../common/models';
import { updateTableActionOutputSchema } from '../output-schemas';

export const airtableUpdateTableAction = createAction({
  auth: airtableAuth,
  name: 'update_table',
  classification: 'WRITE',
  displayName: 'Update Table (Agent)',
  description: 'Rename or re-describe a table.',
  audience: 'ai',
  outputSchema: updateTableActionOutputSchema,
  aiMetadata: {
    description:
      'Renames a table and/or changes its description. Supply at least one of name or description. Requires a token with the schema.bases:write scope. Idempotent: setting the same name/description converges to the same state.',
    idempotent: true,
  },
  props: {
    base_id: Property.ShortText({
      displayName: 'Base ID',
      description:
        'The Airtable base ID (e.g. "appXXXXXXXXXXXXXX"). Resolve it with List Bases (Agent).',
      required: true,
    }),
    table_id_or_name: Property.ShortText({
      displayName: 'Table ID or Name',
      description:
        'The table ID (e.g. "tblXXXXXXXXXXXXXX") or its exact name. Resolve it with Get Base Schema (Agent).',
      required: true,
    }),
    name: Property.ShortText({
      displayName: 'New Name',
      description: 'Optional new name for the table.',
      required: false,
    }),
    description: Property.LongText({
      displayName: 'New Description',
      description: 'Optional new description for the table.',
      required: false,
    }),
  },
  async run(context) {
    const { auth, propsValue } = context;
    const { base_id, table_id_or_name, name, description } = propsValue;

    if (!name && !description) {
      throw new Error('Supply at least one of New Name or New Description.');
    }

    const body: { name?: string; description?: string } = {};
    if (name) body.name = name;
    if (description) body.description = description;

    const request: HttpRequest = {
      method: HttpMethod.PATCH,
      url: `https://api.airtable.com/v0/meta/bases/${base_id}/tables/${encodeURIComponent(
        table_id_or_name
      )}`,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: auth.secret_text,
      },
      body,
    };

    try {
      const response = await httpClient.sendRequest<AirtableTable>(request);
      return response.body;
    } catch (error: any) {
      const status = error?.response?.status;
      if (status === 401 || status === 403) {
        throw new Error(
          'Airtable rejected the request (permission). Updating a table requires a token with the schema.bases:write scope.'
        );
      }
      if (status === 404) {
        throw new Error(
          `Base "${base_id}" or table "${table_id_or_name}" was not found. Verify the IDs with List Bases (Agent) and Get Base Schema (Agent).`
        );
      }
      if (status === 422) {
        throw new Error(
          'Airtable rejected the update (422). Check the new name is unique within the base.'
        );
      }
      if (status === 429) {
        throw new Error('Airtable rate limit hit (429). Retry after a short delay.');
      }
      throw error;
    }
  },
});