0

Upload Transient Document

by
Published 4 days ago

Upload a file from Windmill object storage as a transient document; returns a transientDocumentId to reference when creating an agreement.

Script adobe_acrobat_sign Verified

The script

Submitted by hugo989 Bun
Verified 5 days ago
1
import * as wmill from "windmill-client"
2
import { S3Object } from "windmill-client"
3

4
async function apiBase(auth: RT.AdobeAcrobatSign): Promise<string> {
5
  if (auth.base_uri) return auth.base_uri.replace(/\/+$/, "")
6
  const r = await fetch("https://api.adobesign.com/api/rest/v6/baseUris", {
7
    headers: {
8
      Authorization: `Bearer ${auth.token}`,
9
      Accept: "application/json",
10
    },
11
  })
12
  if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
13
  const { apiAccessPoint } = (await r.json()) as { apiAccessPoint: string }
14
  return apiAccessPoint.replace(/\/+$/, "")
15
}
16

17
/**
18
 * Upload Transient Document
19
 * Upload a file from Windmill object storage as a transient document. Returns a transientDocumentId to reference in Create Agreement (valid ~7 days, single use).
20
 */
21
export async function main(
22
  auth: RT.AdobeAcrobatSign,
23
  file: S3Object,
24
  file_name: string,
25
  mime_type: string | undefined
26
) {
27
  const bytes = await wmill.loadS3File(file)
28
  if (!bytes) {
29
    throw new Error("Could not load the file from object storage")
30
  }
31

32
  const form = new FormData()
33
  form.append("File-Name", file_name)
34
  if (mime_type !== undefined && mime_type !== "") {
35
    form.append("Mime-Type", mime_type)
36
  }
37
  form.append("File", new Blob([bytes]), file_name)
38

39
  const base = await apiBase(auth)
40
  const response = await fetch(`${base}/api/rest/v6/transientDocuments`, {
41
    method: "POST",
42
    headers: {
43
      Authorization: `Bearer ${auth.token}`,
44
      Accept: "application/json",
45
    },
46
    body: form,
47
  })
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  return await response.json()
54
}
55