1 | |
2 | type Box = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create collaboration |
7 | * Adds a collaboration for a single user or a single group to a file |
8 | or folder. |
9 | */ |
10 | export async function main( |
11 | auth: Box, |
12 | fields: string | undefined, |
13 | notify: string | undefined, |
14 | body: { |
15 | item: { type?: "file" | "folder"; id?: string }; |
16 | accessible_by: { type: "user" | "group"; id?: string; login?: string }; |
17 | role: |
18 | | "editor" |
19 | | "viewer" |
20 | | "previewer" |
21 | | "uploader" |
22 | | "previewer uploader" |
23 | | "viewer uploader" |
24 | | "co-owner"; |
25 | is_access_only?: false | true; |
26 | can_view_path?: false | true; |
27 | expires_at?: string; |
28 | }, |
29 | ) { |
30 | const url = new URL(`https://api.box.com/2.0/collaborations`); |
31 | for (const [k, v] of [ |
32 | ["fields", fields], |
33 | ["notify", notify], |
34 | ]) { |
35 | if (v !== undefined && v !== "" && k !== undefined) { |
36 | url.searchParams.append(k, v); |
37 | } |
38 | } |
39 | const response = await fetch(url, { |
40 | method: "POST", |
41 | headers: { |
42 | "Content-Type": "application/json", |
43 | Authorization: "Bearer " + auth.token, |
44 | }, |
45 | body: JSON.stringify(body), |
46 | }); |
47 | if (!response.ok) { |
48 | const text = await response.text(); |
49 | throw new Error(`${response.status} ${text}`); |
50 | } |
51 | return await response.json(); |
52 | } |
53 |
|