import { defineAction } from "../../../automation/actions" import type { Webflow } from "webflow-api" import * as serializers from "webflow-api/serialization" import { getWebflowApi, webflowAction, webflowActionSchemas, webflowInputSchema, webflowObjectSchema, webflowRequest, webflowVoidSchema, type WebflowInputValue, } from "../lib" type SiteInput = WebflowInputValue & { siteId: string } type CollectionInput = WebflowInputValue & { collectionId: string } type FieldInput = CollectionInput & { fieldId: string } /** Lists CMS collections in one Webflow site. */ export const listWebflowCollections = webflowAction< SiteInput, Webflow.CollectionList >( defineAction("List Webflow collections") .describe("Lists CMS collections and their schemas for one site.") .account("webflow", "cms:read") .input(webflowInputSchema(webflowActionSchemas.id("siteId"))) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).collections.list(input.siteId), ), ) /** Retrieves one Webflow CMS collection and its fields. */ export const getWebflowCollection = webflowAction< CollectionInput, Webflow.Collection >( defineAction("Get Webflow collection") .describe("Retrieves one CMS collection and its field schema.") .account("webflow", "cms:read") .input( webflowInputSchema( webflowActionSchemas.id("collectionId"), ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).collections.get(input.collectionId), ), ) /** Creates a Webflow CMS collection with an initial field schema. */ export const createWebflowCollection = webflowAction< SiteInput, Webflow.Collection >( defineAction("Create Webflow collection") .describe("Creates a CMS collection and its initial fields.") .account("webflow", "cms:write") .input( webflowInputSchema>( webflowActionSchemas.id("siteId"), serializers.CollectionsCreateRequest, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).collections.create( input.siteId, webflowRequest(input, "siteId"), ), ), ) /** Permanently deletes a Webflow CMS collection. */ export const deleteWebflowCollection = webflowAction( defineAction("Delete Webflow collection") .describe("Permanently deletes a CMS collection and its items.") .account("webflow", "cms:write") .input( webflowInputSchema( webflowActionSchemas.id("collectionId"), ), ) .output(webflowVoidSchema()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getWebflowApi(account).collections.delete(input.collectionId) }), ) /** Adds a field to a Webflow CMS collection. */ export const createWebflowCollectionField = webflowAction< CollectionInput, Webflow.FieldCreate >( defineAction("Create Webflow collection field") .describe("Adds a typed field to an existing CMS collection.") .account("webflow", "cms:write") .input( webflowInputSchema>( webflowActionSchemas.id("collectionId"), serializers.FieldCreate, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).collections.fields.create( input.collectionId, webflowRequest(input, "collectionId"), ), ), ) /** Updates the mutable settings of a Webflow collection field. */ export const updateWebflowCollectionField = webflowAction< FieldInput, Webflow.Field >( defineAction("Update Webflow collection field") .describe("Updates the label or help text for a CMS collection field.") .account("webflow", "cms:write") .input( webflowInputSchema>( webflowActionSchemas.ids("collectionId", "fieldId"), serializers.collections.FieldUpdate, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).collections.fields.update( input.collectionId, input.fieldId, webflowRequest(input, "collectionId", "fieldId"), ), ), ) /** Permanently deletes a field from a Webflow CMS collection. */ export const deleteWebflowCollectionField = webflowAction( defineAction("Delete Webflow collection field") .describe("Permanently deletes a field from a CMS collection.") .account("webflow", "cms:write") .input( webflowInputSchema( webflowActionSchemas.ids("collectionId", "fieldId"), ), ) .output(webflowVoidSchema()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getWebflowApi(account).collections.fields.delete( input.collectionId, input.fieldId, ) }), )