import type { Encodable } from "@automate.ax/codec" import type { Vercel } from "@vercel/sdk" import * as z from "zod" import { defineAction } from "../../../automation/actions" import type { IntegrationScopeRequirement } from "../../../automation/integrations" import { getVercelApi, getVercelTeamId, vercelAccessTokenAccountOptions, vercelAccountOptions, } from "./api" /** Runtime-validates a provider SDK request while preserving its exact type. */ function vercelRequestSchema>() { return z.custom( (value) => typeof value === "object" && value !== null && !Array.isArray(value), "Expected a Vercel request object.", ) } /** Runtime-validates an encodable provider SDK response. */ function vercelResultSchema() { return z.custom() } /** * Adds an OAuth-installed team when the caller did not choose one. * * @param account - Resolved Vercel integration account. * @param request - Provider SDK request to scope. */ function withInstalledVercelTeam>( account: Parameters[0], request: TRequest, ) { const scope = request as { slug?: string; teamId?: string } if (scope.teamId || scope.slug) return request const teamId = getVercelTeamId(account) return teamId ? { ...request, teamId } : request } /** * Defines one typed SDK-backed Vercel action with OAuth and token support. * * @param options - Action metadata and official SDK handler. * @param options.description - Public action description. * @param options.handler - Official SDK operation to call. * @param options.name - Public action name. * @param options.replaySafety - Whether retries may replay the request. * @param options.scope - OAuth scope requirement for this operation. */ export function defineScopedVercelSdkAction< TRequest extends Record, TResult extends Encodable, >(options: { description: string handler: (api: Vercel, request: TRequest) => Promise name: string replaySafety: "safe" | "unsafe" scope: IntegrationScopeRequirement }) { return defineAction(options.name) .describe(options.description) .account("vercel", vercelAccountOptions(options.scope)) .input(vercelRequestSchema()) .output(vercelResultSchema()) .retry({ replaySafety: options.replaySafety }) .handler(({ account, input }) => options.handler( getVercelApi(account), withInstalledVercelTeam(account, input), ), ) } /** * Defines one typed SDK-backed Vercel action for personal access tokens. * * @param options - Action metadata and official SDK handler. * @param options.description - Public action description. * @param options.handler - Official SDK operation to call. * @param options.name - Public action name. * @param options.replaySafety - Whether retries may replay the request. */ export function defineTokenVercelSdkAction< TRequest extends Record, TResult extends Encodable, >(options: { description: string handler: (api: Vercel, request: TRequest) => Promise name: string replaySafety: "safe" | "unsafe" }) { return defineAction(options.name) .describe(options.description) .account("vercel", vercelAccessTokenAccountOptions()) .input(vercelRequestSchema()) .output(vercelResultSchema()) .retry({ replaySafety: options.replaySafety }) .handler(({ account, input }) => options.handler(getVercelApi(account), input), ) }