0

Render an integration UI with POST method

by
Published Oct 17, 2025
Script gitbook Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gitbook = {
3
  token: string;
4
};
5
/**
6
 * Render an integration UI with POST method
7
 *
8
 */
9
export async function main(
10
  auth: Gitbook,
11
  integrationName: string,
12
  body: {
13
    componentId: string;
14
    props: {};
15
    state?: {};
16
    context:
17
      | ({ theme: "dark" | "light" } & {
18
          type: "configuration_account";
19
          organizationId: string;
20
        })
21
      | ({ theme: "dark" | "light" } & {
22
          type: "configuration_space";
23
          spaceId: string;
24
        })
25
      | ({ theme: "dark" | "light" } & {
26
          type: "configuration_site";
27
          siteId: string;
28
        })
29
      | ({ theme: "dark" | "light" } & {
30
          type: "configuration_contentsource";
31
          organizationId: string;
32
          spaceId?: string;
33
        })
34
      | ({ theme: "dark" | "light" } & {
35
          type: "document";
36
          spaceId: string;
37
          editable: false | true;
38
        });
39
    action?:
40
      | { action: string }
41
      | { action: "@ui.modal.open"; componentId: string; props: {} }
42
      | { action: "@ui.modal.close"; returnValue: {} }
43
      | { action: "@ui.url.open"; url: string }
44
      | { action: "@link.unfurl"; url: string }
45
      | { action: "@editor.node.updateProps"; props: {} };
46
  },
47
) {
48
  const url = new URL(`https://api.gitbook.com/v1/integrations/${integrationName}/render`);
49

50
  const response = await fetch(url, {
51
    method: "POST",
52
    headers: {
53
      "Content-Type": "application/json",
54
      Authorization: "Bearer " + auth.token,
55
    },
56
    body: JSON.stringify(body),
57
  });
58
  if (!response.ok) {
59
    const text = await response.text();
60
    throw new Error(`${response.status} ${text}`);
61
  }
62
  return await response.json();
63
}
64