1 | |
2 |
|
3 | async function apiBase(auth: RT.AdobeAcrobatSign): Promise<string> { |
4 | if (auth.base_uri) return auth.base_uri.replace(/\/+$/, "") |
5 | const r = await fetch("https://api.adobesign.com/api/rest/v6/baseUris", { |
6 | headers: { |
7 | Authorization: `Bearer ${auth.token}`, |
8 | Accept: "application/json", |
9 | }, |
10 | }) |
11 | if (!r.ok) throw new Error(`${r.status} ${await r.text()}`) |
12 | const { apiAccessPoint } = (await r.json()) as { apiAccessPoint: string } |
13 | return apiAccessPoint.replace(/\/+$/, "") |
14 | } |
15 |
|
16 | |
17 | * Create Webhook |
18 | * Register a webhook that pushes Acrobat Sign events to a URL. The endpoint must echo the X-AdobeSign-ClientId header on the verification GET. |
19 | */ |
20 | export async function main( |
21 | auth: RT.AdobeAcrobatSign, |
22 | webhook: { |
23 | name?: string |
24 | scope?: "ACCOUNT" | "GROUP" | "USER" | "RESOURCE" |
25 | state?: "ACTIVE" | "INACTIVE" |
26 | webhookSubscriptionEvents?: string[] |
27 | webhookUrlInfo?: { url?: string } |
28 | resourceType?: string |
29 | resourceId?: string |
30 | [key: string]: unknown |
31 | } |
32 | ) { |
33 | const base = await apiBase(auth) |
34 | const url = new URL(`${base}/api/rest/v6/webhooks`) |
35 |
|
36 | const response = await fetch(url, { |
37 | method: "POST", |
38 | headers: { |
39 | Authorization: `Bearer ${auth.token}`, |
40 | "Content-Type": "application/json", |
41 | Accept: "application/json", |
42 | }, |
43 | body: JSON.stringify(webhook), |
44 | }) |
45 |
|
46 | if (!response.ok) { |
47 | throw new Error(`${response.status} ${await response.text()}`) |
48 | } |
49 |
|
50 | return await response.json() |
51 | } |
52 |
|