Edits history of script submission #22694 for ' Download Agreement PDF (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(/\/+$/, "")
    }
    
    /**
     * Download Agreement PDF
     * Download the combined PDF (all documents merged) of an agreement to Windmill object storage and return its S3 reference.
     */
    export async function main(
      auth: RT.AdobeAcrobatSign,
      agreement_id: string,
      destination: S3Object,
      attach_supporting_documents: boolean | undefined,
      audit_report: boolean | undefined
    ) {
      const base = await apiBase(auth)
      const url = new URL(
        `${base}/api/rest/v6/agreements/${agreement_id}/combinedDocument`
      )
      for (const [k, v] of [
        ["attachSupportingDocuments", attach_supporting_documents],
        ["auditReport", audit_report],
      ] as const) {
        if (v !== undefined) {
          url.searchParams.append(k, String(v))
        }
      }
    
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/pdf",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      await wmill.writeS3File(
        destination,
        response.body as ReadableStream<Uint8Array>
      )
      return destination
    }
    

    Submitted by hugo989 5 days ago