Edits history of script submission #22709 for ' Upload Transient Document (adobe_acrobat_sign)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import * as wmill from "windmill-client"
    import { S3Object } from "windmill-client"
    
    async function apiBase(auth: RT.AdobeAcrobatSign): Promise<string> {
      if (auth.base_uri) return auth.base_uri.replace(/\/+$/, "")
      const r = await fetch("https://api.adobesign.com/api/rest/v6/baseUris", {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
      const { apiAccessPoint } = (await r.json()) as { apiAccessPoint: string }
      return apiAccessPoint.replace(/\/+$/, "")
    }
    
    /**
     * Upload Transient Document
     * Upload a file from Windmill object storage as a transient document. Returns a transientDocumentId to reference in Create Agreement (valid ~7 days, single use).
     */
    export async function main(
      auth: RT.AdobeAcrobatSign,
      file: S3Object,
      file_name: string,
      mime_type: string | undefined
    ) {
      const bytes = await wmill.loadS3File(file)
      if (!bytes) {
        throw new Error("Could not load the file from object storage")
      }
    
      const form = new FormData()
      form.append("File-Name", file_name)
      if (mime_type !== undefined && mime_type !== "") {
        form.append("Mime-Type", mime_type)
      }
      form.append("File", new Blob([bytes]), file_name)
    
      const base = await apiBase(auth)
      const response = await fetch(`${base}/api/rest/v6/transientDocuments`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
        body: form,
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago